From f2cd16b5f31488eb7cb6dfa278b614ceb9acc27c Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 6 Mar 2024 15:24:04 +1000 Subject: [PATCH] Fix URL generation --- src/Response.php | 3 ++- tests/ResponseTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Response.php b/src/Response.php index 953a8e52..756abd84 100644 --- a/src/Response.php +++ b/src/Response.php @@ -7,6 +7,7 @@ use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\App; +use Illuminate\Support\Str; use GuzzleHttp\Promise\PromiseInterface; use Illuminate\Support\Traits\Macroable; use Illuminate\Contracts\Support\Arrayable; @@ -99,7 +100,7 @@ public function toResponse($request) $page = [ 'component' => $this->component, 'props' => $props, - 'url' => $request->getBaseUrl().$request->getRequestUri(), + 'url' => Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), 'version' => $this->version, ]; diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index f8035c51..07cae269 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -361,4 +361,24 @@ public function test_responsable_with_invalid_key(): void $page['props']['resource'] ); } + + public function test_the_page_url_is_prefixed_with_the_proxy_prefix(): void + { + Request::setTrustedProxies(['1.2.3.4'], Request::HEADER_X_FORWARDED_PREFIX); + + $request = Request::create('/user/123', 'GET'); + $request->server->set('REMOTE_ADDR', '1.2.3.4'); + $request->headers->set('X_FORWARDED_PREFIX', '/sub/directory'); + + $user = ['name' => 'Jonathan']; + $response = new Response('User/Edit', ['user' => $user], 'app', '123'); + $response = $response->toResponse($request); + $view = $response->getOriginalContent(); + $page = $view->getData()['page']; + + $this->assertInstanceOf(BaseResponse::class, $response); + $this->assertInstanceOf(View::class, $view); + + $this->assertSame('/sub/directory/user/123', $page['url']); + } }