Skip to content

Commit a4be0bf

Browse files
Request pagination links from the base URL
1 parent 89fb17f commit a4be0bf

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Fix authentication headers being sent to origins other than the base URL (GHSA-9hx4-j425-r37q)
1111
* Stop authenticating requests redirected from an `http` base URL to `https`
1212
* Disable redirect following in Symfony's `Psr18Client` and `HttplugClient`
13+
* Update `ResultPager` to request pagination links from the base URL
1314

1415
## [12.1.0] - 2026-05-06
1516

‎src/ResultPager.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function get(string $key): array
182182
return [];
183183
}
184184

185-
$result = $this->client->getHttpClient()->get($pagination);
185+
$result = $this->client->getHttpClient()->get(self::getPathAndQuery($pagination));
186186

187187
$content = ResponseMediator::getContent($result);
188188

@@ -195,6 +195,20 @@ private function get(string $key): array
195195
return $content;
196196
}
197197

198+
private static function getPathAndQuery(string $url): string
199+
{
200+
$path = \parse_url($url, \PHP_URL_PATH);
201+
$query = \parse_url($url, \PHP_URL_QUERY);
202+
$pathAndQuery = $path.(\is_string($query) ? '?'.$query : '');
203+
204+
// some paths cannot be requested without their host
205+
if (!\is_string($path) || \parse_url($pathAndQuery, \PHP_URL_PATH) !== $path) {
206+
return $url;
207+
}
208+
209+
return $pathAndQuery;
210+
}
211+
198212
private static function bindPerPage(AbstractApi $api, int $perPage): AbstractApi
199213
{
200214
/** @var Closure(AbstractApi): AbstractApi */

‎tests/ResultPagerTest.php‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
9+
* (c) Graham Campbell <hello@gjcampbell.co.uk>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Gitlab\Tests;
16+
17+
use Generator;
18+
use Gitlab\Client;
19+
use Gitlab\ResultPager;
20+
use GuzzleHttp\Psr7\Response;
21+
use PHPUnit\Framework\Attributes\DataProvider;
22+
use PHPUnit\Framework\TestCase;
23+
use Psr\Http\Client\ClientInterface;
24+
use Psr\Http\Message\RequestInterface;
25+
26+
class ResultPagerTest extends TestCase
27+
{
28+
#[DataProvider('linkProvider')]
29+
public function testFetchNextRequestsNextPage(string $link, string $expected): void
30+
{
31+
$requests = [];
32+
$responses = [
33+
new Response(200, ['Content-Type' => 'application/json', 'Link' => '<'.$link.'>; rel="next"'], '[{"id":1}]'),
34+
new Response(200, ['Content-Type' => 'application/json'], '[{"id":2}]'),
35+
];
36+
37+
$httpClient = $this->createMock(ClientInterface::class);
38+
$httpClient
39+
->expects($this->exactly(2))
40+
->method('sendRequest')
41+
->willReturnCallback(static function (RequestInterface $request) use (&$requests, &$responses): Response {
42+
$requests[] = $request;
43+
44+
return \array_shift($responses);
45+
});
46+
47+
$client = Client::createWithHttpClient($httpClient);
48+
$client->setUrl('http://gitlab.internal:8080');
49+
$pager = new ResultPager($client, 1);
50+
51+
$pager->fetch($client->projects(), 'all', [['search' => 'my project']]);
52+
53+
$this->assertSame([['id' => 2]], $pager->fetchNext());
54+
$this->assertSame($expected, (string) $requests[1]->getUri());
55+
}
56+
57+
public static function linkProvider(): Generator
58+
{
59+
yield 'other origin' => [
60+
'https://gitlab.example.com/api/v4/projects?page=2&per_page=1&search=my%20project',
61+
'http://gitlab.internal:8080/api/v4/projects?page=2&per_page=1&search=my%20project',
62+
];
63+
64+
yield 'path with a colon followed by digits' => [
65+
'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project',
66+
'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project',
67+
];
68+
69+
yield 'path beginning with two slashes' => [
70+
'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project',
71+
'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project',
72+
];
73+
}
74+
}

0 commit comments

Comments
 (0)