Skip to content

Commit

Permalink
Merge pull request #81 from ably/RSH1b2
Browse files Browse the repository at this point in the history
RSH1b2 list pagination, test next()
  • Loading branch information
owenpearson authored Mar 4, 2021
2 parents 8441451 + 4b5c2a7 commit 4b43f11
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/Models/PaginatedResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class PaginatedResult {
* @param array $headers Headers to be sent with the request
* @throws AblyException
*/
public function __construct( \Ably\AblyRest $ably, $model, $cipherParams, $method, $path, $params = [], $headers = [] ) {
public function __construct( \Ably\AblyRest $ably, $model, $cipherParams,
$method, $path, $params = [], $headers = [] ) {
$this->ably = $ably;
$this->model = $model;
$this->cipherParams = $cipherParams;
Expand Down Expand Up @@ -140,15 +141,13 @@ public function isPaginated() {
* Parses HTTP headers for pagination links
*/
private function parsePaginationHeaders($headers) {

$path = preg_replace('/\/[^\/]*$/', '/', $this->path);

preg_match_all('/Link: *\<([^\>]*)\>; *rel="([^"]*)"/i', $headers, $matches, PREG_SET_ORDER);

if (!$matches) return;

$this->paginationHeaders = [];

foreach ($matches as $m) {
$link = $m[1];
$rel = $m[2];
Expand All @@ -157,7 +156,10 @@ private function parsePaginationHeaders($headers) {
throw new AblyException( "Server error - only relative URLs are supported in pagination" );
}

$this->paginationHeaders[$rel] = $path.substr($link, 2);
$link = explode('/', $link);
$link = $path . end($link);

$this->paginationHeaders[$rel] = $link;
}
}
}
4 changes: 2 additions & 2 deletions tests/AblyRestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,13 @@ public function testHttpTimeout() {

$ablyTimeout = new AblyRest( [
'key' => 'fake.key:veryFake',
'httpRequestTimeout' => 50, // 50 ms
'httpRequestTimeout' => 20, // 20 ms
]);

$ably->http->get('https://cdn.ably.io/lib/ably.js'); // should work
$this->expectException(AblyRequestException::class);
$this->expectExceptionCode(50003);
$ablyTimeout->http->get('https://cdn.ably.io/lib/ably.js'); // guaranteed to take more than 50 ms
$ablyTimeout->http->get('https://cdn.ably.io/lib/ably.js'); // guaranteed to take more than 20 ms
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PushDeviceRegistrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public function testList() {
$response = self::$ably->push->admin->deviceRegistrations->list_([ 'limit' => 2 ]);
$this->assertEquals(count($response->items), 2);

// pagination
$response = self::$ably->push->admin->deviceRegistrations->list_([ 'limit' => 1 ]);
$this->assertEquals(count($response->items), 1);
$response = $response->next();
$this->assertEquals(count($response->items), 1);

// Filter by device id
$first = $datas[0];
$response = self::$ably->push->admin->deviceRegistrations->list_([ 'deviceId' => $first['id'] ]);
Expand Down

0 comments on commit 4b43f11

Please sign in to comment.