From 3525a3c3b59b49a678cc57a3639b340525d325c5 Mon Sep 17 00:00:00 2001 From: Roj Vroemen Date: Sun, 19 Sep 2021 15:54:28 +0200 Subject: [PATCH 1/2] Allow ResponseFactory location to be called with a redirect response --- src/ResponseFactory.php | 5 +++++ tests/ResponseFactoryTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 11d3530d..ce721531 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Response as BaseResponse; use Illuminate\Support\Traits\Macroable; +use Symfony\Component\HttpFoundation\RedirectResponse; class ResponseFactory { @@ -82,6 +83,10 @@ public function render($component, $props = []) public function location($url) { + if ($url instanceof RedirectResponse) { + $url = $url->getTargetUrl(); + } + return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]); } } diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 6888d651..49d7aeae 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -9,6 +9,7 @@ use Inertia\LazyProp; use Inertia\ResponseFactory; use Inertia\Tests\Stubs\ExampleMiddleware; +use Symfony\Component\HttpFoundation\RedirectResponse; class ResponseFactoryTest extends TestCase { @@ -31,6 +32,16 @@ public function test_location_response() $this->assertEquals('https://inertiajs.com', $response->headers->get('X-Inertia-Location')); } + public function test_location_response_from_redirect() + { + $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_the_version_can_be_a_closure() { Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () { From b48e03534b7d30deaadb5630a88370a1ab783ee5 Mon Sep 17 00:00:00 2001 From: Claudio Dekker Date: Thu, 2 Dec 2021 12:08:31 +0100 Subject: [PATCH 2/2] WIP --- src/ResponseFactory.php | 7 +++---- tests/ResponseFactoryTest.php | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 2427c70f..5f735134 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Response as BaseResponse; use Illuminate\Support\Traits\Macroable; -use Symfony\Component\HttpFoundation\RedirectResponse; class ResponseFactory { @@ -84,15 +83,15 @@ 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]); } - + return new RedirectResponse($url); } } diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 2243b181..1c533ef0 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -11,7 +11,6 @@ use Inertia\LazyProp; use Inertia\ResponseFactory; use Inertia\Tests\Stubs\ExampleMiddleware; -use Symfony\Component\HttpFoundation\RedirectResponse; class ResponseFactoryTest extends TestCase { @@ -37,7 +36,7 @@ public function test_location_response_for_inertia_requests() $this->assertEquals(Response::HTTP_CONFLICT, $response->getStatusCode()); $this->assertEquals('https://inertiajs.com', $response->headers->get('X-Inertia-Location')); } - + public function test_location_response_for_non_inertia_requests() { Request::macro('inertia', function () { @@ -51,8 +50,12 @@ public function test_location_response_for_non_inertia_requests() $this->assertEquals('https://inertiajs.com', $response->headers->get('location')); } - public function test_location_response_from_redirect_response() + 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); @@ -61,6 +64,16 @@ public function test_location_response_from_redirect_response() $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 () {