Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ResponseFactory location to be called with a redirect response #302

Merged
merged 3 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function render($component, $props = [])

public function location($url)
{
if ($url instanceof RedirectResponse) {
$url = $url->getTargetUrl();
}

if (Request::inertia()) {
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ public function test_location_response_for_non_inertia_requests()
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
}

public function test_location_response_for_inertia_requests_using_redirect_response()
{
Request::macro('inertia', function () {
return true;
});

$redirect = new RedirectResponse('https://inertiajs.com');
$response = (new ResponseFactory())->location($redirect);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(409, $response->getStatusCode());
$this->assertEquals('https://inertiajs.com', $response->headers->get('X-Inertia-Location'));
}

public function test_location_response_for_non_inertia_requests_using_redirect_response()
{
$redirect = new RedirectResponse('https://inertiajs.com');
$response = (new ResponseFactory())->location($redirect);

$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
}

public function test_the_version_can_be_a_closure()
{
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
Expand Down