Skip to content

Commit 289222e

Browse files
committed
encrypt/clear history
1 parent 931130e commit 289222e

File tree

2 files changed

+110
-7
lines changed

2 files changed

+110
-7
lines changed

src/ResponseFactory.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ class ResponseFactory
2929

3030
protected $clearHistory = false;
3131

32-
protected $encryptHistory = false;
33-
34-
public function __construct()
35-
{
36-
$this->encryptHistory = config('inertia.history.encrypt', false);
37-
}
32+
protected $encryptHistory;
3833

3934
public function setRootView(string $name): void
4035
{
@@ -150,7 +145,7 @@ public function render(string $component, $props = []): Response
150145
$this->rootView,
151146
$this->getVersion(),
152147
$this->clearHistory,
153-
$this->encryptHistory
148+
$this->encryptHistory ?? config('inertia.history.encrypt', false),
154149
);
155150
}
156151

tests/HistoryTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Illuminate\Session\Middleware\StartSession;
6+
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\Facades\Route;
8+
use Inertia\Inertia;
9+
use Inertia\Tests\Stubs\ExampleMiddleware;
10+
11+
class HistoryTest extends TestCase
12+
{
13+
public function test_the_history_is_not_encrypted_or_cleared_by_default(): void
14+
{
15+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
16+
return Inertia::render('User/Edit');
17+
});
18+
19+
$response = $this->withoutExceptionHandling()->get('/', [
20+
'X-Inertia' => 'true',
21+
]);
22+
23+
$response->assertSuccessful();
24+
$response->assertJson([
25+
'component' => 'User/Edit',
26+
'encryptHistory' => false,
27+
'clearHistory' => false,
28+
]);
29+
}
30+
31+
public function test_the_history_can_be_encrypted(): void
32+
{
33+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
34+
Inertia::encryptHistory();
35+
36+
return Inertia::render('User/Edit');
37+
});
38+
39+
$response = $this->withoutExceptionHandling()->get('/', [
40+
'X-Inertia' => 'true',
41+
]);
42+
43+
$response->assertSuccessful();
44+
$response->assertJson([
45+
'component' => 'User/Edit',
46+
'encryptHistory' => true,
47+
]);
48+
}
49+
50+
public function test_the_history_can_be_encrypted_globally(): void
51+
{
52+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
53+
Config::set('inertia.history.encrypt', true);
54+
55+
return Inertia::render('User/Edit');
56+
});
57+
58+
$response = $this->withoutExceptionHandling()->get('/', [
59+
'X-Inertia' => 'true',
60+
]);
61+
62+
$response->assertSuccessful();
63+
$response->assertJson([
64+
'component' => 'User/Edit',
65+
'encryptHistory' => true,
66+
]);
67+
}
68+
69+
public function test_the_history_can_be_encrypted_globally_and_overridden(): void
70+
{
71+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
72+
Config::set('inertia.history.encrypt', true);
73+
74+
Inertia::encryptHistory(false);
75+
76+
return Inertia::render('User/Edit');
77+
});
78+
79+
$response = $this->withoutExceptionHandling()->get('/', [
80+
'X-Inertia' => 'true',
81+
]);
82+
83+
$response->assertSuccessful();
84+
$response->assertJson([
85+
'component' => 'User/Edit',
86+
'encryptHistory' => false,
87+
]);
88+
}
89+
90+
public function test_the_history_can_be_cleared(): void
91+
{
92+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
93+
Inertia::clearHistory();
94+
95+
return Inertia::render('User/Edit');
96+
});
97+
98+
$response = $this->withoutExceptionHandling()->get('/', [
99+
'X-Inertia' => 'true',
100+
]);
101+
102+
$response->assertSuccessful();
103+
$response->assertJson([
104+
'component' => 'User/Edit',
105+
'clearHistory' => true,
106+
]);
107+
}
108+
}

0 commit comments

Comments
 (0)