我注意到X-CSRFToken
标头在测试之间被删除,用于从被测应用程序触发的所有XHR请求。我不确定保留这个标题,因为我已经通过Cypress.Cookies.preserveOnce('sessionid', 'csrftoken')
保存了Cookie
因此,我想到将自定义标头X-CSRFToken
附加到来自应用程序的所有XHR请求。这是我使用的脚本,我从cookie中获取csrftoken
并设置为自定义标题。
cy.server({
onAnyRequest: function(route, proxy) {
proxy.xhr.setRequestHeader('X-CSRFToken', cy.getCookie('csrftoken'));
}
})
在这里,我得到以下错误,
Argument of type '{ onAnyRequest: (route: any, proxy: any) => void; }' is not assignable to parameter of type 'Partial<ServerOptions>'.
Object literal may only specify known properties, and 'onAnyRequest' does not exist in type 'Partial<ServerOptions>'.
我期待这种方法的任何工作解决方案或更好的解决方案。
为了让每个人都知道,我与Cypress
的制造商沟通,并了解到即将发出的请求的存根正在开发中并且可以在 - https://github.com/cypress-io/cypress/issues/687下进行跟踪
我想你正在寻找onRequest
而不是onAnyRequest
。 Here's the documentation for cy.server
options