Skip to content

Commit

Permalink
make sure there is always an authority when following redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Mar 1, 2024
1 parent a59255b commit af5d7e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/FollowRedirections.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Response\StatusCode,
Header\Location,
};
use Innmind\Url\{
Url,
Authority,
};
use Innmind\Immutable\{
Either,
Sequence,
Expand Down Expand Up @@ -93,7 +97,7 @@ private function redirect(Redirection $redirection): Either
->find(Location::class)
->map(static fn($header) => $header->url())
->map(static fn($url) => Request::of(
$url,
self::resolveUrl($redirection->request()->url(), $url),
$redirection->request()->method(),
$redirection->request()->protocolVersion(),
$redirection->request()->headers(),
Expand Down Expand Up @@ -122,7 +126,7 @@ private function seeOther(Redirection $redirection): Either
->find(Location::class)
->map(static fn($header) => $header->url())
->map(static fn($url) => Request::of(
$url,
self::resolveUrl($redirection->request()->url(), $url),
Method::get,
$redirection->request()->protocolVersion(),
$redirection->request()->headers(),
Expand All @@ -132,4 +136,17 @@ private function seeOther(Redirection $redirection): Either
static fn() => Either::left($redirection),
);
}

private static function resolveUrl(Url $request, Url $location): Url
{
if ($location->authority()->equals(Authority::none())) {
$location = $location
->withScheme($request->scheme())
->withAuthority($request->authority());
}

return $location->withPath(
$request->path()->resolve($location->path()),
);
}
}
25 changes: 21 additions & 4 deletions tests/FollowRedirectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ public function testRedirectMaximum5Times()
$inner
->expects($matcher = $this->exactly(6))
->method('__invoke')
->willReturnCallback(function($request) use ($matcher, $start, $newUrl, $expected) {
->willReturnCallback(function($request) use ($matcher, $start, $firstUrl, $expected) {
match ($matcher->numberOfInvocations()) {
1 => $this->assertSame($start, $request),
default => $this->assertSame($newUrl, $request->url()),
// assertions on the new url are done in self::testRedirect() and self::testRedirectSeeOther()
default => $this->assertNotSame($firstUrl, $request->url()),
};

return $expected;
Expand Down Expand Up @@ -251,7 +252,15 @@ public function testRedirectSeeOther()
$this->assertSame($start, $request);
} else {
$this->assertSame(Method::get, $request->method());
$this->assertSame($newUrl, $request->url());
$this->assertFalse($request->url()->authority()->equals(Authority::none()));
$this->assertTrue($request->url()->path()->absolute());
// not a direct comparison as new url might be a relative path
$this->assertStringEndsWith(
$newUrl->path()->toString(),
$request->url()->path()->toString(),
);
$this->assertSame($newUrl->query(), $request->url()->query());
$this->assertSame($newUrl->fragment(), $request->url()->fragment());
$this->assertSame($start->headers(), $request->headers());
$this->assertSame('', $request->body()->toString());
}
Expand Down Expand Up @@ -322,7 +331,15 @@ public function testRedirect()
$this->assertSame($start, $request);
} else {
$this->assertSame($start->method(), $request->method());
$this->assertSame($newUrl, $request->url());
$this->assertFalse($request->url()->authority()->equals(Authority::none()));
$this->assertTrue($request->url()->path()->absolute());
// not a direct comparison as new url might be a relative path
$this->assertStringEndsWith(
$newUrl->path()->toString(),
$request->url()->path()->toString(),
);
$this->assertSame($newUrl->query(), $request->url()->query());
$this->assertSame($newUrl->fragment(), $request->url()->fragment());
$this->assertSame($start->headers(), $request->headers());
$this->assertSame($start->body(), $request->body());
}
Expand Down

0 comments on commit af5d7e3

Please sign in to comment.