cors - 在 cypress 测试中请求路由时出现 CORS 错误
问题描述
我想模拟我的 SPA 完成的一些 API 调用。因此,我正在使用 cypress.JS 并使用以下测试检查如何做到这一点。
it("Then it works", () => {
axios.defaults.baseURL = 'http://localhost';
cy.server()
cy.route("GET", "http://localhost/users/", true)
axios.get("/users/").then(response => {
console.log("received response: " + response)
expect(response.body).to.equal(true)
}).catch(error => console.log(error))
})
它不起作用我收到错误“从源 'http://localhost:8080' 访问 XMLHttpRequest 在 'http://localhost:8080' 已被 CORS 策略阻止:没有 'Access-Control-Allow-Origin ' 请求的资源上存在标头。"
有没有办法在测试期间防止这个错误?我不明白为什么赛普拉斯会以可能发生此错误的方式处理这个简单的测试。
解决方案
在您cypress.json
设置chromeWebSecurity
为false
.
{
"chromeWebSecurity": false
}
从此处的 cypress 文档中,设置chromeWebSecurity
为false
允许您执行以下操作:
- 显示不安全的内容
- 导航到没有跨域错误的任何超级域
- 访问嵌入在您的应用程序中的跨源 iframe。
推荐阅读
- forms - Vue.js 重用表单验证规则
- ssl - 解密由于 JMeter https 请求(例如通过 Wireshark)生成的“加密警报”数据包
- dockerfile - 为什么运行时会出现 chown:在 `/usr/local/hadoop/logs' 之后缺少操作数
- git - 有没有办法从数据目录中恢复 gitlab 存储库,如下所示?
- node.js - 过程或函数 insert_azienda 指定了太多参数 (Node.js)
- hyperledger-fabric - 如何创建频道更新提案以及如何签名
- android - 如何执行方法onClick?
- jwt - 使用 java 库 auth0-java 为 JitsiMeet 生成 jwt 令牌
- excel - 当 sheet1 中的新数据输入更改另一个 sheet2 中的单元格值时自动弹出
- python - 我如何给python输入一个值,我只是不知道和理解