diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c6dda3..5053b1c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fix authentication headers being sent to origins other than the base URL (GHSA-9hx4-j425-r37q) * Stop authenticating requests redirected from an `http` base URL to `https` * Disable redirect following in Symfony's `Psr18Client` and `HttplugClient` +* Update `ResultPager` to request pagination links from the base URL ## [12.1.0] - 2026-05-06 diff --git a/src/ResultPager.php b/src/ResultPager.php index d2ab7ba2..c2d3a52b 100644 --- a/src/ResultPager.php +++ b/src/ResultPager.php @@ -182,7 +182,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); @@ -195,6 +195,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 new file mode 100644 index 00000000..749c8d43 --- /dev/null +++ b/tests/ResultPagerTest.php @@ -0,0 +1,74 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Tests; + +use Generator; +use Gitlab\Client; +use Gitlab\ResultPager; +use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestInterface; + +class ResultPagerTest extends TestCase +{ + #[DataProvider('linkProvider')] + public function testFetchNextRequestsNextPage(string $link, string $expected): void + { + $requests = []; + $responses = [ + new Response(200, ['Content-Type' => 'application/json', 'Link' => '<'.$link.'>; rel="next"'], '[{"id":1}]'), + new Response(200, ['Content-Type' => 'application/json'], '[{"id":2}]'), + ]; + + $httpClient = $this->createMock(ClientInterface::class); + $httpClient + ->expects($this->exactly(2)) + ->method('sendRequest') + ->willReturnCallback(static function (RequestInterface $request) use (&$requests, &$responses): Response { + $requests[] = $request; + + return \array_shift($responses); + }); + + $client = Client::createWithHttpClient($httpClient); + $client->setUrl('http://gitlab.internal:8080'); + $pager = new ResultPager($client, 1); + + $pager->fetch($client->projects(), 'all', [['search' => 'my project']]); + + $this->assertSame([['id' => 2]], $pager->fetchNext()); + $this->assertSame($expected, (string) $requests[1]->getUri()); + } + + public static function linkProvider(): Generator + { + yield 'other origin' => [ + 'https://gitlab.example.com/api/v4/projects?page=2&per_page=1&search=my%20project', + 'http://gitlab.internal:8080/api/v4/projects?page=2&per_page=1&search=my%20project', + ]; + + yield 'path with a colon followed by digits' => [ + 'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project', + 'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project', + ]; + + yield 'path beginning with two slashes' => [ + 'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project', + 'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project', + ]; + } +}