forked from alexdebril/feed-io
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Handle HTTP redirection responses in Client.php #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c51de5f
Handle HTTP redirection responses in Client.php
infabo 2eea4a5
Implement HTTP redirect handling with loop protection and relative UR…
Grotax c7fbe00
Apply suggestions from code review
Grotax a8f7ec0
Enhance redirect handling: preserve HEAD method for 303 responses and…
Grotax abf5dc4
Normalize URL paths in redirect handling: resolve . and .. segments i…
Grotax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| <?php | ||
Grotax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace FeedIo\Adapter\Http; | ||
|
|
||
| use DateTime; | ||
| use FeedIo\Adapter\NotFoundException; | ||
| use FeedIo\Adapter\ServerErrorException; | ||
| use Nyholm\Psr7\Response as PsrResponse; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Http\Client\ClientInterface as PsrClientInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
|
|
||
| class ClientTest extends TestCase | ||
| { | ||
| private PsrClientInterface $psrClient; | ||
| private Client $client; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->psrClient = $this->createMock(PsrClientInterface::class); | ||
| $this->client = new Client($this->psrClient); | ||
| } | ||
|
|
||
| public function testGetResponseWithSuccess(): void | ||
| { | ||
| $psrResponse = new PsrResponse(200, [], 'feed content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($psrResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/feed.xml'); | ||
|
|
||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| $this->assertEquals('feed content', $response->getBody()); | ||
| } | ||
|
|
||
| public function testGetResponseWith304NotModified(): void | ||
| { | ||
| $modifiedSince = new DateTime('2025-01-01'); | ||
|
|
||
| // HEAD request returns 304 | ||
| $headResponse = new PsrResponse(304); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->with($this->callback(function (RequestInterface $request) use ($modifiedSince) { | ||
| return $request->getMethod() === 'HEAD' | ||
| && $request->hasHeader('If-Modified-Since') | ||
| && $request->getHeaderLine('If-Modified-Since') === $modifiedSince->format(DateTime::RFC2822); | ||
| })) | ||
| ->willReturn($headResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/feed.xml', $modifiedSince); | ||
|
|
||
| $this->assertEquals(304, $response->getStatusCode()); | ||
| } | ||
|
|
||
| public function testGetResponseWithModifiedSinceButFeedChanged(): void | ||
| { | ||
| $modifiedSince = new DateTime('2025-01-01'); | ||
|
|
||
| // HEAD request returns 200 (modified) | ||
| $headResponse = new PsrResponse(200); | ||
| // GET request also returns 200 with content | ||
| $getResponse = new PsrResponse(200, [], 'new feed content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->exactly(2)) | ||
| ->method('sendRequest') | ||
| ->willReturnOnConsecutiveCalls($headResponse, $getResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/feed.xml', $modifiedSince); | ||
|
|
||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| $this->assertEquals('new feed content', $response->getBody()); | ||
| } | ||
|
|
||
| public function testGetResponseThrowsNotFoundOn404(): void | ||
| { | ||
| $psrResponse = new PsrResponse(404); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($psrResponse); | ||
|
|
||
| $this->expectException(NotFoundException::class); | ||
| $this->expectExceptionMessage('not found'); | ||
|
|
||
| $this->client->getResponse('https://example.com/feed.xml'); | ||
| } | ||
|
|
||
| public function testGetResponseThrowsServerErrorOnServerError(): void | ||
| { | ||
| $psrResponse = new PsrResponse(500); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($psrResponse); | ||
|
|
||
| $this->expectException(ServerErrorException::class); | ||
|
|
||
| $this->client->getResponse('https://example.com/feed.xml'); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider redirectStatusCodeProvider | ||
| */ | ||
| public function testGetResponseFollowsRedirects(int $statusCode): void | ||
| { | ||
| $redirectResponse = new PsrResponse($statusCode, ['Location' => 'https://example.com/new-feed.xml']); | ||
| $finalResponse = new PsrResponse(200, [], 'redirected feed content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->exactly(2)) | ||
| ->method('sendRequest') | ||
| ->willReturnOnConsecutiveCalls($redirectResponse, $finalResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/old-feed.xml'); | ||
|
|
||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| $this->assertEquals('redirected feed content', $response->getBody()); | ||
| } | ||
|
|
||
| public static function redirectStatusCodeProvider(): array | ||
| { | ||
| return [ | ||
| '301 Moved Permanently' => [301], | ||
| '302 Found' => [302], | ||
| '303 See Other' => [303], | ||
| '307 Temporary Redirect' => [307], | ||
| '308 Permanent Redirect' => [308], | ||
| ]; | ||
| } | ||
|
|
||
| public function testGetResponseFollowsMultipleRedirects(): void | ||
| { | ||
| $redirect1 = new PsrResponse(301, ['Location' => 'https://example.com/redirect2.xml']); | ||
| $redirect2 = new PsrResponse(302, ['Location' => 'https://example.com/final.xml']); | ||
| $finalResponse = new PsrResponse(200, [], 'final content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->exactly(3)) | ||
| ->method('sendRequest') | ||
| ->willReturnOnConsecutiveCalls($redirect1, $redirect2, $finalResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/start.xml'); | ||
|
|
||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| $this->assertEquals('final content', $response->getBody()); | ||
| } | ||
|
|
||
| public function testGetResponsePreservesModifiedSinceThroughRedirects(): void | ||
| { | ||
| $modifiedSince = new DateTime('2025-01-01'); | ||
|
|
||
| // HEAD request with If-Modified-Since returns redirect | ||
| $headRedirect = new PsrResponse(301, ['Location' => 'https://example.com/new-location.xml']); | ||
| // HEAD request to new location returns 200 | ||
| $headResponse = new PsrResponse(200); | ||
| // GET request to new location | ||
| $getResponse = new PsrResponse(200, [], 'content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->exactly(3)) | ||
| ->method('sendRequest') | ||
| ->willReturnOnConsecutiveCalls($headRedirect, $headResponse, $getResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/old-location.xml', $modifiedSince); | ||
|
|
||
| $this->assertEquals(200, $response->getStatusCode()); | ||
| } | ||
|
|
||
| public function testGetResponseWithEmptyLocationHeaderThrowsException(): void | ||
| { | ||
| $redirectResponse = new PsrResponse(301, ['Location' => '']); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($redirectResponse); | ||
|
|
||
| $this->expectException(ServerErrorException::class); | ||
|
|
||
| $this->client->getResponse('https://example.com/feed.xml'); | ||
| } | ||
|
|
||
| public function testResponseDurationIsTracked(): void | ||
| { | ||
| $psrResponse = new PsrResponse(200, [], 'content'); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($psrResponse); | ||
|
|
||
| $response = $this->client->getResponse('https://example.com/feed.xml'); | ||
|
|
||
| $this->assertIsFloat($response->getDuration()); | ||
| $this->assertGreaterThanOrEqual(0, $response->getDuration()); | ||
| } | ||
|
|
||
| public function testResponseDurationIsTrackedOnError(): void | ||
| { | ||
| $psrResponse = new PsrResponse(404); | ||
|
|
||
| $this->psrClient | ||
| ->expects($this->once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($psrResponse); | ||
|
|
||
| try { | ||
| $this->client->getResponse('https://example.com/feed.xml'); | ||
| $this->fail('Expected NotFoundException to be thrown'); | ||
| } catch (NotFoundException $e) { | ||
| $this->assertIsFloat($e->getDuration()); | ||
| $this->assertGreaterThanOrEqual(0, $e->getDuration()); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.