Browse Source

More client redirection tests

master
J. King 4 years ago
parent
commit
104e0068a8
  1. 3
      lib/HttpClient/HttpClient.php
  2. 28
      tests/cases/HttpClientTest.php

3
lib/HttpClient/HttpClient.php

@ -53,7 +53,8 @@ class HttpClient implements RequestFactoryInterface, ClientInterface {
if ($url = Url::fromString($loc[$b], (string) $request->getUri()->withUserInfo("", ""))) { if ($url = Url::fromString($loc[$b], (string) $request->getUri()->withUserInfo("", ""))) {
$request = $request->withUri($url); $request = $request->withUri($url);
if ($code === 303 && !in_array($request->getMethod(), ["GET", "HEAD"])) { 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; continue 2;
} }

28
tests/cases/HttpClientTest.php

@ -77,11 +77,13 @@ class ParserTest extends \PHPUnit\Framework\TestCase {
\Phake::when($client)->sendRequest->thenReturn($res); \Phake::when($client)->sendRequest->thenReturn($res);
\Phake::when($factory)->createRequest->thenReturn($req); \Phake::when($factory)->createRequest->thenReturn($req);
\Phake::when($req)->withUri->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; $mockCode = \Phake::when($res)->getStatusCode;
$mockLoc = \Phake::when($res)->getHeader("Location"); $mockLoc = \Phake::when($res)->getHeader("Location");
$mockUrl = \Phake::when($req)->getUri; $mockUrl = \Phake::when($req)->getUri;
foreach ($responses as $url => [$code, $loc]) { foreach ($responses as $url => [$code, $loc]) {
$mockUrl->thenReturn(new Url($url)); $mockUrl = $mockUrl->thenReturn(new Url($url));
$mockCode = $mockCode->thenReturn($code); $mockCode = $mockCode->thenReturn($code);
$mockLoc = $mockLoc->thenReturn((array) $loc); $mockLoc = $mockLoc->thenReturn((array) $loc);
} }
@ -94,6 +96,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase {
} }
} finally { } finally {
\Phake::verify($client, \Phake::times(min(sizeof($responses), $max + 1)))->sendRequest($this->identicalTo($req)); \Phake::verify($client, \Phake::times(min(sizeof($responses), $max + 1)))->sendRequest($this->identicalTo($req));
\Phake::verify($req, \Phake::times(0))->withMethod;
$redir = 0; $redir = 0;
$checks = []; $checks = [];
foreach ($responses as $url => [$code, $loc]) { foreach ($responses as $url => [$code, $loc]) {
@ -111,15 +114,34 @@ class ParserTest extends \PHPUnit\Framework\TestCase {
} }
public function provideRedirections(): iterable { public function provideRedirections(): iterable {
$err = new Exception("tooManyRedirects");
return [ return [
[[ [[
'http://example.com/' => [200, null], 'http://example.com/' => [200, null],
], 0, null], ], 0, null],
[[
'http://example.com/' => [302, null],
], 0, null],
[[ [[
'http://example.com/' => [302, "/index.html"], 'http://example.com/' => [302, "/index.html"],
'http://example.com/index.html' => [200, null], '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],
]; ];
} }
} }
Loading…
Cancel
Save