From f925158733109dc547a95ee314b1c8780b40943e Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 3 Apr 2018 15:21:08 -0700 Subject: [PATCH] test: add test for page.waitForFunction with script CSP (#2305) This patch adds a test to fixate page.waitForFunction behavior for pages with CSP. References #1229. --- test/frame.spec.js | 9 ++++++++- test/server/SimpleServer.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test/frame.spec.js b/test/frame.spec.js index 9769b4fe..d9a32fdd 100644 --- a/test/frame.spec.js +++ b/test/frame.spec.js @@ -110,6 +110,13 @@ module.exports.addTests = function({testRunner, expect}) { await page.evaluate(() => window.__FOO = 'hit'); await watchdog; }); + xit('should work with strict CSP policy', async({page, server}) => { + server.setCSP('/empty.html', 'script-src ' + server.PREFIX); + await page.goto(server.EMPTY_PAGE); + const watchdog = page.waitForFunction(() => window.__FOO === 'hit', {polling: 'raf'}); + await page.evaluate(() => window.__FOO = 'hit'); + await watchdog; + }); it('should throw on bad polling value', async({page, server}) => { let error = null; try { @@ -469,4 +476,4 @@ module.exports.addTests = function({testRunner, expect}) { expect(page.frames()[2].parentFrame()).toBe(page.mainFrame()); }); }); -}; \ No newline at end of file +}; diff --git a/test/server/SimpleServer.js b/test/server/SimpleServer.js index 09990283..bbd14538 100644 --- a/test/server/SimpleServer.js +++ b/test/server/SimpleServer.js @@ -78,6 +78,8 @@ class SimpleServer { this._routes = new Map(); /** @type {!Map} */ this._auths = new Map(); + /** @type {!Map} */ + this._csp = new Map(); /** @type {!Map} */ this._requestSubscribers = new Map(); } @@ -109,6 +111,14 @@ class SimpleServer { this._auths.set(path, {username, password}); } + /** + * @param {string} path + * @param {string} csp + */ + setCSP(path, csp) { + this._csp.set(path, csp); + } + async stop() { this.reset(); for (const socket of this._sockets) @@ -158,6 +168,7 @@ class SimpleServer { reset() { this._routes.clear(); this._auths.clear(); + this._csp.clear(); const error = new Error('Static Server has been reset'); for (const subscriber of this._requestSubscribers.values()) subscriber[rejectSymbol].call(null, error); @@ -214,6 +225,8 @@ class SimpleServer { } else { response.setHeader('Cache-Control', 'no-cache, no-store'); } + if (this._csp.has(pathName)) + response.setHeader('Content-Security-Policy', this._csp.get(pathName)); fs.readFile(filePath, function(err, data) { if (err) {