diff --git a/__tests__/plugins/cache.test.js b/__tests__/plugins/cache.test.js index 306a53e..88d2422 100644 --- a/__tests__/plugins/cache.test.js +++ b/__tests__/plugins/cache.test.js @@ -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) + }) + }); + }); diff --git a/plugins/cache.js b/plugins/cache.js index eb16b8c..f4a4a16 100644 --- a/plugins/cache.js +++ b/plugins/cache.js @@ -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; });