From a3bc6c383403d8c4e6e2ae71bb292b10be302639 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Tue, 28 Mar 2023 21:53:54 +0200 Subject: [PATCH] fix: pass a URL instance --- src/index.js | 14 +++++----- test/index.js | 74 +++++++++++++++++++++++++++------------------------ 2 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/index.js b/src/index.js index adde6f0..e172409 100644 --- a/src/index.js +++ b/src/index.js @@ -48,12 +48,12 @@ const fetch = ( return reflect ? resolve({ isRejected: true, error }) : resolve({ - url, - html: '', - mode: 'fetch', - headers: error.response ? error.response.headers : {}, - statusCode: error.response ? error.response.statusCode : undefined - }) + url, + html: '', + mode: 'fetch', + headers: error.response ? error.response.headers : {}, + statusCode: error.response ? error.response.statusCode : undefined + }) } }) @@ -136,7 +136,7 @@ const isFetchMode = url => autoDomains.includes(parseUrl(url).domainWithoutSuffix) const determinateMode = (url, { prerender }) => { - if (prerender === false || isMediaUrl(url)) return 'fetch' + if (prerender === false || isMediaUrl(url.toString())) return 'fetch' if (prerender === true) return 'prerender' return isFetchMode(url) ? 'fetch' : 'prerender' } diff --git a/test/index.js b/test/index.js index d4b18b7..4c5fe38 100644 --- a/test/index.js +++ b/test/index.js @@ -11,46 +11,50 @@ const wait = async (promise, prop) => { return prop ? res[prop] : res } -test('reachable URL', async t => { - const url = 'https://example.com' - const [prerenderDisabled, prerenderEnabled] = await Promise.all([ - getHTML(url, { prerender: false, getBrowserless }), - getHTML(url, { - prerender: true, - getBrowserless, - puppeteerOpts: { adblock: false } - }) - ]) - - t.is( - await wait( +;[ + { title: 'from URL', url: 'https://example.com' }, + { title: 'from URL instance', url: new URL('https://example.com') } +].forEach(({ title, url }) => { + test(title, async t => { + const [prerenderDisabled, prerenderEnabled] = await Promise.all([ getHTML(url, { prerender: false, getBrowserless }), - 'statusCode' - ), - 200 - ) - t.is( - await wait( getHTML(url, { prerender: true, getBrowserless, puppeteerOpts: { adblock: false } - }), - 'statusCode' - ), - 200 - ) - - t.is(prerenderDisabled.statusCode, prerenderEnabled.statusCode) - t.is(prerenderDisabled.statusCode, 200) - - t.true(Object.keys(prerenderDisabled.headers).length > 0) - t.true(Object.keys(prerenderEnabled.headers).length > 0) - t.is(typeof prerenderDisabled.headers, typeof prerenderEnabled.headers) - - t.true(prerenderDisabled.html.length > 0) - t.true(prerenderEnabled.html.length > 0) - t.is(typeof prerenderDisabled.html, typeof prerenderEnabled.html) + }) + ]) + + t.is( + await wait( + getHTML(url, { prerender: false, getBrowserless }), + 'statusCode' + ), + 200 + ) + t.is( + await wait( + getHTML(url, { + prerender: true, + getBrowserless, + puppeteerOpts: { adblock: false } + }), + 'statusCode' + ), + 200 + ) + + t.is(prerenderDisabled.statusCode, prerenderEnabled.statusCode) + t.is(prerenderDisabled.statusCode, 200) + + t.true(Object.keys(prerenderDisabled.headers).length > 0) + t.true(Object.keys(prerenderEnabled.headers).length > 0) + t.is(typeof prerenderDisabled.headers, typeof prerenderEnabled.headers) + + t.true(prerenderDisabled.html.length > 0) + t.true(prerenderEnabled.html.length > 0) + t.is(typeof prerenderDisabled.html, typeof prerenderEnabled.html) + }) }) test('timeout URL', async t => {