From 1e1e50647e93d038a4cc6a9bbbfbf61165d8fd39 Mon Sep 17 00:00:00 2001 From: Joshua Leyshon Date: Thu, 1 Jul 2021 14:37:28 +0930 Subject: [PATCH] Fix default pagination handling for empty `Link` header (#1768) Co-authored-by: Sindre Sorhus --- source/core/options.ts | 5 +++-- test/pagination.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/source/core/options.ts b/source/core/options.ts index b8343debd..0b93beeb5 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -674,11 +674,12 @@ const defaultInternals: Options['_internals'] = { return JSON.parse(response.body as string); }, paginate: ({response}) => { - if (typeof response.headers.link !== 'string') { + const rawLinkHeader = response.headers.link; + if (typeof rawLinkHeader !== 'string' || rawLinkHeader.trim() === '') { return false; } - const parsed = parseLinkHeader(response.headers.link); + const parsed = parseLinkHeader(rawLinkHeader); const next = parsed.find(entry => entry.parameters.rel === 'next' || entry.parameters.rel === '"next"'); if (next) { diff --git a/test/pagination.ts b/test/pagination.ts index a6c7e70a9..784f1e69f 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -43,6 +43,18 @@ test('the link header has no next value', withServer, async (t, server, got) => t.deepEqual(received, items); }); +test('the link header is empty', withServer, async (t, server, got) => { + const items = [1]; + + server.get('/', (_request, response) => { + response.setHeader('link', ''); + response.end(JSON.stringify(items)); + }); + + const received = await got.paginate.all(''); + t.deepEqual(received, items); +}); + test('retrieves all elements', withServer, async (t, server, got) => { attachHandler(server, 2);