From 104e0068a8af080f890b0518d79c411bd69599c5 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 28 Jun 2020 07:51:53 -0400 Subject: [PATCH] More client redirection tests --- lib/HttpClient/HttpClient.php | 3 ++- tests/cases/HttpClientTest.php | 28 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/HttpClient/HttpClient.php b/lib/HttpClient/HttpClient.php index e751de8..0d5a51e 100644 --- a/lib/HttpClient/HttpClient.php +++ b/lib/HttpClient/HttpClient.php @@ -53,7 +53,8 @@ class HttpClient implements RequestFactoryInterface, ClientInterface { if ($url = Url::fromString($loc[$b], (string) $request->getUri()->withUserInfo("", ""))) { $request = $request->withUri($url); if ($code === 303 && !in_array($request->getMethod(), ["GET", "HEAD"])) { - $request = $request->withMethod("GET"); + // this is merely for correctness; the client should not normally be using other methods + $request = $request->withMethod("GET"); // @codeCoverageIgnore } continue 2; } diff --git a/tests/cases/HttpClientTest.php b/tests/cases/HttpClientTest.php index e5b996c..399450b 100644 --- a/tests/cases/HttpClientTest.php +++ b/tests/cases/HttpClientTest.php @@ -77,11 +77,13 @@ class ParserTest extends \PHPUnit\Framework\TestCase { \Phake::when($client)->sendRequest->thenReturn($res); \Phake::when($factory)->createRequest->thenReturn($req); \Phake::when($req)->withUri->thenReturn($req); + \Phake::when($req)->withMethod->thenReturn($req); + \Phake::when($req)->getMethod->thenReturn("GET"); $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)); + $mockUrl = $mockUrl->thenReturn(new Url($url)); $mockCode = $mockCode->thenReturn($code); $mockLoc = $mockLoc->thenReturn((array) $loc); } @@ -94,6 +96,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase { } } finally { \Phake::verify($client, \Phake::times(min(sizeof($responses), $max + 1)))->sendRequest($this->identicalTo($req)); + \Phake::verify($req, \Phake::times(0))->withMethod; $redir = 0; $checks = []; foreach ($responses as $url => [$code, $loc]) { @@ -111,15 +114,34 @@ class ParserTest extends \PHPUnit\Framework\TestCase { } public function provideRedirections(): iterable { - $err = new Exception("tooManyRedirects"); return [ [[ 'http://example.com/' => [200, null], ], 0, null], + [[ + 'http://example.com/' => [302, null], + ], 0, null], [[ 'http://example.com/' => [302, "/index.html"], 'http://example.com/index.html' => [200, null], - ], 0, $err], + ], 0, new Exception("tooManyRedirects")], + [[ + 'http://example.com/' => [302, "/index.html"], + 'http://example.com/index.html' => [404, null], + ], 10, new Exception("httpStatus404")], + [[ + 'http://a:b@c/d/' => [300, "/"], + 'http://c/' => [200, null], + ], 10, null], + [[ + 'http://a/' => [301, ["http://[/", "/b/"]], + 'http://a/b/' => [200, null], + ], 10, null], + [[ + 'http://a/' => [301, ["http://[/", "/b/"]], + 'http://a/b/' => [303, "http://example.com/"], + 'http://example.com/' => [304, "//a:b@c"] + ], 10, null], ]; } } \ No newline at end of file