赛普拉斯 - 为所有XHR请求添加自定义标头

问题描述 投票:-1回答:2

我注意到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>'.

我期待这种方法的任何工作解决方案或更好的解决方案。

javascript typescript testing cypress
2个回答
1
投票

为了让每个人都知道,我与Cypress的制造商沟通,并了解到即将发出的请求的存根正在开发中并且可以在 - https://github.com/cypress-io/cypress/issues/687下进行跟踪


0
投票

我想你正在寻找onRequest而不是onAnyRequestHere's the documentation for cy.server options

© www.soinside.com 2019 - 2024. All rights reserved.