Skip to content

Commit

Permalink
Fix URL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Mar 6, 2024
1 parent 463b280 commit f2cd16b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];

Expand Down
20 changes: 20 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit f2cd16b

Please sign in to comment.