问题
Im my Cypress test I want to set x-forwarded-for header to force the browser to load a page with a specific locale. However, while doing so, I'm getting errors as following in the console:
Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: Request header field x-forwarded-for is not allowed by Access-Control-Allow-Headers in preflight response.
In my cypress.json file, I have set:
"env":{
"chromeWebSecurity": false
}
Which is supposed to prevent this from happening, but it doesn't.
My test code looks like below:
describe('Login', () => {
it('Test', () => {
cy.server({
onAnyRequest: function (route, proxy) {
proxy.xhr.setRequestHeader(
"x-forwarded-for",
"5.44.16.0"
);
},
});
cy.visit(Cypress.env('baseUrl'))
cy.get('[ng-click="TopNavigationComponentCtrl.loginClick()"] > .ng-binding').click()
})
})
So it's a very simple test which is supposed only to navigate to the webpage and click Login button. The page is loaded with a correct locale, but it doesn't click a button and throws those CORS errors in the console. Also, I get an error like below from Cypress:
来源:https://stackoverflow.com/questions/62513920/cors-policy-error-when-trying-to-setrequestheader-in-cypress-test