kopia lustrzana https://gitlab.com/rysiekpl/libresilient
additional tests for handling errors/rejections in plugins (ref. #36)
rodzic
b7419d0fe8
commit
e4db403d62
|
@ -2882,4 +2882,110 @@ describe('service-worker', async () => {
|
|||
|
||||
assertEquals(await response.json(), { test: "success" })
|
||||
})
|
||||
|
||||
it("should return a 404 Not Found HTTP response object when handling a failed navigation request", async () => {
|
||||
window.LibResilientConfig = {
|
||||
plugins: [{
|
||||
name: 'reject-all'
|
||||
}],
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
|
||||
let rejectingFetch = spy(
|
||||
(request, init)=>{ return Promise.reject('reject-all rejecting a request for: ' + request); }
|
||||
)
|
||||
|
||||
window.LibResilientPluginConstructors.set('reject-all', ()=>{
|
||||
return {
|
||||
name: 'reject-all',
|
||||
description: 'Reject all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: rejectingFetch
|
||||
}
|
||||
})
|
||||
|
||||
await import("../../service-worker.js?" + window.test_id);
|
||||
await self.dispatchEvent(new Event('install'))
|
||||
await self.waitForSWInstall()
|
||||
|
||||
let fetch_event = new FetchEvent(window.location.origin + 'test.json', {mode: "navigate"})
|
||||
window.dispatchEvent(fetch_event)
|
||||
let response = await fetch_event.waitForResponse()
|
||||
|
||||
assertEquals(response.status, 404)
|
||||
assertEquals(response.statusText, 'Not Found')
|
||||
assertEquals(response.headers.get('content-type'), 'text/html')
|
||||
assertEquals((await response.text()).slice(0, 57), '<!DOCTYPE html><html><head><title>Loading failed.</title>')
|
||||
})
|
||||
|
||||
it("should not return a 404 Not Found HTTP response object when handling a rejected non-navigation request", async () => {
|
||||
window.LibResilientConfig = {
|
||||
plugins: [{
|
||||
name: 'reject-all'
|
||||
}],
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
|
||||
let rejectingFetch = spy(
|
||||
(request, init)=>{ return Promise.reject('reject-all rejecting a request for: ' + request); }
|
||||
)
|
||||
|
||||
window.LibResilientPluginConstructors.set('reject-all', ()=>{
|
||||
return {
|
||||
name: 'reject-all',
|
||||
description: 'Reject all requests.',
|
||||
version: '0.0.1',
|
||||
fetch: rejectingFetch
|
||||
}
|
||||
})
|
||||
|
||||
await import("../../service-worker.js?" + window.test_id);
|
||||
await self.dispatchEvent(new Event('install'))
|
||||
await self.waitForSWInstall()
|
||||
|
||||
let fetch_event = new FetchEvent(window.location.origin + 'test.json')
|
||||
window.dispatchEvent(fetch_event)
|
||||
assertRejects(async ()=>{ await fetch_event.waitForResponse() })
|
||||
})
|
||||
|
||||
it("should not return a 404 Not Found HTTP response object when handling a non-navigation request that throws an error", async () => {
|
||||
window.LibResilientConfig = {
|
||||
plugins: [{
|
||||
name: 'error-out'
|
||||
}],
|
||||
loggedComponents: [
|
||||
'service-worker'
|
||||
]
|
||||
}
|
||||
|
||||
let throwingFetch = spy(
|
||||
(request, init)=>{ throw new Error('error-out throwing an Error for: ' + request); }
|
||||
)
|
||||
|
||||
window.LibResilientPluginConstructors.set('error-out', ()=>{
|
||||
return {
|
||||
name: 'error-out',
|
||||
description: 'Throws.',
|
||||
version: '0.0.1',
|
||||
fetch: throwingFetch
|
||||
}
|
||||
})
|
||||
|
||||
await import("../../service-worker.js?" + window.test_id);
|
||||
await self.dispatchEvent(new Event('install'))
|
||||
await self.waitForSWInstall()
|
||||
|
||||
let fetch_event = new FetchEvent(window.location.origin + 'test.json')
|
||||
window.dispatchEvent(fetch_event)
|
||||
assertRejects(async ()=>{
|
||||
await fetch_event.waitForResponse()
|
||||
},
|
||||
Error,
|
||||
'error-out throwing an Error for: https://test.resilient.is/test.json'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
Ładowanie…
Reference in New Issue