question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubbing requests via cy.intercept doesnt work the same way as cy.route

See original GitHub issue

Current behavior

The request I want to stub looks like so with the old cy.route

cy.route({
			method: 'POST',
			url: '**/sessions',
			status: 200,
			response: {
				success: true,
			},
			headers: {
				'x-token': "token",
			},
		})

With this, I get the “x-token” returned as a header back to the client. alongside other headers.

connection: "keep-alive"
content-length: "16"
content-type: "application/json; charset=utf-8"
date: "Thu, 11 Feb 2021 12:58:34 GMT"
vary: "Accept-Encoding",
x-token: "token"

Trying out the new cy.intercept API,

cy.intercept('**/sessions', {
			body: { success: true },
			headers: {
				x-token: "token"
			},
		})

This is what I get back

content-type: "application/json"

Desired behavior

I expect the x-token to be part of the headers returned in the response. If Im missing anything, please let me know, thanks

Test code to reproduce

https://github.com/Tevinthuku/cypress-test-tiny https://codesandbox.io/s/modest-hofstadter-gl5xj?file=/src/App.js

Versions

Cypress v 6.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
flotwigcommented, May 4, 2021

@sainthkh of the 3 options you’ve listed, I think it would make sense to modify (1) slightly and go with it:

Automatically append 'Access-Control-Expose-Headers': '*' to all stubbed requests. It can sometimes cause false positive.

If we only apply it to stubbed requests, like we do with the other CORS headers, it should help avoid unnecessary modification of the header.

Another issue was opened about this same confusion, so it’s probably worth fixing: https://github.com/cypress-io/cypress/issues/14654

0reactions
cypress-bot[bot]commented, May 10, 2021

Released in 7.3.0.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

intercept - Cypress Documentation
Spy and stub network requests and responses. Tip: We recommend you read the Network Requests guide first. All intercepts are automatically cleared before....
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
The command cy.intercept can match requests using a substring, a minimatch, or a regular expression. By default, it intercepts requests matching ...
Read more >
cy.intercept() not stubbing API in Cypress - Stack Overflow
Supply a response body to stub in the matching route. In cy.intercept(method, url, routeHandler?) , routeHandler is a more complicated beast.
Read more >
cy.request vs. cy.intercept - DEV Community ‍ ‍
cy.intercept does not make a request, but rather "listens" to requests that occur on the network layer. If we "ask" Cypress to name ......
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
With minimatch, we don't care what the final ID number is, ... With .intercept() it is possible to work with fetch requests the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found