Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RWA cy.intercept use cases #3742

Merged
merged 7 commits into from
Mar 29, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions content/api/commands/intercept.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,32 @@ cy.wait('@headers')
.should('have.property', 'x-custom-headers', 'added by cy.intercept')
```

#### Add, modify or delete a header to all outgoing requests

You can add, modify or delete a header to all outgoing requests using a `beforeEach()` in the `cypress/support/index.js` file

```ts
// Code from Real World App (RWA)
// cypress/support/index.ts
import './commands'

beforeEach(() => {
cy.intercept(
{ url: 'http://localhost:3001', middleware: true },
// Delete 'if-none-match' header from all outgoing requests
(req) => delete req.headers['if-none-match']
)
})
```

<Alert type="info">

##### <Icon name="graduation-cap"></Icon> Real World Example

Clone the <Icon name="github"></Icon> [Real World App (RWA)](https://github.com/cypress-io/cypress-realworld-app) and refer to the [cypress/support/index.ts](https://github.com/cypress-io/cypress-realworld-app/blob/develop/cypress/support/index.ts) file for a working example.

</Alert>

#### Dynamically stubbing a response

You can use the `req.reply()` function to dynamically control the response to a request.
Expand Down Expand Up @@ -660,6 +686,38 @@ The available functions on `res` are:
}
```

#### Throttle or delay response all incoming responses

You can throttle or delay all incoming responses using a `beforeEach()` in the `cypress/support/index.js` file

```ts
// Code from Real World App (RWA)
// cypress/support/index.ts
import { isMobile } from './utils'
import './commands'

// Delay API responses for mobile testing to simulate real world conditions
kevinold marked this conversation as resolved.
Show resolved Hide resolved
if (isMobile()) {
cy.intercept({ url: 'http://localhost:3001', middleware: true }, (req) => {
req.on('response', (res) => {
// For all request except the /login
if (req.url !== 'http://localhost:3001/login') {
kevinold marked this conversation as resolved.
Show resolved Hide resolved
// Delay the response
kevinold marked this conversation as resolved.
Show resolved Hide resolved
res.delay(2500)
kevinold marked this conversation as resolved.
Show resolved Hide resolved
}
})
})
}
```

<Alert type="info">

##### <Icon name="graduation-cap"></Icon> Real World Example

Clone the <Icon name="github"></Icon> [Real World App (RWA)](https://github.com/cypress-io/cypress-realworld-app) and refer to the [cypress/support/index.ts](https://github.com/cypress-io/cypress-realworld-app/blob/develop/cypress/support/index.ts) file for a working example.

</Alert>

## History

| Version | Changes |
Expand Down