kopia lustrzana https://gitlab.com/rysiekpl/libresilient
cache plugin tests: 100% coverage (ref. #8); also, minor bugfix in cache plugin
rodzic
28d099a834
commit
4b8ac7e444
|
@ -56,7 +56,7 @@ describe("plugin: cache", () => {
|
||||||
require("../../plugins/cache.js");
|
require("../../plugins/cache.js");
|
||||||
expect.assertions(7);
|
expect.assertions(7);
|
||||||
return self.LibResilientPlugins[0].stash('https://resilient.is/test.json').then((result)=>{
|
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')
|
return self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
|
||||||
}).then(fetchResult => {
|
}).then(fetchResult => {
|
||||||
expect(fetchResult.status).toEqual(200)
|
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", () => {
|
test("it should clear an array of urls successfully", () => {
|
||||||
require("../../plugins/cache.js");
|
require("../../plugins/cache.js");
|
||||||
expect.assertions(4);
|
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)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -82,6 +82,9 @@
|
||||||
statusText: resource.statusText,
|
statusText: resource.statusText,
|
||||||
headers: {}
|
headers: {}
|
||||||
};
|
};
|
||||||
|
if (typeof resource.url === 'string' && resource.url !== '') {
|
||||||
|
init.url = resource.url
|
||||||
|
}
|
||||||
resource.headers.forEach(function(val, header){
|
resource.headers.forEach(function(val, header){
|
||||||
init.headers[header] = val;
|
init.headers[header] = val;
|
||||||
});
|
});
|
||||||
|
|
Ładowanie…
Reference in New Issue