From 203c75d19429684e5807f2149b100a5f40499679 Mon Sep 17 00:00:00 2001 From: Mikhail Losev Date: Wed, 24 Feb 2021 11:33:12 +0300 Subject: [PATCH] Async predicate for request filter rules (#5937) * Async predicate for request filter rules * fix cors issue --- package.json | 1 - .../request-filter-rule-async-predicate.html | 22 ++++++++++++++ .../api/es-next/request-hooks/test.js | 4 +++ .../request-filter-rule-async-predicate.js | 23 +++++++++++++++ .../fixtures/regression/gh-3049/test.js | 9 ++++-- .../fixtures/regression/gh-3456/test.js | 2 +- .../fixtures/regression/gh-3929/test.js | 13 ++++++--- .../fixtures/regression/gh-5239/test.js | 26 ++++++++--------- .../gh-5239/testcafe-fixtures/index.js | 2 +- .../fixtures/regression/gh-5549/test.js | 9 ++++-- .../regression/hammerhead/gh-863/test.js | 29 ++++++++++--------- .../gh-863/testcafe-fixtures/index.js | 2 +- 12 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 test/functional/fixtures/api/es-next/request-hooks/pages/api/request-filter-rule-async-predicate.html create mode 100644 test/functional/fixtures/api/es-next/request-hooks/testcafe-fixtures/api/request-filter-rule-async-predicate.js diff --git a/package.json b/package.json index 2b3df775456..6b6cf0dae00 100644 --- a/package.json +++ b/package.json @@ -214,7 +214,6 @@ "run-sequence": "^1.2.2", "saucelabs-connector": "^0.2.0", "serve-static": "^1.10.0", - "server-destroy": "^1.0.1", "sinon": "^7.3.0", "stack-chain": "^2.0.0", "strip-ansi": "^3.0.0", diff --git a/test/functional/fixtures/api/es-next/request-hooks/pages/api/request-filter-rule-async-predicate.html b/test/functional/fixtures/api/es-next/request-hooks/pages/api/request-filter-rule-async-predicate.html new file mode 100644 index 00000000000..0d52b050ef6 --- /dev/null +++ b/test/functional/fixtures/api/es-next/request-hooks/pages/api/request-filter-rule-async-predicate.html @@ -0,0 +1,22 @@ + + + + + Async predicate for request filter rules + + +

Send request status

+ + + + diff --git a/test/functional/fixtures/api/es-next/request-hooks/test.js b/test/functional/fixtures/api/es-next/request-hooks/test.js index a922bfc369c..36a0b1ee5e6 100644 --- a/test/functional/fixtures/api/es-next/request-hooks/test.js +++ b/test/functional/fixtures/api/es-next/request-hooks/test.js @@ -62,5 +62,9 @@ describe('Request Hooks', () => { it("Test's request hooks should not override the fixture's request hooks (GH-4122)", () => { return runTests('./testcafe-fixtures/api/i4122.js', null, { only: 'chrome' }); }); + + it('Async predicate for request filter rules', () => { + return runTests('./testcafe-fixtures/api/request-filter-rule-async-predicate.js', null, { only: 'chrome' }); + }); }); }); diff --git a/test/functional/fixtures/api/es-next/request-hooks/testcafe-fixtures/api/request-filter-rule-async-predicate.js b/test/functional/fixtures/api/es-next/request-hooks/testcafe-fixtures/api/request-filter-rule-async-predicate.js new file mode 100644 index 00000000000..ead46d29725 --- /dev/null +++ b/test/functional/fixtures/api/es-next/request-hooks/testcafe-fixtures/api/request-filter-rule-async-predicate.js @@ -0,0 +1,23 @@ +import { RequestMock, Selector } from 'testcafe'; + +const getUrlPromise = new Promise(resolve => { + setTimeout(() => { + resolve('http://dummy-url.com/get'); + }, 2000); +}); + +const mock = RequestMock() + .onRequestTo(async req => { + return req.url === await getUrlPromise; + }) + .respond('Done!', 200, { 'access-control-allow-origin': '*' }); + +fixture `Fixture` + .requestHooks(mock) + .page('http://localhost:3000/fixtures/api/es-next/request-hooks/pages/api/request-filter-rule-async-predicate.html'); + +test('test', async t => { + await t + .click('button') + .expect(Selector('h2').textContent).eql('Done!'); +}); diff --git a/test/functional/fixtures/regression/gh-3049/test.js b/test/functional/fixtures/regression/gh-3049/test.js index 26c38a2be85..3aae17d914b 100644 --- a/test/functional/fixtures/regression/gh-3049/test.js +++ b/test/functional/fixtures/regression/gh-3049/test.js @@ -7,6 +7,8 @@ const isLocalChrome = config.useLocalBrowsers && config.browsers.some(browser => if (isLocalChrome) { describe('[Regression](GH-3049) - Should increase small browser window', function () { it('Run browser with minimal window size', function () { + let failedCount = 0; + return createTestCafe('127.0.0.1', 1335, 1336) .then(tc => { testCafe = tc; @@ -19,9 +21,12 @@ if (isLocalChrome) { .src(path.join(__dirname, './testcafe-fixtures/index.js')) .run(); }) - .then(failedCount => { - testCafe.close(); + .then(failed => { + failedCount = failed; + return testCafe.close(); + }) + .then(() => { if (failedCount) throw new Error('Error occurred'); }); diff --git a/test/functional/fixtures/regression/gh-3456/test.js b/test/functional/fixtures/regression/gh-3456/test.js index 81f6a7c7b5e..b43ff07012a 100644 --- a/test/functional/fixtures/regression/gh-3456/test.js +++ b/test/functional/fixtures/regression/gh-3456/test.js @@ -39,7 +39,7 @@ if (config.useLocalBrowsers) { return assertionHelper.removeScreenshotDir(); }) .then(() => { - testCafe.close(); + return testCafe.close(); }); }); }); diff --git a/test/functional/fixtures/regression/gh-3929/test.js b/test/functional/fixtures/regression/gh-3929/test.js index 7af0fc73d5f..ceca0742cb9 100644 --- a/test/functional/fixtures/regression/gh-3929/test.js +++ b/test/functional/fixtures/regression/gh-3929/test.js @@ -7,6 +7,8 @@ if (config.useLocalBrowsers && !config.useHeadlessBrowsers) { this.timeout(60000); it('Should reconnect with bad network conditions', function () { + let failedCount = 0; + return createTestCafe('127.0.0.1', 1335, 1336) .then(tc => { testCafe = tc; @@ -17,11 +19,14 @@ if (config.useLocalBrowsers && !config.useHeadlessBrowsers) { .src(path.join(__dirname, './testcafe-fixtures/index.js')) .run(); }) - .then(err => { - testCafe.close(); + .then(failed => { + failedCount = failed; - if (err) - throw new Error('Error occured'); + return testCafe.close(); + }) + .then(() => { + if (failedCount) + throw new Error('Error occurred'); }); }); }); diff --git a/test/functional/fixtures/regression/gh-5239/test.js b/test/functional/fixtures/regression/gh-5239/test.js index 3b955345def..39dc9767673 100644 --- a/test/functional/fixtures/regression/gh-5239/test.js +++ b/test/functional/fixtures/regression/gh-5239/test.js @@ -1,10 +1,9 @@ -const http = require('http'); -const path = require('path'); -const expect = require('chai').expect; -const config = require('../../../config'); -const createTestCafe = require('../../../../../lib'); -const enableDestroy = require('server-destroy'); - +const http = require('http'); +const path = require('path'); +const expect = require('chai').expect; +const config = require('../../../config'); +const createTestCafe = require('../../../../../lib'); +const { getFreePort } = require('endpoint-utils'); const { createReporter } = require('../../../utils/reporter'); const ERROR_RESPONSE_COUNT = 8; @@ -19,7 +18,7 @@ const customReporter = createReporter({ } }); -function createServer () { +async function createServer () { let requestCounter = 0; const requestListener = function (req, res) { @@ -40,10 +39,11 @@ function createServer () { }; const server = http.createServer(requestListener); + const port = await getFreePort(); - server.listen(1340); + process.env.TEST_SERVER_PORT = port.toString(); - enableDestroy(server); + server.listen(port); return server; } @@ -68,14 +68,14 @@ const isLocalChrome = config.useLocalBrowsers && config.browsers.some(browser => describe('[Regression](GH-5239)', function () { if (isLocalChrome) { - it('Should make multiple request for the page if the server does not respond', function () { + it('Should make multiple request for the page if the server does not respond', async function () { this.timeout(30000); - const server = createServer(); + const server = await createServer(); return run({ retryTestPages: true, browsers: 'chrome --headless', src: './testcafe-fixtures/index.js' }) .then(() => { - server.destroy(); + server.close(); }); }); } diff --git a/test/functional/fixtures/regression/gh-5239/testcafe-fixtures/index.js b/test/functional/fixtures/regression/gh-5239/testcafe-fixtures/index.js index 93e13133ad4..d5593f88450 100644 --- a/test/functional/fixtures/regression/gh-5239/testcafe-fixtures/index.js +++ b/test/functional/fixtures/regression/gh-5239/testcafe-fixtures/index.js @@ -1,7 +1,7 @@ import { Selector } from 'testcafe'; fixture `GH-5239 - Should make multiple request for the page if the server does not respond` - .page `http://localhost:1340`; + .page `http://localhost:${process.env.TEST_SERVER_PORT}`; test(`Click on the element`, async t => { await t.click(Selector('h1').withText('example')); diff --git a/test/functional/fixtures/regression/gh-5549/test.js b/test/functional/fixtures/regression/gh-5549/test.js index 5c84bc3e90b..4d8905c38d8 100644 --- a/test/functional/fixtures/regression/gh-5549/test.js +++ b/test/functional/fixtures/regression/gh-5549/test.js @@ -7,6 +7,8 @@ let testCafe = null; if (config.useLocalBrowsers) { describe(`[Regression](GH-5449) Should not crash if TestCafe is created via "createTestCafe('null')"`, () => { it(`[Regression](GH-5449) Should not crash if TestCafe is created via "createTestCafe('null')"`, () => { + let failedCount = 0; + return createTestCafe(null) .then(tc => { testCafe = tc; @@ -18,9 +20,12 @@ if (config.useLocalBrowsers) { .src(path.join(__dirname, './testcafe-fixtures/index.js')) .run(); }) - .then(failedCount => { - testCafe.close(); + .then(failed => { + failedCount = failed; + return testCafe.close(); + }) + .then(() => { if (failedCount) throw new Error('Error occurred'); }); diff --git a/test/functional/fixtures/regression/hammerhead/gh-863/test.js b/test/functional/fixtures/regression/hammerhead/gh-863/test.js index a294365820f..6e31c5d1809 100644 --- a/test/functional/fixtures/regression/hammerhead/gh-863/test.js +++ b/test/functional/fixtures/regression/hammerhead/gh-863/test.js @@ -1,10 +1,10 @@ -const http = require('http'); -const path = require('path'); -const fs = require('fs'); -const createTestCafe = require('../../../../../../lib'); -const config = require('../../../../config'); -const { expect } = require('chai'); -const enableDestroy = require('server-destroy'); +const http = require('http'); +const path = require('path'); +const fs = require('fs'); +const createTestCafe = require('../../../../../../lib'); +const config = require('../../../../config'); +const { expect } = require('chai'); +const { getFreePort } = require('endpoint-utils'); const resourceRequestCounter = { script1: 0, @@ -24,7 +24,7 @@ function addCachingHeader (res) { res.setHeader('cache-control', 'max-age=86400'); // Cache response 1 day } -function createServer () { +async function createServer () { const server = http.createServer((req, res) => { let content = ''; @@ -59,10 +59,11 @@ function createServer () { res.end(content); }); + const port = await getFreePort(); - server.listen(1340); + process.env.TEST_SERVER_PORT = port.toString(); - enableDestroy(server); + server.listen(port); return server; } @@ -87,12 +88,12 @@ const isLocalChrome = config.useLocalBrowsers && config.browsers.some(browser => if (isLocalChrome) { describe('Cache', () => { - it('Should cache resources between tests', () => { - const server = createServer(); + it('Should cache resources between tests', async () => { + const server = await createServer(); - return run({ src: './testcafe-fixtures/index.js', browser: 'chrome' }) + return run({ src: './testcafe-fixtures/index.js', browser: 'chrome:headless' }) .then(() => { - server.destroy(); + server.close(); expect(resourceRequestCounter.script1).eql(1); expect(resourceRequestCounter.script2).eql(1); diff --git a/test/functional/fixtures/regression/hammerhead/gh-863/testcafe-fixtures/index.js b/test/functional/fixtures/regression/hammerhead/gh-863/testcafe-fixtures/index.js index 7f34ef5f138..46bd7ca1578 100644 --- a/test/functional/fixtures/regression/hammerhead/gh-863/testcafe-fixtures/index.js +++ b/test/functional/fixtures/regression/hammerhead/gh-863/testcafe-fixtures/index.js @@ -5,7 +5,7 @@ const reload = ClientFunction(() => { }); fixture `Fixture` - .page('http://localhost:1340/'); + .page(`http://localhost:${process.env.TEST_SERVER_PORT}/`); async function assertTestElements () { await t