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

feat: change cy.intercept override behavior #14543

Merged
merged 35 commits into from
Mar 26, 2021
Merged

Conversation

flotwig
Copy link
Contributor

@flotwig flotwig commented Jan 13, 2021

User facing changelog

  • BREAKING CHANGE: Request handlers supplied to cy.intercept are now matched starting with the most-recently-defined request interceptor. This allows users to override request handlers by calling cy.intercept again. This matches the previous behavior that was standard in cy.route.
  • There are now new events on the req object for cy.intercept: before:response, response, after:response.
  • Response handlers (supplied via event handlers or via req.continue(cb)) supplied to cy.intercept will be called in reverse order until res.send is called or until there are no more response handlers.
  • A new option, middleware, has been added to the RouteMatcher type. If true, the supplied request handler will be called before any non-middleware request handlers.
  • A new function, req.continue(cb), is available on request objects yielded by cy.intercept. It is functionally the same as req.reply(cb) - the request will stop propagating to other event handlers, will be sent outgoing, and the response will be handled by cb.
  • cy.intercept can now be called with a new overload: cy.intercept(url, routeMatcher, handler). routeMatcher will be merged with url, and the handler is required.
  • Fixed an issue where the response.body would be incorrect after stubbing a response using res.send({ fixture }) in a req.continue callback or a before:response or response handler.

Additional details

See changes to net_stubbing_spec for new + updated tests.

How has the user experience changed?

  • New RouteMatcher option: middleware: boolean
    • Handlers with middleware: true will be called in the order they are defined and chained.
    • With middleware: true, only dynamic handlers are supported - makes no sense to support cy.intercept({ middleware: true }, staticResponse)
  • req now emits events (regardless of middleware setting)
  • Events on req:
    interface InterceptionEvents {
    /**
    * Emitted before `response` and before any `req.continue` handlers.
    * Modifications to `res` will be applied to the incoming response.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    */
    on(eventName: 'before:response', cb: HttpResponseInterceptor): Interception
    /**
    * Emitted after `before:response` and after any `req.continue` handlers - before the response is sent to the browser.
    * Modifications to `res` will be applied to the incoming response.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    */
    on(eventName: 'response', cb: HttpResponseInterceptor): Interception
    /**
    * Emitted once the response to a request has finished sending to the browser.
    * Modifications to `res` have no impact.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    */
    on(eventName: 'after:response', cb: (res: CyHttpMessages.IncomingResponse) => void | Promise<void>): Interception
    }
  • req.continue(cb) has been added. req.reply(cb) still works.
  • A third overload of cy.intercept has been added, to allow users to more easily do things like cy.intercept('/url', { middleware: true }, ...) to set middleware, etc:
    /**
    * Use `cy.intercept()` to stub and intercept HTTP requests and responses.
    *
    * @see https://on.cypress.io/intercept
    *
    * @example
    * cy.intercept('/fruits', { middleware: true }, (req) => { ... })
    *
    * @param mergeRouteMatcher Additional route matcher options to merge with `url`. Typically used for middleware.
    */
    intercept(url: string, mergeRouteMatcher: Omit<RouteMatcherOptions, 'url'>, response: RouteHandler): Chainable<null>
    /**

PR Tasks

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 13, 2021

Thanks for taking the time to open a PR!

@cypress
Copy link

cypress bot commented Jan 13, 2021



Test summary

9497 0 111 3Flakiness 0


Run details

Project cypress
Status Passed
Commit ae33091
Started Mar 26, 2021 5:21 PM
Ended Mar 26, 2021 5:34 PM
Duration 12:06 💡
OS Linux Debian - 10.8
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@flotwig flotwig changed the base branch from develop to 7.0-release March 3, 2021 19:27
@kevinold
Copy link
Contributor

@flotwig I think I have found a regression with this patch against the RWA with request modifications not being honored in some way.

In v6.6.0, when I remove the if-none-match header from a request (https://github.com/cypress-io/cypress-realworld-app/pull/676/files#diff-553f62c7d670ed3411a618d14b703ed95485dac575be4993025c3d263d099751R16-R18)

cy.intercept("GET", "/notifications", (req) => {
  delete req.headers["if-none-match"];
}).as("getNotifications");

Subsequent cy.wait's do not return a 304 from the endpoint

Screen Shot 2021-03-12 at 11 02 11 AM

Under this branch, the response is 304 instead of the results:

Screen Shot 2021-03-12 at 11 11 13 AM

@kevinold
Copy link
Contributor

@flotwig flotwig marked this pull request as ready for review March 24, 2021 22:06
Copy link
Contributor

@chrisbreiding chrisbreiding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@flotwig
Copy link
Contributor Author

flotwig commented Mar 26, 2021

The 2 failing stages are currently failing in 7.0-release as well, merging.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 5, 2021

Released in 7.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 5, 2021
@flotwig flotwig deleted the cy-intercept-overrides branch January 24, 2022 18:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: breaking change Requires a new major release version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants