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 @@ -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

Expand Down
16 changes: 15 additions & 1 deletion src/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 */
Expand Down
74 changes: 74 additions & 0 deletions tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* 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',
];
}
}