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

Restore request and session on redirects #539

Merged
merged 4 commits into from
Sep 13, 2023
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
16 changes: 7 additions & 9 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response as BaseResponse;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;

class ResponseFactory
{
Expand Down Expand Up @@ -103,18 +105,14 @@ public function render(string $component, $props = []): Response
}

/**
* @param string|RedirectResponse $url
* @param string|SymfonyRedirect $url
*/
public function location($url): \Symfony\Component\HttpFoundation\Response
public function location($url): SymfonyResponse
{
if ($url instanceof RedirectResponse) {
$url = $url->getTargetUrl();
}

if (Request::inertia()) {
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url instanceof SymfonyRedirect ? $url->getTargetUrl() : $url]);
}

return new RedirectResponse($url);
return $url instanceof SymfonyRedirect ? $url : Redirect::away($url);
}
}
27 changes: 27 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use Illuminate\Support\Facades\Request;
use Inertia\Tests\Stubs\ExampleMiddleware;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\Store;

class ResponseFactoryTest extends TestCase
{
Expand Down Expand Up @@ -75,6 +78,30 @@ public function test_location_response_for_non_inertia_requests_using_redirect_r
$this->assertEquals('https://inertiajs.com', $response->headers->get('location'));
}

public function test_location_redirects_are_not_modified(): void
{
$response = (new ResponseFactory())->location('/foo');

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

public function test_location_response_for_non_inertia_requests_using_redirect_response_with_existing_session_and_request_properties(): void
{
$redirect = new RedirectResponse('https://inertiajs.com');
$redirect->setSession($session = new Store('test', new NullSessionHandler));
$redirect->setRequest($request = new HttpRequest);
$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'));
$this->assertSame($session, $response->getSession());
$this->assertSame($request, $response->getRequest());
$this->assertSame($response, $redirect);
}

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