Skip to content
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
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::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'),
'version' => $this->version,
];

Expand Down
39 changes: 39 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,43 @@ public function test_responsable_with_invalid_key(): void
$page['props']['resource']
);
}

public function test_the_page_url_is_prefixed_with_the_proxy_prefix(): void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this test passed prior to the changes. It exists to ensure no regression on the issue that #333 was solving.

{
if (version_compare(app()->version(), '7', '<')) {
$this->markTestSkipped('This test requires Laravel 7 or higher.');
}

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']);
}

public function test_the_page_url_doesnt_double_up(): void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test previously failed. I took it from #360.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue could be hosting Laravel in a subdirectory, which Laravel explicitly doesn't support. That being said, the previous combination of baseUrl and getRequestUri doesn't seem correct.

{
$request = Request::create('/subpath/product/123', 'GET', [], [], [], [
'SCRIPT_FILENAME' => '/project/public/index.php',
'SCRIPT_NAME' => '/subpath/index.php',
]);
$request->headers->add(['X-Inertia' => 'true']);

$response = new Response('Product/Show', []);
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertSame('/subpath/product/123', $page->url);
}
}