@@ -346,8 +346,35 @@ public function testResolveRequestReturns422ForJsonRequest(): void
346346 $ this ->assertSame (422 , $ response ->getStatusCode ());
347347 }
348348
349- public function testResolveRequestReturns422ForAjaxRequest (): void
349+ public function testResolveRequestReturns422WhenJsonIsPreferred (): void
350350 {
351+ service ('superglobals ' )->setServer ('HTTP_ACCEPT ' , 'application/json ' );
352+ $ formRequest = $ this ->makeFormRequest ($ this ->makeRequest ());
353+
354+ $ response = $ formRequest ->resolveRequest ();
355+
356+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
357+ $ this ->assertSame (422 , $ response ->getStatusCode ());
358+
359+ $ body = json_decode ($ response ->getBody (), true );
360+ $ this ->assertArrayHasKey ('errors ' , $ body );
361+ $ this ->assertArrayHasKey ('title ' , $ body ['errors ' ]);
362+ }
363+
364+ public function testResolveRequestRedirectsForAjaxRequest (): void
365+ {
366+ service ('superglobals ' )->setServer ('HTTP_X_REQUESTED_WITH ' , 'XMLHttpRequest ' );
367+ $ formRequest = $ this ->makeFormRequest ($ this ->makeRequest ());
368+
369+ $ response = $ formRequest ->resolveRequest ();
370+
371+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
372+ $ this ->assertSame (303 , $ response ->getStatusCode ());
373+ }
374+
375+ public function testResolveRequestReturns422ForAjaxRequestThatPrefersJson (): void
376+ {
377+ service ('superglobals ' )->setServer ('HTTP_ACCEPT ' , 'application/json ' );
351378 service ('superglobals ' )->setServer ('HTTP_X_REQUESTED_WITH ' , 'XMLHttpRequest ' );
352379 $ formRequest = $ this ->makeFormRequest ($ this ->makeRequest ());
353380
@@ -357,6 +384,17 @@ public function testResolveRequestReturns422ForAjaxRequest(): void
357384 $ this ->assertSame (422 , $ response ->getStatusCode ());
358385 }
359386
387+ public function testResolveRequestRedirectsForWildcardAcceptHeader (): void
388+ {
389+ service ('superglobals ' )->setServer ('HTTP_ACCEPT ' , '*/* ' );
390+ $ formRequest = $ this ->makeFormRequest ($ this ->makeRequest ());
391+
392+ $ response = $ formRequest ->resolveRequest ();
393+
394+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
395+ $ this ->assertSame (303 , $ response ->getStatusCode ());
396+ }
397+
360398 // -------------------------------------------------------------------------
361399 // Authorization failure
362400 // -------------------------------------------------------------------------
@@ -678,7 +716,7 @@ public function testInvalidFormRequestRedirectsWebRequest(): void
678716 }
679717
680718 // -------------------------------------------------------------------------
681- // Integration: validation failure - JSON / AJAX
719+ // Integration: validation failure - JSON
682720 // -------------------------------------------------------------------------
683721
684722 #[RunInSeparateProcess]
@@ -697,13 +735,28 @@ public function testInvalidFormRequestReturns422ForJsonRequest(): void
697735 }
698736
699737 #[RunInSeparateProcess]
700- public function testInvalidFormRequestReturns422ForAjaxRequest (): void
738+ public function testInvalidFormRequestReturns422WhenJsonIsPreferred (): void
701739 {
702- service ('superglobals ' )->setServer ('HTTP_X_REQUESTED_WITH ' , 'XMLHttpRequest ' );
740+ service ('superglobals ' )->setServer ('HTTP_ACCEPT ' , 'application/json ' );
703741
704742 $ response = $ this ->runRequest ('/posts ' , 'store ' , 'POST ' );
705743
706744 $ this ->assertSame (422 , $ response ->getStatusCode ());
745+
746+ $ body = json_decode ((string ) $ response ->getBody (), true );
747+ $ this ->assertArrayHasKey ('errors ' , $ body );
748+ $ this ->assertArrayHasKey ('title ' , $ body ['errors ' ]);
749+ }
750+
751+ #[RunInSeparateProcess]
752+ public function testInvalidFormRequestRedirectsForAjaxRequest (): void
753+ {
754+ service ('superglobals ' )->setServer ('HTTP_REFERER ' , 'http://example.com/posts/create ' );
755+ service ('superglobals ' )->setServer ('HTTP_X_REQUESTED_WITH ' , 'XMLHttpRequest ' );
756+
757+ $ response = $ this ->runRequest ('/posts ' , 'store ' , 'POST ' );
758+
759+ $ this ->assertSame (303 , $ response ->getStatusCode ());
707760 }
708761
709762 // -------------------------------------------------------------------------
0 commit comments