cache plugin tests: 100% coverage (ref. #8); also, minor bugfix in cache plugin

merge-requests/1/head
Michał 'rysiek' Woźniak 2021-08-30 15:55:53 +00:00
rodzic 28d099a834
commit 4b8ac7e444
2 zmienionych plików z 181 dodań i 2 usunięć

Wyświetl plik

@ -56,7 +56,7 @@ describe("plugin: cache", () => {
require("../../plugins/cache.js");
expect.assertions(7);
return self.LibResilientPlugins[0].stash('https://resilient.is/test.json').then((result)=>{
expect(result).toBe(undefined)
expect(result).toEqual(undefined)
return self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
@ -82,6 +82,35 @@ describe("plugin: cache", () => {
})
});
test("it should stash an array of urls successfully", () => {
require("../../plugins/cache.js");
expect.assertions(13);
return self.LibResilientPlugins[0].stash(['https://resilient.is/test.json', 'https://resilient.is/test2.json']).then((result)=>{
expect(result).toEqual([undefined, undefined])
return self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.url).toEqual('https://resilient.is/test.json')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
}).then(() => {
return self.LibResilientPlugins[0].fetch('https://resilient.is/test2.json')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.url).toEqual('https://resilient.is/test2.json')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
})
});
test("it should clear an array of urls successfully", () => {
require("../../plugins/cache.js");
expect.assertions(4);
@ -96,4 +125,151 @@ describe("plugin: cache", () => {
})
});
test("it should error out when stashing a Response without a url/key", () => {
require("../../plugins/cache.js");
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
}
});
response.url=''
expect.assertions(1);
return expect(self.LibResilientPlugins[0].stash(response)).rejects.toThrow(Error)
});
test("it should stash a Response successfully", () => {
require("../../plugins/cache.js");
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
url: 'https://resilient.is/test.json'
});
expect.assertions(7);
return self.LibResilientPlugins[0].stash(response).then((result)=>{
expect(result).toEqual(undefined)
return self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.url).toEqual('https://resilient.is/test.json')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
})
});
test("it should stash a Response with an explicit key successfully", () => {
require("../../plugins/cache.js");
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
url: 'https://resilient.is/test.json'
});
expect.assertions(7);
return self.LibResilientPlugins[0].stash(response, 'special-key').then((result)=>{
expect(result).toEqual(undefined)
return self.LibResilientPlugins[0].fetch('special-key')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.url).toEqual('https://resilient.is/test.json')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
})
});
test("it should stash a Response with no url set but with an explicit key successfully", () => {
require("../../plugins/cache.js");
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
});
response.url = ''
expect.assertions(6);
return self.LibResilientPlugins[0].stash(response, 'special-key').then((result)=>{
expect(result).toEqual(undefined)
return self.LibResilientPlugins[0].fetch('special-key')
}).then(fetchResult => {
expect(fetchResult.status).toEqual(200)
expect(fetchResult.statusText).toEqual('OK')
expect(fetchResult.headers.has('Etag')).toEqual(true)
expect(fetchResult.headers.get('ETag')).toEqual('TestingETagHeader')
return fetchResult.json().then(json => {
expect(json).toEqual({ test: "success" })
})
})
});
test("it should clear a Response successfully", () => {
require("../../plugins/cache.js");
const response = new Response(
new Blob(
[JSON.stringify({ test: "success" })],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader'
},
url: 'https://resilient.is/test.json'
});
expect.assertions(3);
return self.LibResilientPlugins[0].stash(response).then((result)=>{
expect(result).toBe(undefined)
return self.LibResilientPlugins[0].unstash(response)
}).then(result => {
expect(result).toEqual(true)
return expect(self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')).rejects.toThrow(Error)
})
});
});

Wyświetl plik

@ -73,7 +73,7 @@
}
key = resource.url
}
// we need to create a new Response object
// with all the headers added explicitly
// otherwise the x-libresilient-* headers get ignored
@ -82,6 +82,9 @@
statusText: resource.statusText,
headers: {}
};
if (typeof resource.url === 'string' && resource.url !== '') {
init.url = resource.url
}
resource.headers.forEach(function(val, header){
init.headers[header] = val;
});