@@ -204,6 +204,39 @@ public function testCsrfVerifyJsonReturnsSelfOnMatch(): void
204204 $ this ->assertSame ('{"foo":"bar"} ' , $ request ->getBody ());
205205 }
206206
207+ public function testCsrfVerifyHeaderWithJsonBodyPreservesBody (): void
208+ {
209+ service ('superglobals ' )
210+ ->setServer ('REQUEST_METHOD ' , 'POST ' )
211+ ->setCookie ('csrf_cookie_name ' , self ::CORRECT_CSRF_HASH );
212+
213+ $ security = $ this ->createMockSecurity ();
214+ $ request = $ this ->createIncomingRequest ();
215+ $ body = '{"foo":"bar"} ' ;
216+
217+ $ request ->setHeader ('X-CSRF-TOKEN ' , self ::CORRECT_CSRF_HASH );
218+ $ request ->setBody ($ body );
219+
220+ $ this ->assertInstanceOf (Security::class, $ security ->verify ($ request ));
221+ $ this ->assertSame ($ body , $ request ->getBody ());
222+ }
223+
224+ public function testCsrfVerifyHeaderWithJsonBodyStripsTokenFromBody (): void
225+ {
226+ service ('superglobals ' )
227+ ->setServer ('REQUEST_METHOD ' , 'POST ' )
228+ ->setCookie ('csrf_cookie_name ' , self ::CORRECT_CSRF_HASH );
229+
230+ $ security = $ this ->createMockSecurity ();
231+ $ request = $ this ->createIncomingRequest ();
232+
233+ $ request ->setHeader ('X-CSRF-TOKEN ' , self ::CORRECT_CSRF_HASH );
234+ $ request ->setBody ('{"csrf_test_name":" ' . self ::CORRECT_CSRF_HASH . '","foo":"bar"} ' );
235+
236+ $ this ->assertInstanceOf (Security::class, $ security ->verify ($ request ));
237+ $ this ->assertSame ('{"foo":"bar"} ' , $ request ->getBody ());
238+ }
239+
207240 public function testCsrfVerifyPutBodyThrowsExceptionOnNoMatch (): void
208241 {
209242 service ('superglobals ' )
0 commit comments