From 79ea89d0cab9148f9dfc87ba811a56ef1eb6be33 Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Sat, 5 Oct 2019 10:39:18 +0200 Subject: [PATCH] Introduce `--no-request-interception` (#207) This works around a puppeteer issue when running scripts within a web worker. See https://github.com/GoogleChrome/puppeteer/issues/4208. --- lib/args.js | 6 ++++-- lib/chromium.js | 25 +++++++++++++++---------- test/args-test.js | 7 +++++++ test/chromium-test.js | 10 ++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/args.js b/lib/args.js index 722d8c65..67da90dc 100644 --- a/lib/args.js +++ b/lib/args.js @@ -25,7 +25,8 @@ var defaults = { timeout: '2000', yields: '0', colors: null, - 'async-polling': true + 'async-polling': true, + 'request-interception': true }; function args(argv) { @@ -37,7 +38,8 @@ function args(argv) { 'mocha-path', 'web-security'], boolean: ['help', 'version', 'watch', 'cover', 'node', 'wd', 'debug', 'invert', 'recursive', 'colors', 'ignore-ssl-errors', 'browser-field', - 'commondir', 'allow-chrome-as-root', 'async-polling', 'dumpio'], + 'commondir', 'allow-chrome-as-root', 'async-polling', 'dumpio', + 'request-interception'], alias: { help: 'h', version: 'v', diff --git a/lib/chromium.js b/lib/chromium.js index da064bc7..50937e7d 100644 --- a/lib/chromium.js +++ b/lib/chromium.js @@ -175,16 +175,21 @@ module.exports = function (b, opts) { output.write('\n'); }); - // Prevent image and font requests from causing error messages: - page.setRequestInterception(true); - page.on('request', function (request) { - if (IGNORE_RESOURCE_TYPES[request.resourceType()]) { - // Respond with 200 ok or a warning is logged: - request.respond({ status: 200 }); - } else { - request.continue(); - } - }); + // This switch can be used to work around an issue in puppeteer when + // running scripts within a web worker. See + // https://github.com/GoogleChrome/puppeteer/issues/4208 + if (opts['request-interception']) { + // Prevent image and font requests from causing error messages: + page.setRequestInterception(true); + page.on('request', function (request) { + if (IGNORE_RESOURCE_TYPES[request.resourceType()]) { + // Respond with 200 ok or a warning is logged: + request.respond({ status: 200 }); + } else { + request.continue(); + } + }); + } // We need a "real" web page loaded from a URL, or things like // localStorage are disabled. diff --git a/test/args-test.js b/test/args-test.js index f7bc63fc..2be75fc8 100644 --- a/test/args-test.js +++ b/test/args-test.js @@ -30,6 +30,7 @@ describe('args', function () { assert.equal(opts['ignore-ssl-errors'], false); assert.equal(opts['browser-field'], true); assert.equal(opts.commondir, true); + assert.equal(opts['request-interception'], true); }); it('parses --reporter', function () { @@ -323,6 +324,12 @@ describe('args', function () { assert.equal(opts['web-security'], false); }); + it('parses --no-request-interception', function () { + var opts = args(['--no-request-interception']); + + assert.equal(opts['request-interception'], false); + }); + it('fails with invert but no grep option', function (done) { run('passes', ['--invert'], function (code, stdout) { assert.equal(code, 1); diff --git a/test/chromium-test.js b/test/chromium-test.js index 9fee91de..69070650 100644 --- a/test/chromium-test.js +++ b/test/chromium-test.js @@ -453,6 +453,16 @@ describe('chromium', function () { }); }); + it('allows to turn off request interception', function (done) { + run('requests', ['-R', 'tap', '--no-request-interception'], + function (code, stdout, stderr) { + assert.equal(stderr, + 'Failed to load resource: net::ERR_FILE_NOT_FOUND\n'); + assert.equal(code, 0); + done(); + }); + }); + context('https-server with a port value given', function () { var server; var port;