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] When following redirects, terminate each test request in proper order #35604

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,12 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
$request = Request::createFromBase($symfonyRequest)
);

$kernel->terminate($request, $response);

if ($this->followRedirects) {
$response = $this->followRedirects($response);
}

$kernel->terminate($request, $response);

return $this->createTestResponse($response);
}

Expand Down Expand Up @@ -623,12 +623,12 @@ protected function prepareCookiesForJsonRequest()
*/
protected function followRedirects($response)
{
$this->followRedirects = false;

while ($response->isRedirect()) {
$response = $this->get($response->headers->get('Location'));
}

$this->followRedirects = false;

return $response;
}

Expand Down
60 changes: 60 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Illuminate\Tests\Foundation\Testing\Concerns;

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

class MakesHttpRequestsTest extends TestCase
Expand Down Expand Up @@ -114,6 +117,48 @@ public function testWithoutAndWithCredentials()
$this->defaultCookies = ['foo' => 'bar'];
$this->assertSame(['foo' => 'bar'], $this->prepareCookiesForJsonRequest());
}

public function testFollowingRedirects()
{
$router = $this->app->make(Registrar::class);
$url = $this->app->make(UrlGenerator::class);

$router->get('from', function () use ($url) {
return new RedirectResponse($url->to('to'));
});

$router->get('to', function () {
return 'OK';
});

$this->followingRedirects()
->get('from')
->assertOk()
->assertSee('OK');
}

public function testFollowingRedirectsTerminatesInExpectedOrder()
{
$router = $this->app->make(Registrar::class);
$url = $this->app->make(UrlGenerator::class);

$callOrder = [];
TerminatingMiddleware::$callback = function ($request) use (&$callOrder) {
$callOrder[] = $request->path();
};

$router->get('from', function () use ($url) {
return new RedirectResponse($url->to('to'));
})->middleware(TerminatingMiddleware::class);

$router->get('to', function () {
return 'OK';
})->middleware(TerminatingMiddleware::class);

$this->followingRedirects()->get('from');

$this->assertEquals(['from', 'to'], $callOrder);
}
}

class MyMiddleware
Expand All @@ -123,3 +168,18 @@ public function handle($request, $next)
return $next($request.'WithMiddleware');
}
}

class TerminatingMiddleware
{
public static $callback;

public function handle($request, $next)
{
return $next($request);
}

public function terminate($request, $response)
{
call_user_func(static::$callback, $request, $response);
}
}