简体   繁体   中英

How to manipulate any route & request type with cy.intercept in Cypres.io?

Previously toCypress 6.0.0 , I was using the cy.server() to set a request header on any request like so:

Cypress.Commands.add('setHeaderToken', () => {
  cy.server({
    onAnyRequest: (route, proxy) => {
      proxy.xhr.setRequestHeader('<CUSTOM-HEADER-HERE>', '<header-value-here>')
    },
  })
});

Then calling it in the beforeEach hook like so:

beforeEach(() => {
  cy.setHeaderToken();
});

Now, I'm usingCypress 6.5.0 & I'm trying to accomplish the same functionality with cy.intercept method like so:

Cypress.Commands.add('setHeaderToken', () => {
  cy.intercept('/*', (req) => {
    req.headers['<CUSTOM-HEADER-HERE>'] = '<header-value-here>'
  });
});

This is not working, despite if no HTTP method is defined Cypress will match all requests by default .

您应该使用***/*而不是/* ,前导斜杠不适用于 minimatch。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM