Skip to content

Commit

Permalink
Add RWA cy.intercept use cases (#3742)
Browse files Browse the repository at this point in the history
* example of deleting a header from all outgoing requests with cy.intercept

* add throttle or delay response all incoming responses example for cy.intercept

* update to use throttle

Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com>

* update to use throttle

Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com>

* update to use throttle

Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com>

* remove conditional throttle

Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com>
  • Loading branch information
Kevin Old and amirrustam committed Mar 29, 2021
1 parent e7914e9 commit 1dc4e05
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions content/api/commands/intercept.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,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 @@ -663,6 +689,35 @@ 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'

// Throttle API responses for mobile testing to simulate real world conditions
if (isMobile()) {
cy.intercept({ url: 'http://localhost:3001', middleware: true }, (req) => {
req.on('response', (res) => {
// Throttle the response to 1 Mbps to simulate a mobile 3G connection
res.throttle(1000)
})
})
}
```
<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

0 comments on commit 1dc4e05

Please sign in to comment.