Skip to content

Commit ab1f0a7

Browse files
committed
Fixes issue 187
See inertiajs/inertia#187
1 parent e7b9527 commit ab1f0a7

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/ResponseFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public function version($version)
4646

4747
public function getVersion()
4848
{
49-
return $this->version instanceof Closure ? App::call($this->version) : $this->version;
49+
$version = $this->version instanceof Closure
50+
? App::call($this->version)
51+
: $this->version;
52+
53+
return (string) $version;
5054
}
5155

5256
public function render($component, $props = [])

tests/MiddlewareTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Illuminate\Foundation\Testing\TestResponse;
6+
use Illuminate\Http\Request;
7+
use Inertia\Inertia;
8+
use Inertia\Middleware;
9+
10+
class MiddlewareTest extends TestCase
11+
{
12+
public function test_the_version_can_be_a_number()
13+
{
14+
Inertia::version(1597347897973);
15+
16+
$request = Request::create('/user/123', 'GET');
17+
$request->headers->add(['X-Inertia' => 'true']);
18+
$request->headers->add(['X-Inertia-Version' => 1597347897973]);
19+
20+
$response = $this->makeMockResponse($request);
21+
22+
$response->assertSuccessful();
23+
$response->assertJson([
24+
'component' => 'User/Edit',
25+
]);
26+
}
27+
28+
public function test_the_version_can_be_a_string()
29+
{
30+
Inertia::version('foo-version');
31+
32+
$request = Request::create('/user/edit', 'GET');
33+
$request->headers->add(['X-Inertia' => 'true']);
34+
$request->headers->add(['X-Inertia-Version' => 'foo-version']);
35+
36+
$response = $this->makeMockResponse($request);
37+
38+
$response->assertSuccessful();
39+
$response->assertJson([
40+
'component' => 'User/Edit',
41+
]);
42+
}
43+
44+
public function test_it_will_instruct_inertia_to_reload_on_a_version_mismatch()
45+
{
46+
Inertia::version(1234);
47+
48+
$request = Request::create('/user/123', 'GET');
49+
$request->headers->add(['X-Inertia' => 'true']);
50+
$request->headers->add(['X-Inertia-Version' => 4321]);
51+
52+
$response = $this->makeMockResponse($request);
53+
54+
$response->assertStatus(409);
55+
$response->assertHeader('X-Inertia-Location', $request->fullUrl());
56+
self::assertEmpty($response->content());
57+
}
58+
59+
private function makeMockResponse($request)
60+
{
61+
$response = (new Middleware())->handle($request, function ($request) {
62+
return Inertia::render('User/Edit', ['user' => ['name' => 'Jonathan']])->toResponse($request);
63+
});
64+
65+
return TestResponse::fromBaseResponse($response);
66+
}
67+
}

0 commit comments

Comments
 (0)