Skip to content

Commit f3470df

Browse files
[8.x] Added assertRedirectToSignedRoute() method for testing responses (#38349)
* Added assertRedirectToSignedRoute() method for testing responses. * Updated method to handle when there's no extra query params. * Updated method to accept name and parameters rather than a URI. * Style updates. * Update TestResponse.php * Fixed failing tests. Co-authored-by: Taylor Otwell <[email protected]>
1 parent 848a898 commit f3470df

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Cookie\CookieValuePrefix;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Http\RedirectResponse;
11+
use Illuminate\Http\Request;
1112
use Illuminate\Support\Arr;
1213
use Illuminate\Support\Carbon;
1314
use Illuminate\Support\Collection;
@@ -266,6 +267,43 @@ public function assertRedirect($uri = null)
266267
return $this;
267268
}
268269

270+
/**
271+
* Assert whether the response is redirecting to a given signed route.
272+
*
273+
* @param string|null $name
274+
* @param mixed $parameters
275+
* @return $this
276+
*/
277+
public function assertRedirectToSignedRoute($name = null, $parameters = [])
278+
{
279+
if (! is_null($name)) {
280+
$uri = route($name, $parameters);
281+
}
282+
283+
PHPUnit::assertTrue(
284+
$this->isRedirect(), 'Response status code ['.$this->getStatusCode().'] is not a redirect status code.'
285+
);
286+
287+
$request = Request::create($this->headers->get('Location'));
288+
289+
PHPUnit::assertTrue(
290+
$request->hasValidSignature(), 'The response is not a redirect to a signed route.'
291+
);
292+
293+
if (! is_null($name)) {
294+
$expectedUri = rtrim($request->fullUrlWithQuery([
295+
'signature' => null,
296+
'expires' => null,
297+
]), '?');
298+
299+
PHPUnit::assertEquals(
300+
app('url')->to($uri), $expectedUri
301+
);
302+
}
303+
304+
return $this;
305+
}
306+
269307
/**
270308
* Asserts that the response contains the given header and equals the optional value.
271309
*
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Testing;
4+
5+
use Illuminate\Contracts\Routing\Registrar;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Routing\UrlGenerator;
8+
use Illuminate\Support\Facades\Facade;
9+
use Orchestra\Testbench\TestCase;
10+
11+
class AssertRedirectToSignedRouteTest extends TestCase
12+
{
13+
/**
14+
* @var \Illuminate\Contracts\Routing\Registrar
15+
*/
16+
private $router;
17+
18+
/**
19+
* @var \Illuminate\Routing\UrlGenerator
20+
*/
21+
private $urlGenerator;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$this->router = $this->app->make(Registrar::class);
28+
29+
$this->router
30+
->get('signed-route')
31+
->name('signed-route');
32+
33+
$this->router
34+
->get('signed-route-with-param/{param}')
35+
->name('signed-route-with-param');
36+
37+
$this->urlGenerator = $this->app->make(UrlGenerator::class);
38+
}
39+
40+
public function testAssertRedirectToSignedRouteWithoutRouteName()
41+
{
42+
$this->router->get('test-route', function () {
43+
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route'));
44+
});
45+
46+
$this->get('test-route')
47+
->assertRedirectToSignedRoute();
48+
}
49+
50+
public function testAssertRedirectToSignedRouteWithRouteName()
51+
{
52+
$this->router->get('test-route', function () {
53+
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route'));
54+
});
55+
56+
$this->get('test-route')
57+
->assertRedirectToSignedRoute('signed-route');
58+
}
59+
60+
public function testAssertRedirectToSignedRouteWithRouteNameAndParams()
61+
{
62+
$this->router->get('test-route', function () {
63+
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route-with-param', 'hello'));
64+
});
65+
66+
$this->router->get('test-route-with-extra-param', function () {
67+
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route-with-param', [
68+
'param' => 'foo',
69+
'extra' => 'another',
70+
]));
71+
});
72+
73+
$this->get('test-route')
74+
->assertRedirectToSignedRoute('signed-route-with-param', 'hello');
75+
76+
$this->get('test-route-with-extra-param')
77+
->assertRedirectToSignedRoute('signed-route-with-param', [
78+
'param' => 'foo',
79+
'extra' => 'another',
80+
]);
81+
}
82+
83+
public function testAssertRedirectToSignedRouteWithRouteNameToTemporarySignedRoute()
84+
{
85+
$this->router->get('test-route', function () {
86+
return new RedirectResponse($this->urlGenerator->temporarySignedRoute('signed-route', 60));
87+
});
88+
89+
$this->get('test-route')
90+
->assertRedirectToSignedRoute('signed-route');
91+
}
92+
93+
public function tearDown(): void
94+
{
95+
parent::tearDown();
96+
97+
Facade::setFacadeApplication(null);
98+
}
99+
}

0 commit comments

Comments
 (0)