-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CORS issue with XMLHttpRequest AJAX call #1680
Comments
Continuing on this, when using: nock(/localhost:910[01]/).get('/available', {
reqheaders: {
'Access-Control-Allow-Origin': '*'
}
}).reply(500); It simple times out and never returns anything. |
Hi, @ls-guillaume-lambert . Specify So, you will need to change your code as follows. nock(/localhost:910[01]/).get('/available').reply(500, 'reply body', {
'Access-Control-Allow-Origin': '*'
}); Also I've created the example of testcafe with nock integration. |
Yup this does work, but when using the nock(/localhost:910[01]/).get('/available').reply((uri, requestBody) => {
return [500, 'Error', {
'Access-Control-Allow-Origin': '*'
}];
); This surely has to do with https://github.com/node-nock/nock/blob/master/lib/interceptor.js#L61 |
To put this into context, in our e2e test, we need to change the response when a variable is set. Also note that this a polled XMLHttpRequest: let isAppInstalled = false;
nock(/localhost:910[01]/).persist().get('/available').reply((uri, requestBody) => {
return [isAppInstalled ? 200, 500, isAppInstalled ? 'Ok' : 'Error', {
'Access-Control-Allow-Origin': '*'
}];
);
test('simple', async t => {
await t.click('#someSelector');
// should return 500 with nock
isAppInstalled = true;
// should return 200 with nock
await t.expect(Selector('#installed').exists).ok();
}); So we suspect it has something to do with the way |
'222' is an internal http status code. Testcafe performs validation of a cross-domain request by itself (see https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/request-pipeline/xhr/same-origin-policy.js). If a request does not pass CORS rules, the proxy responds with 222 status code. In your case, '222' status code means that nock's request mock was configured wrongly and it does not pass CORS rules. You can also use a working example from the #1680 (comment). |
Thanks for the response, I will look at debugging this. If it fails, do you see other ways of achieving we want as mentioned in #1680 (comment)? |
Found a way to do it by using test('Mock XHR Request changes', async t => {
const xhrNotInstalledInterceptor = nock(/localhost:910[01]/).persist().get('/available');
const xhrNotInstalledScope = xhrNotInstalledInterceptor.reply(500, 'Error', {
'Access-Control-Allow-Origin': '*',
});
// Do some expects here
// ...
nock.removeInterceptor(xhrNotInstalledInterceptor);
const xhrInstalledInterceptor = nock(/localhost:910[01]/).persist().get('/available');
const xhrInstalledScope = xhrInstalledInterceptor.reply(200, 'Success', {
'Access-Control-Allow-Origin': '*',
});
// Do more expects here
// ...
}); |
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow. |
Are you requesting a feature or reporting a bug?
Reporting a bug (coming from
hammerhead.js:8
)What is the current behavior?
An AJAX call with XMLHttpRequest to another domain causes CORS issues and returns a
222
status with no response. We are usingnock
to mock responses.What is the expected behavior?
An AJAX call with XMLHttpRequest should return a response even when its from another domain.
How would you reproduce the current behavior (if this is a bug)?
Create a test that goes to a page which does an XMLHttpRequest on an external domain.
We use the following code:
To put this into context, we have an URL we are polling (https://localhost:9101/available) to get some information, and this is what we want to mock. The code aboves returns a
222
code fromhammerhead.js
.Specify your
The text was updated successfully, but these errors were encountered: