Skip to content

Commit

Permalink
[8.x] When following redirects, terminate each test request in proper…
Browse files Browse the repository at this point in the history
… order (#35604)

* Terminate each test request in proper order

* StyleCI

* StyleCI
  • Loading branch information
inxilpro committed Dec 15, 2020
1 parent d6d9b90 commit 2fb0f00
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
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
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);
}
}

0 comments on commit 2fb0f00

Please sign in to comment.