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(net-stubbing): experimental full network mocking support #4176

Merged
merged 204 commits into from
Aug 31, 2020

Conversation

flotwig
Copy link
Contributor

@flotwig flotwig commented May 10, 2019

User facing changelog

  • With experimentalNetworkMocking enabled, added support for cy.route2, which can intercept any HTTP traffic.

Additional details

Objectives

  • Dynamic modification of HTTP requests from driver
  • Dynamic modification of HTTP responses from driver
    • Delay
    • Throttle (kbps)
  • Static modification of HTTP responses from driver
  • Aliasing & waiting

Todo List

  • Merge proxy-refactor: Refactor proxy into own package, implement middleware pattern #5136
  • Add timeout for res.send, req.reply to be called before test is autofailed
  • Fail test instead of warning when doing strange stuff (next-next, etc...)
  • Handle errors in responses
  • Handle errors thrown in any callback, ensure backend errors are handled too
  • Need a better way to handle type exporting

Internal Events

event:name from to payload description
cy.route() Events
route:added driver server AddRouteFrame
HTTPController Events
http:request:received server driver CyIncomingRequest fired when HTTP proxy receives headers + body of request
http:request:continue driver server CyIncomingRequest fired when driver is done modifying request and wishes to pass control back to the proxy
http:response:received server driver CyIncomingResponse fired when a response is received and the driver has a req.reply callback registered
http:response:continue driver server CyIncomingResponse fired when driver is done modifying response/driver callback completes, passes control back to proxy
http:request:complete server driver fired when a response has been sent completely by the server to an intercepted request

PR Tasks

@mikeastock
Copy link

When is this targeted to be released?

@cypress
Copy link

cypress bot commented Aug 26, 2019



Test summary

8341 0 124 3


Run details

Project cypress
Status Passed
Commit cc357f9
Started Aug 27, 2020 7:19 PM
Ended Aug 27, 2020 7:30 PM
Duration 11:04 💡
OS Linux Debian - 10.2
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

@Mouvedia
Copy link

200+ commits PRs
I pity the reviewers.

Saibamen added a commit to Saibamen/cypress that referenced this pull request Aug 31, 2020
@Saibamen Saibamen mentioned this pull request Aug 31, 2020
flotwig pushed a commit that referenced this pull request Aug 31, 2020
@vivekbwaj
Copy link

When is the new version going to be released with full network stubbing support?

@dipjyotimetia
Copy link

Looking forward for the new releases having the full network layer stubbing.

@DylanSp
Copy link

DylanSp commented Sep 1, 2020

Release 5.1.0 has the experimental support from this PR.

@kumar303
Copy link

kumar303 commented Sep 2, 2020

I just came to say THANK YOU @flotwig and team. I could not get fetch stubbing to work with any of the previous workarounds but in 5.1.0 it is working flawlessly with cy.route2().

@redReno

This comment has been minimized.

@Andarist
Copy link
Contributor

We had a pretty hacky (but very much working) setup for mocking JSONP and OAuth requests and also some hacks for dynamic responses. That's all gone with the new API - thank you very much.

Not only it has new capabilities but is IMHO actually easier to use. Great work 🚀

@redReno
Copy link

redReno commented Oct 31, 2020

This is working perfect for us. Fetch requests were blocking our tests but now it's not an issue. Thanks!

@hiran47
Copy link

hiran47 commented Nov 20, 2020

This is a lifesaver and thank you so much for this 🙏

Few questions out of curiosity:

  1. What's your plan in promoting this from experimental feature to a core feature?
  2. Will route2 become route eventually?
  3. Any tentative date/timeline for above?

@flotwig
Copy link
Contributor Author

flotwig commented Nov 20, 2020

1. What's your plan in promoting this from `experimental feature` to a `core feature`?

@hkar9113 This is going to be promoted from "experimental" in the next major release of Cypress, 6.0.0. #9185

2. Will `route2` become `route` eventually?

With 6.0.0, route2 will be renamed to cy.http. #9182 We thought about replacing cy.route but determined it would cause more harm than good (old documentation would be confusing and it would break backwards-compatibility for folks who still want to use the XHR-based stubbing)

With 6.0.0, we will begin to encourage users to use cy.http over cy.route. Once all major use cases/bugs are ironed out with cy.http, cy.route will be cut out of the Cypress core and moved to a plugin for those who wish to use it moving forward, but this will not be until (at the earliest) Cypress 7.0.0.

3. Any tentative date/timeline for above?

Cypress 6.0.0 is scheduled to come out next week.

@TheNumbaCruncha
Copy link

TheNumbaCruncha commented Apr 27, 2021

Hi guys , sorry if i'm on the wrong thread here but basically what i want to do is stub every outgoing request with header that has a dynamic parameter where the test stored in the cypress\integration folder is setting the parameter

Basically we have this functionality today for a constant header on this page https://docs.cypress.io/api/commands/intercept#Intercepting-a-request

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

so something like
beforeEach(() => {
cy.intercept(
{ url: 'http://localhost:3001/**', middleware: true },
(req, dynamic parameter) => //do something with dynamic parameter
)
})

@flotwig
Copy link
Contributor Author

flotwig commented Apr 27, 2021

@TheNumbaCruncha yes, you can do that with this feature. I'm not sure what your "dynamic parameter" is - this seems like a JS question, maybe try StackOverflow or GitHub Discussions, this is a pull request comment thread.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Apr 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.