diff --git a/__tests__/plugins/alt-fetch.test.js b/__tests__/plugins/alt-fetch.test.js index d94f62a..c4e5c58 100644 --- a/__tests__/plugins/alt-fetch.test.js +++ b/__tests__/plugins/alt-fetch.test.js @@ -58,7 +58,7 @@ describe("plugin: alt-fetch", () => { expect(self.LibResilientPlugins[0].name).toEqual('alt-fetch'); }); - test("it should fail upon bad config", () => { + test("it should fail with bad config", () => { self.LibResilientConfig = { plugins: { 'alt-fetch':{ @@ -70,7 +70,7 @@ describe("plugin: alt-fetch", () => { expect(()=>{require("../../plugins/alt-fetch.js")}).toThrow(Error); }); - test("it should fetch the content", async () => { + test("it should fetch the content, trying all configured endpoints (if fewer or equal to concurrency setting)", async () => { require("../../plugins/alt-fetch.js"); global.fetch.mockImplementation((url, init) => { @@ -92,7 +92,50 @@ describe("plugin: alt-fetch", () => { const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); - expect(fetch).toHaveBeenCalled(); + expect(fetch).toHaveBeenCalledTimes(3); + expect(await response.json()).toEqual({test: "success"}) + expect(response.url).toEqual('https://resilient.is/test.json') + }) + + test("it should fetch the content using, trying random endpoints out of all configured (if more than concurrency setting)", async () => { + + self.LibResilientConfig = { + plugins: { + 'alt-fetch':{ + endpoints: [ + 'https://alt.resilient.is/test.json', + 'https://error.resilientis/test.json', + 'https://timeout.resilientis/test.json', + 'https://alt2.resilient.is/test.json', + 'https://alt3.resilient.is/test.json', + 'https://alt4.resilient.is/test.json' + ] + } + } + } + + require("../../plugins/alt-fetch.js"); + + global.fetch.mockImplementation((url, init) => { + const response = new Response( + new Blob( + [JSON.stringify({ test: "success" })], + {type: "application/json"} + ), + { + status: 200, + statusText: "OK", + headers: { + 'ETag': 'TestingETagHeader' + }, + url: url + }); + return Promise.resolve(response); + }); + + const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); + + expect(fetch).toHaveBeenCalledTimes(3); expect(await response.json()).toEqual({test: "success"}) expect(response.url).toEqual('https://resilient.is/test.json') }) @@ -102,7 +145,7 @@ describe("plugin: alt-fetch", () => { const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); - expect(fetch).toHaveBeenCalled(); + expect(fetch).toHaveBeenCalledTimes(3); expect(await response.json()).toEqual({test: "success"}) expect(response.url).toEqual('https://resilient.is/test.json') expect(response.headers.has('X-LibResilient-Method')).toEqual(true) @@ -133,7 +176,7 @@ describe("plugin: alt-fetch", () => { const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); - expect(fetch).toHaveBeenCalled(); + expect(fetch).toHaveBeenCalledTimes(3); expect(await response.json()).toEqual({test: "success"}) expect(response.url).toEqual('https://resilient.is/test.json') expect(response.headers.has('X-LibResilient-Method')).toEqual(true)