Skip to content

Commit 1a99ec8

Browse files
committed
history middleware tests
1 parent 735d20f commit 1a99ec8

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ServiceProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Inertia;
44

55
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;
6-
use Illuminate\Foundation\Vite;
76
use Illuminate\Http\Request;
87
use Illuminate\Routing\Router;
98
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
@@ -32,6 +31,7 @@ public function register(): void
3231
$this->registerRequestMacro();
3332
$this->registerRouterMacro();
3433
$this->registerTestingMacros();
34+
$this->registerMiddleware();
3535

3636
$this->app->bind('inertia.testing.view-finder', function ($app) {
3737
return new FileViewFinder(
@@ -108,4 +108,12 @@ protected function registerTestingMacros(): void
108108

109109
throw new LogicException('Could not detect TestResponse class.');
110110
}
111+
112+
protected function registerMiddleware(): void
113+
{
114+
$this->app['router']->aliasMiddleware(
115+
'inertia.encrypt',
116+
EncryptHistoryMiddleware::class
117+
);
118+
}
111119
}

tests/HistoryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Session\Middleware\StartSession;
66
use Illuminate\Support\Facades\Config;
77
use Illuminate\Support\Facades\Route;
8+
use Inertia\EncryptHistoryMiddleware;
89
use Inertia\Inertia;
910
use Inertia\Tests\Stubs\ExampleMiddleware;
1011

@@ -47,6 +48,40 @@ public function test_the_history_can_be_encrypted(): void
4748
]);
4849
}
4950

51+
public function test_the_history_can_be_encrypted_via_middleware(): void
52+
{
53+
Route::middleware([StartSession::class, ExampleMiddleware::class, EncryptHistoryMiddleware::class])->get('/', function () {
54+
return Inertia::render('User/Edit');
55+
});
56+
57+
$response = $this->withoutExceptionHandling()->get('/', [
58+
'X-Inertia' => 'true',
59+
]);
60+
61+
$response->assertSuccessful();
62+
$response->assertJson([
63+
'component' => 'User/Edit',
64+
'encryptHistory' => true,
65+
]);
66+
}
67+
68+
public function test_the_history_can_be_encrypted_via_middleware_alias(): void
69+
{
70+
Route::middleware([StartSession::class, ExampleMiddleware::class, 'inertia.encrypt'])->get('/', function () {
71+
return Inertia::render('User/Edit');
72+
});
73+
74+
$response = $this->withoutExceptionHandling()->get('/', [
75+
'X-Inertia' => 'true',
76+
]);
77+
78+
$response->assertSuccessful();
79+
$response->assertJson([
80+
'component' => 'User/Edit',
81+
'encryptHistory' => true,
82+
]);
83+
}
84+
5085
public function test_the_history_can_be_encrypted_globally(): void
5186
{
5287
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {

0 commit comments

Comments
 (0)