Skip to content

Commit

Permalink
Added a clear message about failed CORS validation requests (close #2482
Browse files Browse the repository at this point in the history
) (#2613)

* add clear message about failed CORS validation requests (close #2482)

* tmp

* formatting

* rename

* add condition

* fix lint
  • Loading branch information
miherlosev authored Jul 11, 2018
1 parent 4b5547a commit 3cd7b7d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 6 deletions.
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
8 changes: 6 additions & 2 deletions src/api/request-hooks/request-mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import RequestHook from './hook';
import { ResponseMock, RequestFilterRule } from 'testcafe-hammerhead';
import { ResponseMock, RequestFilterRule, SAME_ORIGIN_CHECK_FAILED_STATUS_CODE } 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,10 @@ class RequestMock extends RequestHook {
event.setMock(mock);
}

onResponse () {}
onResponse (event) {
if (event.statusCode === SAME_ORIGIN_CHECK_FAILED_STATUS_CODE)
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
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>
17 changes: 15 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,19 @@
const expect = require('chai').expect;

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
@@ -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');
});

0 comments on commit 3cd7b7d

Please sign in to comment.