Skip to content

Commit

Permalink
add clear message about failed CORS validation requests (close DevExp…
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jul 11, 2018
1 parent 4b5547a commit 31ddb8b
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ gulp.step('test-functional-local-run', function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.localBrowsers);
});

gulp.task('test-functional-local', gulp.series('build', 'test-functional-local-run'));
gulp.task('test-functional-local', gulp.series(/*'build',*/ 'test-functional-local-run'));

gulp.step('test-functional-local-ie-run', function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.localBrowsersIE);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"source-map-support": "^0.5.5",
"strip-bom": "^2.0.0",
"testcafe-browser-tools": "1.6.4",
"testcafe-hammerhead": "14.2.0",
"testcafe-hammerhead": "14.2.1",
"testcafe-legacy-api": "3.1.7",
"testcafe-reporter-json": "^2.1.0",
"testcafe-reporter-list": "^2.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/api/request-hooks/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default class RequestHook {
this.requestFilterRules = this._prepareRequestFilterRules(requestFilterRules);
this._instantiatedRequestFilterRules = [];
this.responseEventConfigureOpts = responseEventConfigureOpts;

this.warningLog = null;
}

_prepareRequestFilterRules (rules) {
Expand Down
5 changes: 4 additions & 1 deletion src/api/request-hooks/request-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import RequestHook from './hook';
import { ResponseMock, RequestFilterRule } from 'testcafe-hammerhead';
import { APIError } from '../../errors/runtime';
import MESSAGE from '../../errors/runtime/message';
import WARNING_MESSAGE from '../../notifications/warning-message';

class RequestMock extends RequestHook {
constructor () {
Expand All @@ -17,7 +18,9 @@ class RequestMock extends RequestHook {
event.setMock(mock);
}

onResponse () {}
onResponse (event) {
this.warningLog.addWarning(WARNING_MESSAGE.requestMockCORSValidationFailed, RequestMock.name, event._requestFilterRule);
}

// API
onRequestTo (requestFilterRuleInit) {
Expand Down
3 changes: 2 additions & 1 deletion src/notifications/warning-message.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export default class TestRun extends EventEmitter {

this.quarantine = null;

this.warningLog = warningLog;

this.injectable.scripts.push('/testcafe-core.js');
this.injectable.scripts.push('/testcafe-ui.js');
this.injectable.scripts.push('/testcafe-automation.js');
Expand Down Expand Up @@ -150,6 +152,8 @@ export default class TestRun extends EventEmitter {
}

_initRequestHook (hook) {
hook.warningLog = this.warningLog;

hook._instantiateRequestFilterRules();
hook._instantiatedRequestFilterRules.forEach(rule => {
this.session.addRequestEventListeners(rule, {
Expand All @@ -161,6 +165,8 @@ export default class TestRun extends EventEmitter {
}

_disposeRequestHook (hook) {
hook.warningLog = null;

hook._instantiatedRequestFilterRules.forEach(rule => {
this.session.removeRequestEventListeners(rule);
});
Expand Down
4 changes: 2 additions & 2 deletions test/functional/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ testingEnvironments[testingEnvironmentNames.localBrowsers] = {
platform: 'Windows 10',
browserName: 'chrome',
alias: 'chrome'
},
}/*,
{
platform: 'Windows 10',
browserName: 'ie',
Expand All @@ -112,7 +112,7 @@ testingEnvironments[testingEnvironmentNames.localBrowsers] = {
platform: 'Windows 10',
browserName: 'firefox',
alias: 'firefox'
}
}*/
]
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="btnSendFetch">Send fetch request</button>
<span>Request status:</span><span id="requestStatusText">Not send</span>
<script>
var btnSendFetch = document.getElementById('btnSendFetch');

btnSendFetch.addEventListener('click', function () {
fetch('http://dummy-url.com/get')
.then(res => {
return res.text();
})
.then(() => {
document.getElementById('requestStatusText').textContent = 'Sent';
});
});
</script>
</body>
</html>
15 changes: 13 additions & 2 deletions test/functional/fixtures/api/es-next/request-hooks/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
describe('Request Hooks', () => {
it('RequestMock', () => {
return runTests('./testcafe-fixtures/request-mock.js', 'Basic', { only: 'chrome' });
describe('RequestMock', () => {
it('Basic', () => {
return runTests('./testcafe-fixtures/request-mock/basic.js', 'Basic', { only: 'chrome' });
});

it('Request failed the CORS validation', () => {
return runTests('./testcafe-fixtures/request-mock/failed-cors-validation.js', 'Failed CORS validation', { only: 'chrome' })
.then(() => {
expect(testReport.warnings).eql([
'RequestMock: CORS validation failed for a request specified as { url: "http://dummy-url.com/get" }'
]);
});
});
});

describe('RequestLogger', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const testPageMarkup = `
</html>
`;

const requestMock = RequestMock()
const basic = RequestMock()
.onRequestTo('http://dummy-url.com')
.respond(testPageMarkup)
.onRequestTo('http://dummy-url.com/get')
Expand All @@ -32,7 +32,7 @@ const requestMock = RequestMock()
fixture `Basic`;

test
.requestHooks(requestMock)
.requestHooks(basic)
('Basic', async t => {
await t
.navigateTo('http://dummy-url.com')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Selector, RequestMock } from 'testcafe';

const mock = RequestMock()
.onRequestTo('http://dummy-url.com/get')
.respond({ prop: 'value' }, 200, { 'not-specify-cors-headers': true });

fixture `Failed CORS validation`
.page('http://localhost:3000/fixtures/api/es-next/request-hooks/pages/failed-cors-validation.html')
.requestHooks(mock);

test('Failed CORS validation', async t => {
await t
.click('#btnSendFetch')
.expect(Selector('#requestStatusText').textContent).eql('Sent');
});
4 changes: 2 additions & 2 deletions test/functional/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ after(function () {

// TODO: Run takeScreenshot tests first because other tests heavily impact them
if (config.useLocalBrowsers && !config.isLegacyEnvironment) {
require('./fixtures/api/es-next/take-screenshot/test');
require('./fixtures/screenshots-on-fails/test');
//require('./fixtures/api/es-next/take-screenshot/test');
//require('./fixtures/screenshots-on-fails/test');
}

0 comments on commit 31ddb8b

Please sign in to comment.