implemented query params normalization (ref. #27)

merge-requests/23/head
Michał 'rysiek' Woźniak 2023-10-07 17:08:05 +00:00
rodzic 428c6be365
commit dfd07201eb
2 zmienionych plików z 76 dodań i 1 usunięć

Wyświetl plik

@ -349,6 +349,7 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
@ -367,6 +368,7 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
@ -385,6 +387,7 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
@ -403,6 +406,7 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
@ -421,12 +425,32 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
it("should use default LibResilientConfig values when 'normalizeQueryParams' field in config.json contains an invalid value", async () => {
let mock_response_data = {
data: JSON.stringify({loggedComponents: ['service-worker', 'fetch'], plugins: [{name: "fetch"}], defaultPluginTimeout: 5000, normalizeQueryParams: "not a boolean"})
}
window.fetch = spy(window.getMockedFetch(mock_response_data))
await import("../../service-worker.js?" + window.test_id);
await self.dispatchEvent(new Event('install'))
await self.waitForSWInstall()
assertEquals(typeof self.LibResilientConfig, "object")
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 10000)
assertEquals(self.LibResilientConfig.plugins, [{name: "fetch"},{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'fetch', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, true)
assertSpyCalls(self.fetch, 1)
})
it("should use config values from a valid fetched config.json file, caching it", async () => {
let mock_response_data = {
data: JSON.stringify({loggedComponents: ['service-worker', 'cache'], plugins: [{name: "cache"}], defaultPluginTimeout: 5000})
data: JSON.stringify({loggedComponents: ['service-worker', 'cache'], plugins: [{name: "cache"}], defaultPluginTimeout: 5000, normalizeQueryParams: false})
}
window.fetch = spy(window.getMockedFetch(mock_response_data))
@ -438,6 +462,7 @@ describe('service-worker', async () => {
assertEquals(self.LibResilientConfig.defaultPluginTimeout, 5000)
assertEquals(self.LibResilientConfig.plugins, [{name: "cache"}])
assertEquals(self.LibResilientConfig.loggedComponents, ['service-worker', 'cache'])
assertEquals(self.LibResilientConfig.normalizeQueryParams, false)
assertSpyCalls(self.fetch, 1)
// cacheConfigJSON() is called asynchronously in the Service Worker,
@ -987,6 +1012,49 @@ describe('service-worker', async () => {
assertEquals(await response.json(), { test: "success" })
});
it("should normalize query params in requested URLs by default", async () => {
console.log(self.LibResilientConfig)
await import("../../service-worker.js?" + window.test_id);
await self.dispatchEvent(new Event('install'))
await self.waitForSWInstall()
let fetch_event = new FetchEvent('test.json?b=bbb&a=aaa&d=ddd&c=ccc')
window.dispatchEvent(fetch_event)
let response = await fetch_event.waitForResponse()
assertEquals(
fetch.calls[1].args[0],
"https://test.resilient.is/test.json?a=aaa&b=bbb&c=ccc&d=ddd"
)
})
it("should not normalize query params in requested URLs if 'normalizeQueryParams' is set to false", async () => {
self.LibResilientConfig = {
plugins: [{
name: 'fetch'
}],
loggedComponents: [
'service-worker'
],
normalizeQueryParams: false
}
await import("../../service-worker.js?" + window.test_id);
await self.dispatchEvent(new Event('install'))
await self.waitForSWInstall()
let fetch_event = new FetchEvent('test.json?b=bbb&a=aaa&d=ddd&c=ccc')
window.dispatchEvent(fetch_event)
let response = await fetch_event.waitForResponse()
assertEquals(
fetch.calls[1].args[0],
"https://test.resilient.is/test.json?b=bbb&a=aaa&d=ddd&c=ccc"
)
})
it("should pass the Request() init data to plugins", async () => {
self.LibResilientConfig = {
plugins: [{

Wyświetl plik

@ -98,6 +98,13 @@ let verifyConfigData = (cdata) => {
return false;
}
}
// normalizeQueryParams is optional
if ("normalizeQueryParams" in cdata) {
if (cdata.normalizeQueryParams !== true && cdata.normalizeQueryParams !=- false) {
self.log('service-worker', 'fetched config contains invalid "normalizeQueryParams" data (boolean expected)')
return false;
}
}
// we're good
return true;
}