From b7419d0fe80410712c551a58a648843755e3e459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Wed, 6 Mar 2024 16:33:22 +0000 Subject: [PATCH] tests passing for work done so far for error handling improvements (ref. #36) --- plugins/any-of/__tests__/browser.test.js | 28 ---------- plugins/delay/__tests__/browser.test.js | 24 --------- .../dnslink-fetch/__tests__/browser.test.js | 54 ------------------- plugins/dnslink-fetch/index.js | 6 +-- plugins/fetch/__tests__/browser.test.js | 24 --------- plugins/test-plugin/__tests__/browser.test.js | 24 --------- plugins/test-plugin/index.js | 11 ++-- 7 files changed, 6 insertions(+), 165 deletions(-) diff --git a/plugins/any-of/__tests__/browser.test.js b/plugins/any-of/__tests__/browser.test.js index 1a9bb8d..af0665a 100644 --- a/plugins/any-of/__tests__/browser.test.js +++ b/plugins/any-of/__tests__/browser.test.js @@ -122,32 +122,4 @@ describe('browser: any-of plugin', async () => { }) assertEquals(await response.json(), {test: "success"}) }); - - it("should throw an error when HTTP status is >= 400", async () => { - - window.fetch = spy((url, init) => { - return Promise.resolve( - new Response( - new Blob( - ["Not Found"], - {type: "text/plain"} - ), - { - status: 404, - statusText: "Not Found" - } - ) - ) - }); - - assertRejects( - async () => { - return await LibResilientPluginConstructors - .get('any-of')(LR, init) - .fetch('https://resilient.is/test.json') }, - AggregateError, - 'All promises were rejected' - ) - assertSpyCalls(fetch, 1); - }); }) diff --git a/plugins/delay/__tests__/browser.test.js b/plugins/delay/__tests__/browser.test.js index 612290f..df92c55 100644 --- a/plugins/delay/__tests__/browser.test.js +++ b/plugins/delay/__tests__/browser.test.js @@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => { assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader') }); - it("should throw an error when HTTP status is >= 400", async () => { - window.fetch = (url, init) => { - const response = new Response( - new Blob( - ["Not Found"], - {type: "text/plain"} - ), - { - status: 404, - statusText: "Not Found", - url: url - }); - return Promise.resolve(response); - } - assertRejects( - async () => { - return await LibResilientPluginConstructors - .get('fetch')(LR) - .fetch('https://resilient.is/test.json') }, - Error, - 'HTTP Error: 404 Not Found' - ) - }); - }) diff --git a/plugins/dnslink-fetch/__tests__/browser.test.js b/plugins/dnslink-fetch/__tests__/browser.test.js index ae7b26e..5fad586 100644 --- a/plugins/dnslink-fetch/__tests__/browser.test.js +++ b/plugins/dnslink-fetch/__tests__/browser.test.js @@ -1950,58 +1950,4 @@ describe('browser: dnslink-fetch plugin', async () => { assertEquals(response.headers.get('X-LibResilient-Method'), 'dnslink-fetch') assertEquals(response.headers.get('X-LibResilient-Etag'), 'TestingLastModifiedHeader') }); - - it("should throw an error when HTTP status is >= 400", async () => { - - window.resolvingFetch = (url, init) => { - if (url.startsWith('https://dns.hostux.net/dns-query')) { - const response = new Response( - new Blob( - [JSON.stringify(fetchResponse[0])], - {type: fetchResponse[1]} - ), - { - status: 200, - statusText: "OK", - headers: { - 'Last-Modified': 'TestingLastModifiedHeader' - }, - url: url - }); - return Promise.resolve(response); - } else { - const response = new Response( - new Blob( - ["Not Found"], - {type: "text/plain"} - ), - { - status: 404, - statusText: "Not Found", - url: url - }); - return Promise.resolve(response); - } - } - window.fetch = spy(window.resolvingFetch) - - window.fetchResponse = [ - {Status: 0, Answer: [ - {type: 16, data: 'dnslink=/https/example.org'}, - {type: 16, data: 'dnslink=/http/example.net/some/path'} - ]}, - "application/json" - ] - - assertRejects( - async ()=>{ - const response = await LibResilientPluginConstructors - .get('dnslink-fetch')(LR, init) - .fetch('https://resilient.is/test.json') - console.log(response) - }, - Error, - 'HTTP Error:' - ) - }); }) diff --git a/plugins/dnslink-fetch/index.js b/plugins/dnslink-fetch/index.js index 80e43c0..53d7846 100644 --- a/plugins/dnslink-fetch/index.js +++ b/plugins/dnslink-fetch/index.js @@ -226,11 +226,7 @@ u=>fetch(u, init) )) .then((response) => { - // 4xx? 5xx? that's a paddlin' - if (response.status >= 400) { - // throw an Error to fall back to other plugins: - throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText); - } + // all good, it seems LR.log(pluginName, "fetched:", response.url); diff --git a/plugins/fetch/__tests__/browser.test.js b/plugins/fetch/__tests__/browser.test.js index 612290f..df92c55 100644 --- a/plugins/fetch/__tests__/browser.test.js +++ b/plugins/fetch/__tests__/browser.test.js @@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => { assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader') }); - it("should throw an error when HTTP status is >= 400", async () => { - window.fetch = (url, init) => { - const response = new Response( - new Blob( - ["Not Found"], - {type: "text/plain"} - ), - { - status: 404, - statusText: "Not Found", - url: url - }); - return Promise.resolve(response); - } - assertRejects( - async () => { - return await LibResilientPluginConstructors - .get('fetch')(LR) - .fetch('https://resilient.is/test.json') }, - Error, - 'HTTP Error: 404 Not Found' - ) - }); - }) diff --git a/plugins/test-plugin/__tests__/browser.test.js b/plugins/test-plugin/__tests__/browser.test.js index 612290f..df92c55 100644 --- a/plugins/test-plugin/__tests__/browser.test.js +++ b/plugins/test-plugin/__tests__/browser.test.js @@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => { assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader') }); - it("should throw an error when HTTP status is >= 400", async () => { - window.fetch = (url, init) => { - const response = new Response( - new Blob( - ["Not Found"], - {type: "text/plain"} - ), - { - status: 404, - statusText: "Not Found", - url: url - }); - return Promise.resolve(response); - } - assertRejects( - async () => { - return await LibResilientPluginConstructors - .get('fetch')(LR) - .fetch('https://resilient.is/test.json') }, - Error, - 'HTTP Error: 404 Not Found' - ) - }); - }) diff --git a/plugins/test-plugin/index.js b/plugins/test-plugin/index.js index 521c0e5..fd82648 100644 --- a/plugins/test-plugin/index.js +++ b/plugins/test-plugin/index.js @@ -32,12 +32,11 @@ // run built-in regular fetch() return fetch(url, init) .then(async (response) => { - // 4xx? 5xx? that's a paddlin' - if (response.status >= 400) { - // throw an Error to fall back to LibResilient: - throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText); - } - // all good, it seems + + // we got something, it seems + // it might be a 2xx; it might be a 3xx redirect + // it might also be a 4xx or a 5xx error + // the service worker will know how to deal with those LR.log(pluginName, `fetched successfully: ${response.url}`); // we need to create a new Response object