kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker: two more tests - POST requests, unstashing (ref. #8)
rodzic
88f3f37408
commit
a85b3cb5d4
|
@ -109,6 +109,46 @@ describe("service-worker", () => {
|
|||
expect(response.headers.get('X-LibResilient-ETag')).toEqual('TestingETagHeader')
|
||||
});
|
||||
|
||||
test("making a POST request should work and not go through the plugins", async () => {
|
||||
global.fetch.mockImplementation((request, init) => {
|
||||
return Promise.resolve(
|
||||
new Response(
|
||||
new Blob(
|
||||
[JSON.stringify({ test: "success" })],
|
||||
{type: "application/json"}
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
'ETag': 'TestingETagHeader'
|
||||
},
|
||||
method: 'POST',
|
||||
url: request.url
|
||||
})
|
||||
);
|
||||
});
|
||||
self.LibResilientConfig = {
|
||||
plugins: {
|
||||
'reject-all': {}
|
||||
},
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
self.LibResilientPlugins.push({
|
||||
name: 'reject-all',
|
||||
description: 'Reject all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: (request, init)=>{ return Promise.reject(request); }
|
||||
})
|
||||
require("../service-worker.js");
|
||||
var response = await self.trigger('fetch', new Request('/test.json', {method: "POST"}))
|
||||
console.log(response)
|
||||
expect(response.method).toEqual('POST')
|
||||
expect(await response.json()).toEqual({ test: "success" })
|
||||
})
|
||||
|
||||
test("stashing content after a successful fetch should work", async () => {
|
||||
self.LibResilientConfig = {
|
||||
plugins: {
|
||||
|
@ -190,5 +230,66 @@ describe("service-worker", () => {
|
|||
return json
|
||||
})).toEqual({ test: "success" })
|
||||
});
|
||||
|
||||
test("unstashing content explicitly should work", async () => {
|
||||
self.LibResilientConfig = {
|
||||
plugins: {
|
||||
'cache': {}
|
||||
},
|
||||
loggedComponents: [
|
||||
'service-worker', 'cache'
|
||||
]
|
||||
}
|
||||
require("../plugins/cache.js");
|
||||
require("../service-worker.js");
|
||||
|
||||
await self.trigger('install')
|
||||
await self.trigger('activate')
|
||||
|
||||
await self.trigger(
|
||||
'message',
|
||||
{
|
||||
data:{
|
||||
stash: [new Response(
|
||||
new Blob(
|
||||
[JSON.stringify({ test: "success" })],
|
||||
{type: "application/json"}
|
||||
),
|
||||
{
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: {
|
||||
'ETag': 'TestingETagHeader'
|
||||
},
|
||||
url: self.location.origin + '/test.json'
|
||||
})]
|
||||
}
|
||||
})
|
||||
|
||||
expect (await caches.open('v1').then((cache)=>{
|
||||
return cache.keys()
|
||||
}).then((keys)=>{
|
||||
return keys[0].url
|
||||
})).toEqual(self.location.origin + '/test.json')
|
||||
expect (await caches.open('v1').then((cache)=>{
|
||||
return cache.match(self.location.origin + '/test.json')
|
||||
}).then((response)=>{
|
||||
return response.json()
|
||||
}).then((json)=>{
|
||||
return json
|
||||
})).toEqual({ test: "success" })
|
||||
|
||||
// now unstash
|
||||
await self.trigger(
|
||||
'message',
|
||||
{
|
||||
data:{
|
||||
unstash: [self.location.origin + '/test.json']
|
||||
}
|
||||
})
|
||||
expect (await caches.open('v1').then((cache)=>{
|
||||
return cache.keys()
|
||||
})).toEqual([])
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Ładowanie…
Reference in New Issue