Skip to content

Commit

Permalink
Remove httpbin.org for redirect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jun 22, 2020
1 parent fa79253 commit 37f307c
Showing 1 changed file with 60 additions and 22 deletions.
82 changes: 60 additions & 22 deletions test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
use Amp\Http\Cookie\RequestCookie;
use Amp\Http\Cookie\ResponseCookie;
use Amp\Http\Rfc7230;
use Amp\Http\Server\Request as ServerRequest;
use Amp\Http\Server\RequestHandler\CallableRequestHandler;
use Amp\Http\Server\Response as ServerResponse;
use Amp\Http\Server\Server;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\Socket;
use Amp\Success;
use Psr\Log\NullLogger;
use function Amp\asyncCall;
use function Amp\call;
use function Amp\coroutine;
Expand All @@ -38,6 +43,18 @@ class ClientHttpBinIntegrationTest extends AsyncTestCase
private $builder;
/** @var callable */
private $responseCallback;
/** @var Server */
private $httpServer;

protected function tearDownAsync()
{
if (!$this->httpServer) {
return;
}

$this->httpServer->stop();
$this->httpServer = null;
}

public function testHttp10Response(): \Generator
{
Expand Down Expand Up @@ -215,7 +232,7 @@ public function testHeaderCase(): \Generator
['accept', '*/*'],
['user-agent', 'amphp/http-client @ v4.x'],
['Accept-Encoding', 'gzip, deflate, identity'],
['host', (string) $this->socket->getAddress()]
['host', (string) $this->socket->getAddress()],
], $result);
}

Expand Down Expand Up @@ -354,42 +371,34 @@ public function testReason(): \Generator

public function testRedirect(): \Generator
{
$statusCode = 299;
$redirectTo = "/status/{$statusCode}";
$uri = "http://httpbin.org/redirect-to?url=" . \rawurlencode($redirectTo);

$this->client = $this->builder->followRedirects(0)->build();

$this->givenServer(static function () {
return new ServerResponse(302, ['location' => 'https://example.org/']);
});

/** @var Response $response */
$response = yield $this->executeRequest(new Request($uri));
$response = yield $this->executeRequest($this->createRequest());

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(302, $response->getStatus());

$originalUri = $response->getOriginalRequest()->getUri();
$this->assertSame(302, $response->getStatus());
$this->assertSame($response, $response->getOriginalResponse());

$this->assertSame($uri, (string) $originalUri);
}

public function testRedirectWithFollow(): \Generator
{
$statusCode = 299;
$redirectTo = "/status/{$statusCode}";
$uri = "http://httpbin.org/redirect-to?url=" . \rawurlencode($redirectTo);

$this->client = $this->builder->followRedirects()->build();

$this->givenServer(static function () {
return new ServerResponse(302, ['location' => 'https://example.org/']);
});

/** @var Response $response */
$response = yield $this->executeRequest(new Request($uri));
$response = yield $this->executeRequest($this->createRequest());

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($statusCode, $response->getStatus());

$originalUri = $response->getOriginalRequest()->getUri();
$this->assertSame(200, $response->getStatus());
$this->assertSame(302, $response->getOriginalResponse()->getStatus());

$this->assertSame($uri, (string) $originalUri);
}

public function testClientAddsZeroContentLengthHeaderForEmptyBodiesOnPost(): \Generator
Expand Down Expand Up @@ -520,9 +529,21 @@ public function testInfiniteRedirect(): \Generator
{
$this->builder->followRedirects(10);

$request = $this->createRequest();
$request->setUri($request->getUri()->withPath('/redirect/11'));

$this->givenServer(static function (ServerRequest $request) {
\preg_match('(/redirect/(\d+))', $request->getUri()->getPath(), $matches);
if ($matches[1] ?? '') {
return new ServerResponse(302, ['location' => '/redirect/' . ($matches[1] - 1)]);
}

return new ServerResponse(200);
});

$this->expectException(TooManyRedirectsException::class);

yield $this->executeRequest(new Request("http://httpbin.org/redirect/11"));
yield $this->executeRequest($request);
}

public function testRequestCancellation(): \Generator
Expand Down Expand Up @@ -756,10 +777,21 @@ protected function setUp(): void
$this->socket->close();
}

if ($this->httpServer) {
$this->httpServer->stop();
$this->httpServer = null;
}

$this->socket = Socket\Server::listen('127.0.0.1:0');
$this->socket->unreference();

asyncCall(function () {
yield delay(0);

if ($this->httpServer) {
return;
}

/** @var Socket\EncryptableSocket $client */
$client = yield $this->socket->accept();
$client->unreference();
Expand All @@ -768,6 +800,12 @@ protected function setUp(): void
});
}

private function givenServer(callable $requestHandler): void
{
$this->httpServer = new Server([$this->socket], new CallableRequestHandler($requestHandler), new NullLogger);
$this->httpServer->start();
}

private function givenRawServerResponse(string $response): void
{
$this->responseCallback = coroutine(static function (Socket\Socket $socket) use ($response) {
Expand Down

0 comments on commit 37f307c

Please sign in to comment.