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
11 changes: 9 additions & 2 deletions lib/shared/wp-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,14 @@ function mergeUrl( endpoint, linkPath ) {
* pagination metadata
*/
function paginateResponse( result, endpoint ) {
if ( ! result.headers || ! result.headers['x-wp-totalpages'] ) {
if ( ! result.headers || ! result.headers.link ) {
// No headers: return as-is
return result;
}

var totalPages = result.headers['x-wp-totalpages'];

if ( ! totalPages || totalPages === '0' || totalPages === '1' ) {
// No paging: return as-is
return result;
}
Expand All @@ -231,7 +238,7 @@ function paginateResponse( result, endpoint ) {
// Store pagination data from response headers on the response collection
result.body._paging = {
total: result.headers['x-wp-total'],
totalPages: result.headers['x-wp-totalpages'],
totalPages: totalPages,
links: links
};

Expand Down
30 changes: 27 additions & 3 deletions tests/lib/shared/wp-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,36 @@ describe( 'WPRequest', function() {
});
});

it( 'passes data through unchanged if no pagination headers are present', function() {
it( 'passes data through unchanged if no headers are present', function() {
mockAgent._response = {
headers: {},
body: 'some object'
};
wpRequest.then(function(parsedResult) {
return wpRequest.then(function(parsedResult) {
expect( parsedResult ).to.equal( 'some object' );
expect( parsedResult ).not.to.have.property( '_paging' );
});
});

it( 'passes data through unchanged if header has no link property', function() {
mockAgent._response = {
headers: {
'x-wp-totalpages': '0',
'x-wp-total': '0'
},
body: 'some object'
};
return wpRequest.then(function(parsedResult) {
expect( parsedResult ).to.equal( 'some object' );
expect( parsedResult ).not.to.have.property( '_paging' );
});
});

it( 'passes data through unchanged if pagination header is unset or empty', function() {
mockAgent._response = {
headers: { link: '' },
body: 'some object'
};
return wpRequest.then(function(parsedResult) {
expect( parsedResult ).to.equal( 'some object' );
expect( parsedResult ).not.to.have.property( '_paging' );
});
Expand Down