diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 3f6f59a36730..45d970959536 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -213,6 +213,23 @@ public function assertRedirectContains($uri) return $this; } + /** + * Assert whether the response is redirecting back to the previous location. + * + * @return $this + */ + public function assertRedirectBack() + { + PHPUnit::withResponse($this)->assertTrue( + $this->isRedirect(), + $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), + ); + + $this->assertLocation(app('url')->previous()); + + return $this; + } + /** * Assert whether the response is redirecting to a given route. * diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index cae497bdd133..32642d0df55c 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -2652,6 +2652,27 @@ public function testAssertRedirect() $response->assertRedirect(); } + public function testAssertRedirectBack() + { + app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1))); + + $store->setPreviousUrl('https://url.com'); + + app('url')->setSessionResolver(fn () => app('session.store')); + + $response = TestResponse::fromBaseResponse( + (new Response('', 302))->withHeaders(['Location' => 'https://url.com']) + ); + + $response->assertRedirectBack(); + + $this->expectException(ExpectationFailedException::class); + + $store->setPreviousUrl('https://url.net'); + + $response->assertRedirectBack(); + } + public function testGetDecryptedCookie() { $response = TestResponse::fromBaseResponse(