Skip to content

Commit

Permalink
Merge pull request #539 from timacdonald/session
Browse files Browse the repository at this point in the history
Restore request and session on redirects
  • Loading branch information
jessarcher authored Sep 13, 2023
2 parents a657a07 + e662c4e commit b79dadf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
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

0 comments on commit b79dadf

Please sign in to comment.