Skip to content

Commit

Permalink
[10.x] Add routeRoute method to test request (#49366)
Browse files Browse the repository at this point in the history
* feat: add routeRoute method to test request

* Update MakesHttpRequests.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
fragkp and taylorotwell authored Dec 13, 2023
1 parent 996375d commit eb94883
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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".
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit eb94883

Please sign in to comment.