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

fix: only update content-length header if one was present in spy #25920

Merged
merged 13 commits into from
Mar 27, 2023

Conversation

Bourg
Copy link
Contributor

@Bourg Bourg commented Feb 22, 2023

Additional details

Prior to this change, if cy.intercept is used to spy on a request with no modifications, a content-length header will be added to the outbound request regardless of whether one was present on the spied request. The code that does this is trying to automatically fix the content-length header in cases where the body was changed, but this behavior only makes sense if the original request included a content-length to update.

Steps to test

The issue links to a reproduction repo I created when I filed the issue. I want to add some automated test coverage, but the affected middleware does not currently have any coverage, so could use some guidance on whether / where I could test this.

How has the user experience changed?

No change to user experience

PR Tasks

@CLAassistant
Copy link

CLAassistant commented Feb 22, 2023

CLA assistant check
All committers have signed the CLA.

@Bourg Bourg changed the title fix - only update content-length header if one was present to begin with fix: only update content-length header if one was present to begin with Feb 22, 2023
Copy link
Contributor

@flotwig flotwig left a comment

Choose a reason for hiding this comment

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

@Bourg Looks good to me. To add tests, I'd recommend adding an integration test in this file: https://github.com/cypress-io/cypress/blob/47c787f5ae930fb0fa4b215a29330f5e362ed98b/packages/proxy/test/integration/net-stubbing.spec.ts

You can run it via yarn test-integration in packages/proxy.

@mjhenkes mjhenkes assigned mjhenkes and unassigned flotwig Feb 28, 2023
@nagash77 nagash77 assigned flotwig and mjhenkes and unassigned mjhenkes and flotwig Feb 28, 2023
@mjhenkes mjhenkes removed their assignment Mar 14, 2023
Copy link
Contributor

@AtofStryker AtofStryker left a comment

Choose a reason for hiding this comment

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

@Bourg this change also looks good on my end. Would you be able to add a test like mentioned above? If not let us know and we can come up with something!

@Bourg
Copy link
Contributor Author

Bourg commented Mar 20, 2023

@AtofStryker I made an attempt at adding a test but honestly, with my level of knowledge of the net-stubbing internals, setting up the correct state of the supertest to simulate this scenario proved a bit too much for me in the time I had to work on this. Some help on the test would be greatly appreciated.

@AtofStryker
Copy link
Contributor

AtofStryker commented Mar 20, 2023

@Bourg I completely understand. I should be able to take a look and add a test in the near future.

@cypress-io cypress-io deleted a comment from BradenM Mar 20, 2023
@AtofStryker
Copy link
Contributor

@Bourg I had some time to go ahead an add a small unit test in 04f03d6. It wasn't trivial 😅 . It isn't a full on integration test like mentioned above, but I think it gets us what we need and it lives in the same package as the code

@AtofStryker AtofStryker dismissed their stale review March 22, 2023 18:41

added code. can no longer review

@Bourg
Copy link
Contributor Author

Bourg commented Mar 23, 2023

Thank you @AtofStryker - I don't think I would have gotten there on the test implementation, I really appreciate it! I suppose now all that's needed is review.

cli/CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
cli/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Should a test be added here instead of the unit test (or maybe in addition to)?

Copy link
Contributor Author

@Bourg Bourg Mar 24, 2023

Choose a reason for hiding this comment

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

@mschile as far as I have been able to tell, reproducing this from inside a Cypress test requires that there actually be an API upstream that behaves differently depending on the content-length header - there's no way to test it in isolation that I've found. That was part of why this was so hard for me to identify this as a root cause of an issue I was having in the first place - everything looks untouched from inside the test runner.

The way we originally found this issue was that healthy API endpoints would start replying 400 when spied on by Cypress, and the reason turned out to be that AWS ELB - at least as configured in our case - did not expect the content-length header on a GET request and would reject the requests. For the minimal reproduction linked in the issue, I made a simple HTML document that makes a single fetch call to an Express server that echoes back the request headers. You would basically need to move all that machinery into those tests - the only point at which you can see the issue is from the perspective of the Express server, not Cypress.

I do not believe there is any event you can listen to in Cypress that gets you at the request headers of a spied request at the relevant point of the lifecycle. I agree it would be better if we could integration test this, but I think it may be more trouble than it's worth unless there's a more pure way to see the modified headers without an external server.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @Bourg, we do start an external server in the driver tests so we should be able to output the content-length header to verify it doesn't get inadvertently added.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to take a look into this in a few 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

@mschile I added a simple test in b05eda4. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good! You could even use the existing dump-headers request if you wanted. Something like:

it('does not calculate content-length on spied request if one does not exist on the initial request (if merging)', { retries: 0 }, function () {
  cy.intercept('/dump-headers', function (req) {
    // modify the intercepted request to trigger a request merge in net_stubbing
    req.headers['foo'] = 'bar'
    // send the modified request and skip any other
    // matching request handlers
    req.continue()
  })

  cy.visit('/dump-headers')

  // ensure the content-length header does not exist
  cy.contains('content-length').should('not.exist')
})

@emilyrohrbough emilyrohrbough changed the title fix: only update content-length header if one was present to begin with fix: only update content-length header if one was present in spy Mar 27, 2023
@emilyrohrbough emilyrohrbough merged commit 2452dee into cypress-io:develop Mar 27, 2023
@AtofStryker AtofStryker removed their assignment Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using cy.intercept to spy adds a "Content-Length: 0" request header
8 participants