Skip to content

Commit

Permalink
Async predicate for request filter rules (#5937)
Browse files Browse the repository at this point in the history
* Async predicate for request filter rules

* fix cors issue
  • Loading branch information
miherlosev authored Feb 24, 2021
1 parent b572936 commit 203c75d
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 39 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Async predicate for request filter rules</title>
</head>
<body>
<h2>Send request status</h2>
<button onclick="sendRequest()">Send request</button>
<script>
function sendRequest() {
fetch('http://dummy-url.com/get')
.then(res => {
return res.text();
})
.then(text => {
document.querySelector('h2').textContent = text;
});
}
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions test/functional/fixtures/api/es-next/request-hooks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});
});
});
Original file line number Diff line number Diff line change
@@ -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!');
});
9 changes: 7 additions & 2 deletions test/functional/fixtures/regression/gh-3049/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
});
Expand Down
2 changes: 1 addition & 1 deletion test/functional/fixtures/regression/gh-3456/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (config.useLocalBrowsers) {
return assertionHelper.removeScreenshotDir();
})
.then(() => {
testCafe.close();
return testCafe.close();
});
});
});
Expand Down
13 changes: 9 additions & 4 deletions test/functional/fixtures/regression/gh-3929/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
});
});
});
Expand Down
26 changes: 13 additions & 13 deletions test/functional/fixtures/regression/gh-5239/test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +18,7 @@ const customReporter = createReporter({
}
});

function createServer () {
async function createServer () {
let requestCounter = 0;

const requestListener = function (req, res) {
Expand All @@ -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;
}
Expand All @@ -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();
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down
9 changes: 7 additions & 2 deletions test/functional/fixtures/regression/gh-5549/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
});
Expand Down
29 changes: 15 additions & 14 deletions test/functional/fixtures/regression/hammerhead/gh-863/test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = '';

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 203c75d

Please sign in to comment.