diff --git a/test/workbox-strategies/integration/test-cacheOnly.js b/test/workbox-strategies/integration/test-cacheOnly.js new file mode 100644 index 000000000..caa37747b --- /dev/null +++ b/test/workbox-strategies/integration/test-cacheOnly.js @@ -0,0 +1,73 @@ +const expect = require('chai').expect; +const seleniumAssistant = require('selenium-assistant'); + +describe(`[workbox-strategies] CacheOnly Requests`, function() { + let webdriver; + let testServerAddress = global.__workbox.server.getAddress(); + + beforeEach(async function() { + if (webdriver) { + await seleniumAssistant.killWebDriver(webdriver); + webdriver = null; + } + + global.__workbox.server.reset(); + + // Allow async functions 10s to complete + webdriver = await global.__workbox.seleniumBrowser.getSeleniumDriver(); + webdriver.manage().timeouts().setScriptTimeout(10 * 1000); + }); + + after(async function() { + if (webdriver) { + await seleniumAssistant.killWebDriver(webdriver); + } + }); + + const activateSW = async (swFile) => { + const error = await webdriver.executeAsyncScript((swFile, cb) => { + navigator.serviceWorker.register(swFile) + .then(() => { + navigator.serviceWorker.addEventListener('controllerchange', () => { + if (navigator.serviceWorker.controller.scriptURL.endsWith(swFile)) { + cb(); + } + }); + }) + .catch((err) => { + cb(err); + }); + }, swFile); + if (error) { + throw error; + } + }; + + it(`should respond with cached and non-cached entry`, async function() { + const testingURl = `${testServerAddress}/test/workbox-strategies/static/cache-only/`; + const SW_URL = `${testingURl}sw.js`; + + // Load the page and wait for the first service worker to register and activate. + await webdriver.get(testingURl); + + // Register the first service worker. + await activateSW(SW_URL); + + let response = await webdriver.executeAsyncScript((cb) => { + fetch(new URL(`/CacheOnly/InCache/`, location).href) + .then((response) => response.text()) + .then((responseBody) => cb(responseBody)) + .catch((err) => cb(err.message)); + }); + expect(response).to.equal('Cached'); + + // For a non-cached entry, the fetch should throw an error with + // a message defined by the browser. + response = await webdriver.executeAsyncScript((cb) => { + fetch(new URL(`/CacheOnly/NotInCache/`, location).href) + .then(() => cb(null)) + .catch((err) => cb(err.message)); + }); + expect(response).to.exist; + }); +}); diff --git a/test/workbox-strategies/static/cache-only/index.html b/test/workbox-strategies/static/cache-only/index.html new file mode 100644 index 000000000..2fa6d40ea --- /dev/null +++ b/test/workbox-strategies/static/cache-only/index.html @@ -0,0 +1,7 @@ + +
+ + +You need to manually register sw.js
+ + diff --git a/test/workbox-strategies/static/cache-only/sw.js b/test/workbox-strategies/static/cache-only/sw.js new file mode 100644 index 000000000..1408f7851 --- /dev/null +++ b/test/workbox-strategies/static/cache-only/sw.js @@ -0,0 +1,15 @@ +importScripts('/__WORKBOX/buildFile/workbox-sw'); + +/* globals workbox */ + +workbox.routing.registerRoute( + new RegExp('/CacheOnly/.*/'), + new workbox.strategies.CacheOnly(), +); + +self.addEventListener('install', (event) => event.waitUntil( + caches.open(workbox.core.cacheNames.runtime) + .then((cache) => cache.put('/CacheOnly/InCache/', new Response('Cached'))) + .then(() => self.skipWaiting())) +); +self.addEventListener('activate', (event) => event.waitUntil(self.clients.claim()));