Skip to content

Commit b5942a5

Browse files
committed
Merge branch '2.x' into pr/732
2 parents 054bd74 + cf9c8b3 commit b5942a5

File tree

6 files changed

+109
-10
lines changed

6 files changed

+109
-10
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
tests:
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-24.04
1212
strategy:
1313
fail-fast: true
1414
matrix:

src/Response.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function toResponse($request)
109109
[
110110
'component' => $this->component,
111111
'props' => $props,
112-
'url' => Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'),
112+
'url' => $this->getUrl($request),
113113
'version' => $this->version,
114114
'clearHistory' => $this->clearHistory,
115115
'encryptHistory' => $this->encryptHistory,
@@ -369,4 +369,29 @@ public function isPartial(Request $request): bool
369369
{
370370
return $request->header(Header::PARTIAL_COMPONENT) === $this->component;
371371
}
372+
373+
/**
374+
* Get the URL from the request (without the scheme and host) while preserving the trailing slash if it exists.
375+
*/
376+
protected function getUrl(Request $request): string
377+
{
378+
$url = Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/');
379+
380+
$rawUri = Str::before($request->getRequestUri(), '?');
381+
382+
return Str::endsWith($rawUri, '/') ? $this->finishUrlWithTrailingSlash($url) : $url;
383+
}
384+
385+
/**
386+
* Ensure the URL has a trailing slash before the query string (if it exists).
387+
*/
388+
protected function finishUrlWithTrailingSlash(string $url): string
389+
{
390+
// Make sure the relative URL ends with a trailing slash and re-append the query string if it exists.
391+
$urlWithoutQueryWithTrailingSlash = Str::finish(Str::before($url, '?'), '/');
392+
393+
return str_contains($url, '?')
394+
? $urlWithoutQueryWithTrailingSlash.'?'.Str::after($url, '?')
395+
: $urlWithoutQueryWithTrailingSlash;
396+
}
372397
}

src/ServiceProvider.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Inertia;
44

5-
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;
65
use Illuminate\Http\Request;
76
use Illuminate\Routing\Router;
87
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
@@ -99,13 +98,6 @@ protected function registerTestingMacros(): void
9998
return;
10099
}
101100

102-
// Laravel <= 6.0
103-
if (class_exists(LegacyTestResponse::class)) {
104-
LegacyTestResponse::mixin(new TestResponseMacros);
105-
106-
return;
107-
}
108-
109101
throw new LogicException('Could not detect TestResponse class.');
110102
}
111103

src/Testing/TestResponseMacros.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inertia\Testing;
44

55
use Closure;
6+
use Illuminate\Support\Arr;
67

78
class TestResponseMacros
89
{
@@ -27,4 +28,11 @@ public function inertiaPage()
2728
return AssertableInertia::fromTestResponse($this)->toArray();
2829
};
2930
}
31+
32+
public function inertiaProps()
33+
{
34+
return function (?string $propName = null) {
35+
return Arr::get($this->inertiaPage()['props'], $propName);
36+
};
37+
}
3038
}

tests/ResponseTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,4 +866,52 @@ public function test_the_page_url_doesnt_double_up(): void
866866

867867
$this->assertSame('/subpath/product/123', $page->url);
868868
}
869+
870+
public function test_trailing_slashes_in_a_url_are_preserved(): void
871+
{
872+
$request = Request::create('/users/', 'GET');
873+
$request->headers->add(['X-Inertia' => 'true']);
874+
875+
$response = new Response('User/Index', []);
876+
$response = $response->toResponse($request);
877+
$page = $response->getData();
878+
879+
$this->assertSame('/users/', $page->url);
880+
}
881+
882+
public function test_trailing_slashes_in_a_url_with_query_parameters_are_preserved(): void
883+
{
884+
$request = Request::create('/users/?page=1&sort=name', 'GET');
885+
$request->headers->add(['X-Inertia' => 'true']);
886+
887+
$response = new Response('User/Index', []);
888+
$response = $response->toResponse($request);
889+
$page = $response->getData();
890+
891+
$this->assertSame('/users/?page=1&sort=name', $page->url);
892+
}
893+
894+
public function test_a_url_without_trailing_slash_is_resolved_correctly(): void
895+
{
896+
$request = Request::create('/users', 'GET');
897+
$request->headers->add(['X-Inertia' => 'true']);
898+
899+
$response = new Response('User/Index', []);
900+
$response = $response->toResponse($request);
901+
$page = $response->getData();
902+
903+
$this->assertSame('/users', $page->url);
904+
}
905+
906+
public function test_a_url_without_trailing_slash_and_query_parameters_is_resolved_correctly(): void
907+
{
908+
$request = Request::create('/users?page=1&sort=name', 'GET');
909+
$request->headers->add(['X-Inertia' => 'true']);
910+
911+
$response = new Response('User/Index', []);
912+
$response = $response->toResponse($request);
913+
$page = $response->getData();
914+
915+
$this->assertSame('/users?page=1&sort=name', $page->url);
916+
}
869917
}

tests/Testing/TestResponseMacrosTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,30 @@ public function test_it_can_retrieve_the_inertia_page(): void
5050
$this->assertFalse($page['clearHistory']);
5151
});
5252
}
53+
54+
public function test_it_can_retrieve_the_inertia_props(): void
55+
{
56+
$props = ['bar' => 'baz'];
57+
$response = $this->makeMockRequest(
58+
Inertia::render('foo', $props)
59+
);
60+
61+
$this->assertSame($props, $response->inertiaProps());
62+
}
63+
64+
public function test_it_can_retrieve_nested_inertia_prop_values_with_dot_notation(): void
65+
{
66+
$response = $this->makeMockRequest(
67+
Inertia::render('foo', [
68+
'bar' => ['baz' => 'qux'],
69+
'users' => [
70+
['name' => 'John'],
71+
['name' => 'Jane'],
72+
],
73+
])
74+
);
75+
76+
$this->assertSame('qux', $response->inertiaProps('bar.baz'));
77+
$this->assertSame('John', $response->inertiaProps('users.0.name'));
78+
}
5379
}

0 commit comments

Comments
 (0)