alt-fetch plugin tests: coverage 100%, all pass (ref. #8)

merge-requests/1/head
Michał 'rysiek' Woźniak 2021-08-29 17:28:37 +00:00
rodzic 257558904c
commit ef71323cf9
1 zmienionych plików z 48 dodań i 5 usunięć

Wyświetl plik

@ -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 <concurrency> 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)