jeudi 28 février 2019

Cypress - Add custom header for all XHR requests

I notice that X-CSRFToken header is getting removed between tests, for all the XHR requests that triggered from application under test. I'm not sure to preserve this header, as I'm already preserving the Cookies through Cypress.Cookies.preserveOnce('sessionid', 'csrftoken')

Hence, I thought of appending the custom header X-CSRFToken to all the XHR requests from the application. Here is the script I used, where I'm fetching the csrftoken from cookies and setting to custom header.

cy.server({
   onAnyRequest: function(route, proxy) {
        proxy.xhr.setRequestHeader('X-CSRFToken', cy.getCookie('csrftoken'));
   }
})

Here, I'm getting the below error,

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>'.

I'm expecting any working solution to this approach or a better solution.

Aucun commentaire:

Enregistrer un commentaire