tests for fetching config via plugins (ref. #30)

merge-requests/12/merge
Michał 'rysiek' Woźniak 2022-02-07 23:08:13 +00:00
rodzic deeb57a3e2
commit 1fb9ab7e05
1 zmienionych plików z 172 dodań i 0 usunięć

Wyświetl plik

@ -383,6 +383,178 @@ describe("service-worker", () => {
})
test("basic set-up: a stale cached valid config.json file gets used, no fetch happens, fresh config.json is retrieved using the configured plugins and cached", async () => {
self.LibResilientConfig = null
var configData = {loggedComponents: ['service-worker', 'cache', 'resolve-config'], plugins: [{name: "cache"}, {name: "resolve-config"}], defaultPluginTimeout: 5000}
var configUrl = '/config.json'
var configResponse = new Response(
new Blob(
[JSON.stringify(configData)],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader',
// very stale date
'Date': new Date(0).toUTCString()
},
url: configUrl
})
await caches
.open('v1')
.then((cache)=>{
return cache.put(configUrl, configResponse)
})
var newConfigData = {loggedComponents: ['service-worker', 'resolve-config'], plugins: [{name: "resolve-config"}], defaultPluginTimeout: 2000}
let resolveConfigFetch = jest.fn((request, init)=>{
return Promise.resolve(
new Response(
new Blob(
[JSON.stringify(newConfigData)],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader',
// very current date
'Date': new Date().toUTCString()
},
url: configUrl
})
)
})
global.LibResilientPluginConstructors.set('resolve-config', ()=>{
return {
name: 'resolve-config',
description: 'Resolve with config data.',
version: '0.0.1',
fetch: resolveConfigFetch
}
})
try {
require("../service-worker.js");
} catch(e) {}
await self.trigger('install')
// this is silly but works, and is necessary because
// event.waitUntil() in the install event handler is not handled correctly in NodeJS
await new Promise(resolve => setTimeout(resolve, 100));
await self.trigger('activate')
// verify current config (the one from the pre-cached stale `config.json`)
expect(typeof self.LibResilientConfig).toEqual('object')
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}, {name: "resolve-config"}])
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache', 'resolve-config'])
expect(fetch).not.toHaveBeenCalled();
expect(resolveConfigFetch).toHaveBeenCalled();
// verify that the *new* config got cached
cdata = await caches
.open('v1')
.then((cache)=>{
return cache.match(configUrl)
})
.then((cresponse)=>{
return cresponse.json()
})
expect(cdata).toStrictEqual(newConfigData)
})
test("basic set-up: a stale cached valid config.json file gets used, no fetch happens; invalid config.json retrieved using the configured plugins is not cached", async () => {
self.LibResilientConfig = null
var configData = {loggedComponents: ['service-worker', 'cache', 'resolve-config'], plugins: [{name: "cache"}, {name: "resolve-config"}], defaultPluginTimeout: 5000}
var configUrl = '/config.json'
var configResponse = new Response(
new Blob(
[JSON.stringify(configData)],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader',
// very stale date
'Date': new Date(0).toUTCString()
},
url: configUrl
})
await caches
.open('v1')
.then((cache)=>{
return cache.put(configUrl, configResponse)
})
var newConfigData = {loggedComponentsInvalid: ['service-worker', 'resolve-config'], pluginsInvalid: [{name: "resolve-config"}], defaultPluginTimeoutInvalid: 2000}
let resolveConfigFetch = jest.fn((request, init)=>{
return Promise.resolve(
new Response(
new Blob(
[JSON.stringify(newConfigData)],
{type: "application/json"}
),
{
status: 200,
statusText: "OK",
headers: {
'ETag': 'TestingETagHeader',
// very current date
'Date': new Date().toUTCString()
},
url: configUrl
})
)
})
global.LibResilientPluginConstructors.set('resolve-config', ()=>{
return {
name: 'resolve-config',
description: 'Resolve with config data.',
version: '0.0.1',
fetch: resolveConfigFetch
}
})
try {
require("../service-worker.js");
} catch(e) {}
await self.trigger('install')
// this is silly but works, and is necessary because
// event.waitUntil() in the install event handler is not handled correctly in NodeJS
await new Promise(resolve => setTimeout(resolve, 100));
await self.trigger('activate')
// verify current config (the one from the pre-cached stale `config.json`)
expect(typeof self.LibResilientConfig).toEqual('object')
expect(self.LibResilientConfig.defaultPluginTimeout).toBe(5000)
expect(self.LibResilientConfig.plugins).toStrictEqual([{name: "cache"}, {name: "resolve-config"}])
expect(self.LibResilientConfig.loggedComponents).toStrictEqual(['service-worker', 'cache', 'resolve-config'])
expect(fetch).not.toHaveBeenCalled();
expect(resolveConfigFetch).toHaveBeenCalled();
// verify that the *new* config got cached
cdata = await caches
.open('v1')
.then((cache)=>{
return cache.match(configUrl)
})
.then((cresponse)=>{
return cresponse.json()
})
expect(cdata).toStrictEqual(configData)
})
test("fetching content should work", async () => {
self.LibResilientConfig = {
plugins: [{