-
Notifications
You must be signed in to change notification settings - Fork 88
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 Errors when Using cypress #100
Comments
Hi @derweili. In theory you shouldn't even need that Is it possible to share a repo that reproduces this issue, so I can explore it myself? If not, it would be very helpful if you could share a screenshot of the CORS request itself that fails. There must be a response shown in the browser network console somewhere causing this error that includes incorrect CORS headers. You might find https://httptoolkit.tech/will-it-cors/ useful to check what the headers should be for the request. |
@pimterry hi, thank your for your help. Here is my project repo: You can run |
What's the error in the console log? Find that's the only useful thing with cors errors |
@hanvyj Access to fetch at 'http://localhost:45454/start?port=3030' from origin 'http://localhost:65232' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'localhost:45454' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
|
Try set your origin to:
Instead of just
I just had to add cors to our mockttp to support |
The main issue from the requests shown here is that the origin is for the wrong server. Normally you'd do this by echoing the value in the Are you sure you're seeing the errors above in the example you shared though? I've just cloned that repo, run the commands above and the You will see the errors like you've shown above if you set an any incorrect
Yes, so credentials are the one case where Internally Mockttp just uses the cors package to implement most of this logic - the @hanvyj If you change that to If the problem is related to auth headers disallowing I think changing this default here would be a valid non-breaking change that might make CORS much easier for everybody, so do let me know if that works well for either of you, it'd be an easy change. |
@pimterry I just found the error. I was running browserstack local in the background and seems like it uses the same port After I quit browserstack local, the tests run successfull as you described in your comment. Maybe it would help if starting the |
Ah, that'll do it! Interesting. Glad you managed to chase that down.
Hmm, I'm not sure how we could handle this better... The admin server does actually throw an error here already, during For example, save the below to a file, and then run it with your node version of choice: const mockttp = require('mockttp');
mockttp.getAdminServer().start().then(async () => {
console.log('First server started');
await mockttp.getAdminServer().start();
console.log('Second server started (should never happen - port already used)');
}); If you run this with Node 14, it'll print an Is that different to the behaviour you're seeing? |
When trying to start the admin server twice, I get the error you mentioned:
But for some reason I did not get this error when the "browserstack local" app was running which also used the same port. |
Can you share the output from Each one should show a LISTEN line for port 45456, hopefully with some useful differences. I'm wondering if this is an issue with the two services binding on different interfaces, or an IPv4 vs IPv6 issue, or similar. That could cause this, and then requests from elsewhere would unpredictably end up at one or the other two servers, depending on how the client handles that, so Chrome could fail as you're seeing here. We can probably tweak the server setup to make it more explicitly fail in that case, if so. |
I'm trying to use MockTTP together with Cypress tests:
I setup HTTP mock as described in the Browser Setup Instructions
https://github.com/httptoolkit/mockttp/blob/main/docs/setup.md#browser-setup
I added a "startServer.js" script, which I start before running the tests:
My Cypress tests look like this:
I created a fresh ne project based on create-react-app.
Whe running the tests, I get a CORS Error caused by the
mockServer.start(3030)
script:Access to fetch at 'http://localhost:45454/start?port=3030' from origin 'http://localhost:59373' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'localhost:45454' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I'm using following dependencies:
CORS Settings.
I have already tried to set the CORS settings to configurations in various ways, but that did not work either:
Any Ideas what I could do?
I already checked #39
The text was updated successfully, but these errors were encountered: