From bc29355858f1933a0f48408c5e8c2697b12f589f Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 17 Nov 2026 17:25:10 +0000 Subject: [PATCH] Request pagination links from the base URL --- CHANGELOG.md | 1 + src/ResultPager.php | 16 +++++- tests/ResultPagerTest.php | 107 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03b5c01..81ca332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGE LOG * Fixed authentication being sent to origins other than the base URL (GHSA-jp92-97q7-5p29) * Stopped authenticating requests redirected from an `http` base URL to `https` +* Updated ResultPager to request pagination links from the base URL ## V5.1 (06/05/2026) diff --git a/src/ResultPager.php b/src/ResultPager.php index 7a7c955..49f9d32 100644 --- a/src/ResultPager.php +++ b/src/ResultPager.php @@ -162,7 +162,7 @@ private function get(string $key): array return []; } - $result = $this->client->getHttpClient()->get($pagination); + $result = $this->client->getHttpClient()->get(self::getPathAndQuery($pagination)); $content = ResponseMediator::getContent($result); @@ -171,6 +171,20 @@ private function get(string $key): array return $content; } + private static function getPathAndQuery(string $url): string + { + $path = \parse_url($url, \PHP_URL_PATH); + $query = \parse_url($url, \PHP_URL_QUERY); + $pathAndQuery = $path.(\is_string($query) ? '?'.$query : ''); + + // some paths cannot be requested without their host + if (!\is_string($path) || \parse_url($pathAndQuery, \PHP_URL_PATH) !== $path) { + return $url; + } + + return $pathAndQuery; + } + private static function bindPerPage(AbstractApi $api, int $perPage): AbstractApi { /** @var Closure(AbstractApi): AbstractApi */ diff --git a/tests/ResultPagerTest.php b/tests/ResultPagerTest.php index e684529..adc0d46 100644 --- a/tests/ResultPagerTest.php +++ b/tests/ResultPagerTest.php @@ -84,6 +84,113 @@ public function testDirectCallsDoNotPreservePaginationFields(): void self::assertSame('values.name', $query['fields']); } + public function testFetchNextRequestsNextPageFromBaseUrl(): void + { + $httpClient = new MockClient(); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'next' => 'https://api.bitbucket.org/2.0/repositories/my-workspace/my-repo/refs/tags?page=2&q=name%20~%20%22v1%22', + 'values' => [ + ['name' => 'v1.0.0'], + ], + ])); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'values' => [ + ['name' => 'v1.0.1'], + ], + ])); + + $client = new Client(new Builder($httpClient)); + $client->setUrl('http://bitbucket.example.com:8080'); + $pager = new ResultPager($client, 1); + + $pager->fetch( + $client->repositories()->workspaces('my-workspace')->refs('my-repo')->tags(), + 'list' + ); + + self::assertSame(['pagelen' => 1, 'values' => [['name' => 'v1.0.1']]], $pager->fetchNext()); + + $requests = $httpClient->getRequests(); + + self::assertSame('http://bitbucket.example.com:8080/2.0/repositories/my-workspace/my-repo/refs/tags?page=2&q=name%20~%20%22v1%22', (string) $requests[1]->getUri()); + } + + public function testFetchNextRequestsNextPageWithColonAndDigitsInPathAsGiven(): void + { + $next = 'https://api.bitbucket.org/2.0/repositories/my-workspace/my-repo/refs/tags:80/?page=2'; + $httpClient = new MockClient(); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'next' => $next, + 'values' => [ + ['name' => 'v1.0.0'], + ], + ])); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'values' => [ + ['name' => 'v1.0.1'], + ], + ])); + + $client = new Client(new Builder($httpClient)); + $client->setUrl('http://bitbucket.example.com:8080'); + $pager = new ResultPager($client, 1); + + $pager->fetch( + $client->repositories()->workspaces('my-workspace')->refs('my-repo')->tags(), + 'list' + ); + + $pager->fetchNext(); + + $requests = $httpClient->getRequests(); + + self::assertSame($next, (string) $requests[1]->getUri()); + } + + public function testFetchNextRequestsNextPageWithDoubleSlashPathAsGiven(): void + { + $next = 'https://api.bitbucket.org//2.0/repositories/my-workspace/my-repo/refs/tags?page=2'; + $httpClient = new MockClient(); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'next' => $next, + 'values' => [ + ['name' => 'v1.0.0'], + ], + ])); + + $httpClient->addResponse(self::jsonResponse([ + 'pagelen' => 1, + 'values' => [ + ['name' => 'v1.0.1'], + ], + ])); + + $client = new Client(new Builder($httpClient)); + $client->setUrl('http://bitbucket.example.com:8080'); + $pager = new ResultPager($client, 1); + + $pager->fetch( + $client->repositories()->workspaces('my-workspace')->refs('my-repo')->tags(), + 'list' + ); + + $pager->fetchNext(); + + $requests = $httpClient->getRequests(); + + self::assertSame($next, (string) $requests[1]->getUri()); + } + public function testHasPreviousUsesPreviousPaginationField(): void { $httpClient = new MockClient();