File tree Expand file tree Collapse file tree 4 files changed +45
-31
lines changed
packages/http/src/Session Expand file tree Collapse file tree 4 files changed +45
-31
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Tempest \Http \Session \Session ;
4+
5+ use function Tempest \get ;
6+
7+ $ name = Session::CSRF_TOKEN_KEY ;
8+ $ token = get (Session::class)->token ;
9+ ?>
10+
11+ <input type="hidden" name="{{ $name }}" value="{{ $token }}" />
Original file line number Diff line number Diff line change @@ -219,4 +219,14 @@ protected function skipWindows(string $reason): void
219219
220220 $ this ->markTestSkipped ($ reason );
221221 }
222+
223+ /**
224+ * @template TClassName of object
225+ * @param class-string<TClassName> $className
226+ * @return null|TClassName
227+ */
228+ protected function get (string $ className ): ?object
229+ {
230+ return $ this ->container ->get ($ className );
231+ }
222232}
Original file line number Diff line number Diff line change 1111use Tempest \Http \Session \CsrfTokenDidNotMatch ;
1212use Tempest \Http \Session \Session ;
1313use Tempest \Http \Session \VerifyCsrfMiddleware ;
14+ use Tempest \View \ViewCache ;
1415use Tests \Tempest \Integration \FrameworkIntegrationTestCase ;
1516
1617final class CsrfTest extends FrameworkIntegrationTestCase
@@ -97,13 +98,34 @@ public function test_matches_from_header(): void
9798
9899 public function test_csrf_component (): void
99100 {
101+ $ session = $ this ->container ->get (Session::class);
102+ $ session ->set (Session::CSRF_TOKEN_KEY , 'test ' );
103+
100104 $ rendered = $ this ->render (<<<HTML
101105 <x-csrf-token />
102106 HTML );
103107
108+ $ this ->assertSame (
109+ '<input type="hidden" name="_csrf_token" value="test"> ' ,
110+ $ rendered ,
111+ );
112+ }
113+
114+ public function test_csrf_with_cached_view (): void
115+ {
116+ $ this ->get (ViewCache::class)->enabled = true ;
117+
118+ $ oldVersion = $ this ->render (<<<HTML
119+ <x-csrf-token />
120+ HTML );
121+
104122 $ session = $ this ->container ->get (Session::class);
123+ $ session ->destroy ();
124+
125+ $ newVersion = $ this ->render (<<<HTML
126+ <x-csrf-token />
127+ HTML );
105128
106- $ this ->assertStringMatchesFormat ('<input type="hidden" name="_csrf_token" value="%s"> ' , $ rendered );
107- $ this ->assertStringContainsString ($ session ->token , $ rendered );
129+ $ this ->assertNotSame ($ oldVersion , $ newVersion );
108130 }
109131}
You can’t perform that action at this time.
0 commit comments