Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion src/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 */
Expand Down
107 changes: 107 additions & 0 deletions tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down