|
| 1 | +'use strict'; |
| 2 | +const {describe, specify} = require('mocha-sugar-free'); |
| 3 | +require('chai').use(require('chai-subset')); |
| 4 | +const {assert: {ok, match}} = require('chai'); |
| 5 | + |
| 6 | +const {runScriptFromFunction, testServerPort} = require('../utilities/integrationTest'); |
| 7 | + |
| 8 | +describe('integration/requestBlocking', {timeout: 60000, slow: 10000}, () => { |
| 9 | + specify('Argument validation', async () => { |
| 10 | + /* eslint-disable no-undef */ |
| 11 | + const result = await runScriptFromFunction(async () => { |
| 12 | + 'Openrunner-Script: v1'; |
| 13 | + const assert = await include('assert'); |
| 14 | + const requestBlocking = await include('requestBlocking'); |
| 15 | + |
| 16 | + const result = await requestBlocking.block(123).catch(err => err); |
| 17 | + assert.instanceOf(result, Error); |
| 18 | + assert.match(result.message, /invalid.*pattern/i); |
| 19 | + }); |
| 20 | + /* eslint-enable no-undef */ |
| 21 | + |
| 22 | + if (result.error) { |
| 23 | + throw result.error; |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + specify('Blocking of the main document request', async () => { |
| 28 | + /* eslint-disable no-undef */ |
| 29 | + const result = await runScriptFromFunction(async () => { |
| 30 | + 'Openrunner-Script: v1'; |
| 31 | + const tabs = await include('tabs'); |
| 32 | + const requestBlocking = await include('requestBlocking'); |
| 33 | + const tab = await tabs.create(); |
| 34 | + |
| 35 | + await requestBlocking.block('http://localhost/headers/html'); |
| 36 | + |
| 37 | + await tab.navigate(injected.url, {timeout: '2s'}); |
| 38 | + }, {url: `http://localhost:${testServerPort()}/headers/html`}); |
| 39 | + /* eslint-enable no-undef */ |
| 40 | + |
| 41 | + ok(result.error, 'expected an error'); |
| 42 | + match(result.error.message, /Navigating.*localhost:\d+\/headers\/html.*time.*out/i); |
| 43 | + }); |
| 44 | + |
| 45 | + specify('Blocking of a resource', async () => { |
| 46 | + /* eslint-disable no-undef */ |
| 47 | + const result = await runScriptFromFunction(async () => { |
| 48 | + 'Openrunner-Script: v1'; |
| 49 | + const tabs = await include('tabs'); |
| 50 | + await include('assert'); |
| 51 | + await include('wait'); |
| 52 | + const requestBlocking = await include('requestBlocking'); |
| 53 | + const tab = await tabs.create(); |
| 54 | + |
| 55 | + await requestBlocking.block(['http://localhost/static/foo.jpg*']); |
| 56 | + |
| 57 | + await tab.navigate(injected.url, {timeout: '2s'}); |
| 58 | + await tab.wait(async () => { |
| 59 | + await wait.documentComplete(); |
| 60 | + }); |
| 61 | + await tab.run(async () => { |
| 62 | + const images = [...document.querySelectorAll('img')]; |
| 63 | + assert.lengthOf(images, 3); |
| 64 | + |
| 65 | + for (const image of images) { |
| 66 | + assert.isTrue(image.complete); |
| 67 | + assert.strictEqual(image.naturalWidth, 0); |
| 68 | + assert.strictEqual(image.naturalHeight, 0); |
| 69 | + // if the image has loaded properly, its dimensions are 906x775 |
| 70 | + // if the image is very small, a broken image indicator is being displayed |
| 71 | + assert.isBelow(image.clientWidth, 100); |
| 72 | + assert.isBelow(image.clientHeight, 100); |
| 73 | + } |
| 74 | + }); |
| 75 | + }, {url: `http://localhost:${testServerPort()}/static/static.html`}); |
| 76 | + /* eslint-enable no-undef */ |
| 77 | + |
| 78 | + if (result.error) { |
| 79 | + throw result.error; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + specify('Blocking of a fetch request', async () => { |
| 84 | + /* eslint-disable no-undef */ |
| 85 | + const result = await runScriptFromFunction(async () => { |
| 86 | + 'Openrunner-Script: v1'; |
| 87 | + const tabs = await include('tabs'); |
| 88 | + await include('assert'); |
| 89 | + const requestBlocking = await include('requestBlocking'); |
| 90 | + const tab = await tabs.create(); |
| 91 | + |
| 92 | + await requestBlocking.block('http://localhost/headers/json'); |
| 93 | + |
| 94 | + await tab.navigate(injected.url, {timeout: '2s'}); |
| 95 | + |
| 96 | + await tab.run(async () => { |
| 97 | + const fetchPromise = content.fetch(`http://${location.host}/headers/json`, {cache: 'no-cache'}); |
| 98 | + await assert.isRejected(fetchPromise, /NetworkError.*fetch/i); |
| 99 | + }); |
| 100 | + }, {url: `http://localhost:${testServerPort()}/headers/html`}); |
| 101 | + /* eslint-enable no-undef */ |
| 102 | + |
| 103 | + if (result.error) { |
| 104 | + throw result.error; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + specify('Temporary blocking of a fetch request', async () => { |
| 109 | + /* eslint-disable no-undef */ |
| 110 | + const result = await runScriptFromFunction(async () => { |
| 111 | + 'Openrunner-Script: v1'; |
| 112 | + const tabs = await include('tabs'); |
| 113 | + await include('assert'); |
| 114 | + const requestBlocking = await include('requestBlocking'); |
| 115 | + const tab = await tabs.create(); |
| 116 | + |
| 117 | + await tab.navigate(injected.url, {timeout: '2s'}); |
| 118 | + |
| 119 | + await requestBlocking.block('http://localhost/headers/json', async () => { |
| 120 | + await tab.run(async () => { |
| 121 | + const fetchPromise = content.fetch(`http://${location.host}/headers/json`, {cache: 'no-cache'}); |
| 122 | + await assert.isRejected(fetchPromise, /NetworkError.*fetch/i); |
| 123 | + }); |
| 124 | + }); |
| 125 | + await tab.run(async () => { |
| 126 | + const response = await content.fetch(`http://${location.host}/headers/json`, {cache: 'no-cache'}); |
| 127 | + assert.ok(response.ok); |
| 128 | + }); |
| 129 | + }, {url: `http://localhost:${testServerPort()}/headers/html`}); |
| 130 | + /* eslint-enable no-undef */ |
| 131 | + |
| 132 | + if (result.error) { |
| 133 | + throw result.error; |
| 134 | + } |
| 135 | + }); |
| 136 | +}); |
0 commit comments