CORS policy error when trying to setRequestHeader in Cypress test

北城余情 提交于 2020-08-10 20:03:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!