Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Added assertRedirectToSignedRoute() method for testing responses #38349

Merged
merged 7 commits into from
Aug 13, 2021
Merged
38 changes: 38 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -266,6 +267,43 @@ public function assertRedirect($uri = null)
return $this;
}

/**
* Assert whether the response is redirecting to a given signed route.
*
* @param string|null $name
* @param mixed $parameters
* @return $this
*/
public function assertRedirectToSignedRoute($name = null, $parameters = [])
{
if (! is_null($name)) {
$uri = route($name, $parameters);
}

PHPUnit::assertTrue(
$this->isRedirect(), 'Response status code ['.$this->getStatusCode().'] is not a redirect status code.'
);

$request = Request::create($this->headers->get('Location'));

PHPUnit::assertTrue(
$request->hasValidSignature(), 'The response is not a redirect to a signed route.'
);

if (! is_null($name)) {
$expectedUri = rtrim($request->fullUrlWithQuery([
'signature' => null,
'expires' => null,
]), '?');

PHPUnit::assertEquals(
app('url')->to($uri), $expectedUri
);
}

return $this;
}

/**
* Asserts that the response contains the given header and equals the optional value.
*
Expand Down
99 changes: 99 additions & 0 deletions tests/Testing/AssertRedirectToSignedRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Illuminate\Tests\Testing;

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Facade;
use Orchestra\Testbench\TestCase;

class AssertRedirectToSignedRouteTest extends TestCase
{
/**
* @var \Illuminate\Contracts\Routing\Registrar
*/
private $router;

/**
* @var \Illuminate\Routing\UrlGenerator
*/
private $urlGenerator;

protected function setUp(): void
{
parent::setUp();

$this->router = $this->app->make(Registrar::class);

$this->router
->get('signed-route')
->name('signed-route');

$this->router
->get('signed-route-with-param/{param}')
->name('signed-route-with-param');

$this->urlGenerator = $this->app->make(UrlGenerator::class);
}

public function testAssertRedirectToSignedRouteWithoutRouteName()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route'));
});

$this->get('test-route')
->assertRedirectToSignedRoute();
}

public function testAssertRedirectToSignedRouteWithRouteName()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route'));
});

$this->get('test-route')
->assertRedirectToSignedRoute('signed-route');
}

public function testAssertRedirectToSignedRouteWithRouteNameAndParams()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route-with-param', 'hello'));
});

$this->router->get('test-route-with-extra-param', function () {
return new RedirectResponse($this->urlGenerator->signedRoute('signed-route-with-param', [
'param' => 'foo',
'extra' => 'another',
]));
});

$this->get('test-route')
->assertRedirectToSignedRoute('signed-route-with-param', 'hello');

$this->get('test-route-with-extra-param')
->assertRedirectToSignedRoute('signed-route-with-param', [
'param' => 'foo',
'extra' => 'another',
]);
}

public function testAssertRedirectToSignedRouteWithRouteNameToTemporarySignedRoute()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->temporarySignedRoute('signed-route', 60));
});

$this->get('test-route')
->assertRedirectToSignedRoute('signed-route');
}

public function tearDown(): void
{
parent::tearDown();

Facade::setFacadeApplication(null);
}
}