diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index 6a14fb1d30fe..a2b2c8c675a9 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -297,7 +297,7 @@ public function disableCookieEncryption() } /** - * Set the referer header and previous URL session value in order to simulate a previous request. + * Set the referer header and previous URL session value from a given URL in order to simulate a previous request. * * @param string $url * @return $this @@ -309,6 +309,18 @@ public function from(string $url) return $this->withHeader('referer', $url); } + /** + * Set the referer header and previous URL session value from a given route in order to simulate a previous request. + * + * @param string $name + * @param mixed $parameters + * @return $this + */ + public function fromRoute(string $name, $parameters = []) + { + return $this->from($this->app['url']->route($name, $parameters)); + } + /** * Set the Precognition header to "true". * diff --git a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php index 0cbf5d417301..e848cf378a61 100644 --- a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php +++ b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php @@ -18,6 +18,18 @@ public function testFromSetsHeaderAndSession() $this->assertSame('previous/url', $this->app['session']->previousUrl()); } + public function testFromRouteSetsHeaderAndSession() + { + $router = $this->app->make(Registrar::class); + + $router->get('previous/url', fn () => 'ok')->name('previous-url'); + + $this->fromRoute('previous-url'); + + $this->assertSame('http://localhost/previous/url', $this->defaultHeaders['referer']); + $this->assertSame('http://localhost/previous/url', $this->app['session']->previousUrl()); + } + public function testWithTokenSetsAuthorizationHeader() { $this->withToken('foobar');