dnslink-fetch: 100% statements test coverage (ref. #63)

merge-requests/17/head
Michał 'rysiek' Woźniak 2022-10-22 01:42:37 +00:00
rodzic ea2e30145d
commit eef45d9579
2 zmienionych plików z 36 dodań i 71 usunięć

Wyświetl plik

@ -241,7 +241,7 @@ describe("plugin: alt-fetch", () => {
expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingETagHeader')
});
test("it should set the LibResilient ETag basd on Last-Modified header (if ETag is not available in the original response)", async () => {
test("it should set the LibResilient ETag based on Last-Modified header (if ETag is not available in the original response)", async () => {
require("../../../plugins/alt-fetch/index.js");
global.fetch.mockImplementation((url, init) => {

Wyświetl plik

@ -55,6 +55,9 @@ describe("plugin: dnslink-fetch", () => {
{
status: 200,
statusText: "OK",
headers: {
'Last-Modified': 'TestingLastModifiedHeader'
},
url: url
});
return Promise.resolve(response);
@ -252,38 +255,10 @@ describe("plugin: dnslink-fetch", () => {
expect(response.url).toEqual('https://resilient.is/test.json')
})
test.skip("it should pass the Request() init data to fetch() for all used endpoints", async () => {
init = {
name: 'dnslink-fetch',
endpoints: [
'https://alt.resilient.is/',
'https://error.resilient.is/',
'https://timeout.resilient.is/',
'https://alt2.resilient.is/',
'https://alt3.resilient.is/',
'https://alt4.resilient.is/'
]}
test("it should pass the Request() init data to fetch() for all used endpoints", async () => {
require("../../../plugins/dnslink-fetch/index.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);
});
var initTest = {
method: "GET",
headers: new Headers({"x-stub": "STUB"}),
@ -298,54 +273,27 @@ describe("plugin: dnslink-fetch", () => {
cache: undefined
}
global.fetchResponse = [{Status: 0, Answer: [{type: 16, data: 'dnslink=/https/example.org'}, {type: 16, data: 'dnslink=/http/example.net/some/path'}, {type: 16, data: 'dnslink=/https/example.net/some/path'}, {type: 16, data: 'dnslink=/https/example.net/some/other/path'}]}, "application/json"]
const response = await LibResilientPluginConstructors.get('dnslink-fetch')(LR, init).fetch('https://resilient.is/test.json', initTest);
expect(fetch).toHaveBeenCalledTimes(3);
expect(fetch).toHaveBeenNthCalledWith(1, expect.stringContaining('/test.json'), initTest);
expect(fetch).toHaveBeenCalledTimes(4); // 1 fetch to resolve DNSLink, then <concurrency> (default: 3) fetch requests to the two DNSLink-resolved endpoints
expect(await response.json()).toEqual(global.fetchResponse[0])
expect(response.url).toEqual('https://resilient.is/test.json')
expect(fetch).toHaveBeenNthCalledWith(2, expect.stringContaining('/test.json'), initTest);
expect(fetch).toHaveBeenNthCalledWith(3, expect.stringContaining('/test.json'), initTest);
expect(await response.json()).toEqual({test: "success"})
expect(response.url).toEqual('https://resilient.is/test.json')
expect(fetch).toHaveBeenNthCalledWith(4, expect.stringContaining('/test.json'), initTest);
})
test.skip("it should set the LibResilient headers", async () => {
test("it should set the LibResilient headers, setting X-LibResilient-ETag based on Last-Modified (if ETag is unavailable in the original response)", async () => {
require("../../../plugins/dnslink-fetch/index.js");
global.fetchResponse = [{Status: 0, Answer: [{type: 16, data: 'dnslink=/https/example.org'}, {type: 16, data: 'dnslink=/http/example.net/some/path'}]}, "application/json"]
const response = await LibResilientPluginConstructors.get('dnslink-fetch')(LR, init).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')
expect(response.headers.has('X-LibResilient-Method')).toEqual(true)
expect(response.headers.get('X-LibResilient-Method')).toEqual('dnslink-fetch')
expect(response.headers.has('X-LibResilient-Etag')).toEqual(true)
expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingETagHeader')
});
test.skip("it should set the LibResilient ETag basd on Last-Modified header (if ETag is not available in the original response)", async () => {
require("../../../plugins/dnslink-fetch/index.js");
global.fetch.mockImplementation((url, init) => {
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'Last-Modified': 'TestingLastModifiedHeader'
},
url: url
});
return Promise.resolve(response);
});
const response = await LibResilientPluginConstructors.get('dnslink-fetch')(LR, init).fetch('https://resilient.is/test.json');
expect(fetch).toHaveBeenCalledTimes(3);
expect(await response.json()).toEqual({test: "success"})
expect(await response.json()).toEqual(global.fetchResponse[0])
expect(response.url).toEqual('https://resilient.is/test.json')
expect(response.headers.has('X-LibResilient-Method')).toEqual(true)
expect(response.headers.get('X-LibResilient-Method')).toEqual('dnslink-fetch')
@ -353,9 +301,25 @@ describe("plugin: dnslink-fetch", () => {
expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingLastModifiedHeader')
});
test.skip("it should throw an error when HTTP status is >= 400", async () => {
test("it should throw an error when HTTP status is >= 400", async () => {
global.fetch.mockImplementation((url, init) => {
global.fetch.mockImplementation((url, init) => {
if (url.startsWith('https://dns.google/resolve')) {
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"],
@ -367,10 +331,11 @@ describe("plugin: dnslink-fetch", () => {
url: url
});
return Promise.resolve(response);
});
}
});
require("../../../plugins/dnslink-fetch/index.js");
global.fetchResponse = [{Status: 0, Answer: [{type: 16, data: 'dnslink=/https/example.org'}, {type: 16, data: 'dnslink=/http/example.net/some/path'}]}, "application/json"]
expect.assertions(1)
expect(LibResilientPluginConstructors.get('dnslink-fetch')(LR, init).fetch('https://resilient.is/test.json')).rejects.toThrow(Error)
});