From d20058c447b3393fe46df05ba3f4ed4264c410e1 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 27 Jun 2020 18:01:12 -0400 Subject: [PATCH] Initial test for redirection handling --- lib/HttpClient/HttpClient.php | 2 +- tests/cases/HttpClientTest.php | 67 +++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/lib/HttpClient/HttpClient.php b/lib/HttpClient/HttpClient.php index 6a10022..e751de8 100644 --- a/lib/HttpClient/HttpClient.php +++ b/lib/HttpClient/HttpClient.php @@ -54,8 +54,8 @@ class HttpClient implements RequestFactoryInterface, ClientInterface { $request = $request->withUri($url); if ($code === 303 && !in_array($request->getMethod(), ["GET", "HEAD"])) { $request = $request->withMethod("GET"); - continue 2; } + continue 2; } } return $response; diff --git a/tests/cases/HttpClientTest.php b/tests/cases/HttpClientTest.php index ea44b29..a0ec48c 100644 --- a/tests/cases/HttpClientTest.php +++ b/tests/cases/HttpClientTest.php @@ -6,14 +6,20 @@ declare(strict_types=1); namespace MensBeam\Lax\TestCase; +use MensBeam\Lax\Url; +use MensBeam\Lax\HttpClient\HttpClient; use MensBeam\Lax\HttpClient\Exception; +use \Psr\Http\Message\RequestInterface; +use \Psr\Http\Message\ResponseInterface; +use \Psr\Http\Message\RequestFactoryInterface; +use \Psr\Http\Client\ClientInterface; -/** - * @covers MensBeam\Lax\HttpClient\HttpClient - * @covers MensBeam\Lax\HttpClient\Exception - */ +/** @covers MensBeam\Lax\HttpClient\HttpClient */ class ParserTest extends \PHPUnit\Framework\TestCase { - /** @dataProvider provideCodes */ + /** + * @dataProvider provideCodes + * @covers MensBeam\Lax\HttpClient\Exception + */ public function testCreateExceptions(string $symbol, int $exp): void { $exp = new Exception("httpStatus".$exp); $act = new Exception($symbol); @@ -58,4 +64,55 @@ class ParserTest extends \PHPUnit\Framework\TestCase { } yield "httpStatus000401" => ["httpStatus000401", 401]; } + + /** @dataProvider provideRedirections */ + public function testHandleRedirections(array $responses, int $max, ?\Throwable $exc): void { + assert(sizeof($responses) > 0, "Test must have at least one response"); + $client = \Phake::mock(ClientInterface::class); + $factory = \Phake::mock(RequestFactoryInterface::class); + $req = \Phake::mock(RequestInterface::class); + $res = \Phake::mock(ResponseInterface::class); + $c = new HttpClient($client, $factory); + $c->maxRedirects = $max; + \Phake::when($client)->sendRequest->thenReturn($res); + \Phake::when($factory)->createRequest->thenReturn($req); + \Phake::when($req)->withUri->thenReturn($req); + $mockCode = \Phake::when($res)->getStatusCode; + $mockLoc = \Phake::when($res)->getHeader("Location"); + $mockUrl = \Phake::when($req)->getUri; + foreach ($responses as $url => [$code, $loc]) { + $mockUrl->thenReturn(new Url($url)); + $mockCode = $mockCode->thenReturn($code); + $mockLoc = $mockLoc->thenReturn((array) $loc); + } + try { + if ($exc) { + $this->expectExceptionObject($exc); + $c->sendRequest($req); + } else { + $this->assertSame($res, $c->sendRequest($req)); + } + } finally { + $redir = -1; + foreach ($responses as $url => [$code, $loc]) { + if ($redir++ >= $max) { + break; + } + \Phake::verify($client)->sendRequest($this->identicalTo($req)); + } + } + } + + public function provideRedirections(): iterable { + $err = new Exception("tooManyRedirects"); + return [ + [[ + 'http://example.com/' => [200, null], + ], 0, null], + [[ + 'http://example.com/' => [302, "/index.html"], + 'http://example.com/index.html' => [200, null], + ], 0, $err], + ]; + } } \ No newline at end of file