From 370672b584d424a169a7db95816680ab485d0bc6 Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Thu, 25 Mar 2021 16:58:07 -0500 Subject: [PATCH 1/6] example of deleting a header from all outgoing requests with cy.intercept --- content/api/commands/intercept.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index 6657282938..223b8e730c 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -484,6 +484,31 @@ 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 +// cypress/support/index.js +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'] + ) +}) +``` + + + +##### Real World Example + +Clone the [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. + + + #### Dynamically stubbing a response You can use the `req.reply()` function to dynamically control the response to a request. From 73ddf5ffed806f73405e86507b2d57716699a77f Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Thu, 25 Mar 2021 17:12:31 -0500 Subject: [PATCH 2/6] add throttle or delay response all incoming responses example for cy.intercept --- content/api/commands/intercept.md | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index 223b8e730c..5c124e33d0 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -489,7 +489,8 @@ cy.wait('@headers') You can add, modify or delete a header to all outgoing requests using a `beforeEach()` in the `cypress/support/index.js` file ```ts -// cypress/support/index.js +// Code from Real World App (RWA) +// cypress/support/index.ts import './commands' beforeEach(() => { @@ -685,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 +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') { + // Delay the response + res.delay(2500) + } + }) + }) +} +``` + + + +##### Real World Example + +Clone the [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. + + + ## History | Version | Changes | From 3eb4701286214b85699e57277302d1d03bd5b7c5 Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Mon, 29 Mar 2021 11:05:09 -0500 Subject: [PATCH 3/6] update to use throttle Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com> --- content/api/commands/intercept.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index 521c44edd6..d93d1d3351 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -706,7 +706,7 @@ if (isMobile()) { // For all request except the /login if (req.url !== 'http://localhost:3001/login') { // Delay the response - res.delay(2500) + res.throttle(1000) } }) }) From 3954b3aac6a0f2ed6f1a38e569e4cb9236a91262 Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Mon, 29 Mar 2021 11:05:23 -0500 Subject: [PATCH 4/6] update to use throttle Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com> --- content/api/commands/intercept.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index d93d1d3351..cae434fa6a 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -705,7 +705,7 @@ if (isMobile()) { req.on('response', (res) => { // For all request except the /login if (req.url !== 'http://localhost:3001/login') { - // Delay the response + // Throttle the response to 1 Mbps to simulate a mobile 3G connection res.throttle(1000) } }) From 21ad8663ffca008a2cad99dca1533b265682d794 Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Mon, 29 Mar 2021 11:05:50 -0500 Subject: [PATCH 5/6] update to use throttle Co-authored-by: Amir Rustamzadeh <334337+amirrustam@users.noreply.github.com> --- content/api/commands/intercept.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index cae434fa6a..8f8d8f72fc 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -699,7 +699,7 @@ You can throttle or delay all incoming responses using a `beforeEach()` in the ` import { isMobile } from './utils' import './commands' -// Delay API responses for mobile testing to simulate real world conditions +// 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) => { From 9ea7c4f0c025e1a1a66aef19be57804c049f9f1c Mon Sep 17 00:00:00 2001 From: Kevin Old Date: Mon, 29 Mar 2021 11:08:11 -0500 Subject: [PATCH 6/6] remove conditional throttle --- content/api/commands/intercept.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/content/api/commands/intercept.md b/content/api/commands/intercept.md index 8f8d8f72fc..5d65d84800 100644 --- a/content/api/commands/intercept.md +++ b/content/api/commands/intercept.md @@ -703,11 +703,8 @@ import './commands' 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') { - // Throttle the response to 1 Mbps to simulate a mobile 3G connection - res.throttle(1000) - } + // Throttle the response to 1 Mbps to simulate a mobile 3G connection + res.throttle(1000) }) }) }