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

cy.intercept() breaks multipart form submissions #14527

Closed
javitonino opened this issue Jan 13, 2021 · 10 comments
Closed

cy.intercept() breaks multipart form submissions #14527

javitonino opened this issue Jan 13, 2021 · 10 comments
Assignees
Labels

Comments

@javitonino
Copy link

Current behavior

Since upgrading to Cypress 6.2.1, multipart form submissions no longer work when they are intercept()ed. This seems to be a regression introduced by #9359

Desired behavior

I can intercept multipart uploads, as I was able to do in Cypress 6.0.0 - 6.2.0

Test code to reproduce

As a minimal reproducing program, I used https://github.com/AlexCSR/cypress-intercept-buffer-bug (from the #9359 ticket). In my system, that tests works fine in 6.2.0, but breaks in 6.2.1. It seems to be the opposite of what people on that ticket where experiencing.

If I inspect network traffic using that test in 6.2.1:

  • The body of the browser -> cypress proxy request is correct
  • The body of the cypress proxy -> origin server is {} (no multipart boundaries included)

Versions

Broken in Cypress 6.2.1, works in Cypress 6.0.0 - 6.2.0
Tested with Node 15.4.0, 10.20.1 and 12.18.2 running on Linux (Arch Linux & Ubuntu)
Chromium 87 (although that seems irrelevant, I can reproduce by sending HTTP requests to the Cypress proxy using curl)

Maybe @AlexCSR can comment on what versions they use? Since I'm using their code to reproduce and get the opposite results.

@bahmutov
Copy link
Contributor

I am not sure I understand the steps to reproduce it. The references image problem should be broken in 6.2.0 and fixed in 6.2.1. You report the opposite. Can you add screenshots or better yet create a reproducible example?

We have a recipe that shows checking a multipart form here: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__intercept see "form-spec.js" file. Maybe you can use it as the starting point?

@bahmutov bahmutov added stage: needs information Not enough info to reproduce the issue topic: cy.intercept() labels Jan 13, 2021
@samtsai
Copy link
Contributor

samtsai commented Jan 13, 2021

This might be related as well #14535

@javitonino
Copy link
Author

javitonino commented Jan 13, 2021

Yeah, I'm reporting the exact opposite than that other ticket. I even took the reproducing example from there to do the testing and I see the complete opposite: working in 6.2.0 and broken in 6.2.1.

I cloned the example from that ticket and added some Dockerfiles in order to try to isolate this as much as possible. https://github.com/javitonino/cypress-intercept-buffer-bug

This is what I see:

git clone https://github.com/javitonino/cypress-intercept-buffer-bug.git
cd cypress-intercept-buffer-bug
docker build . -f Dockerfile6.2.0 -t cypress-intercept-6.2.0
docker build . -f Dockerfile6.2.1 -t cypress-intercept-6.2.1
docker run cypress-intercept-6.2.0

[...]
    ✔  All specs passed!                        00:03        3        3        -        -        - 
docker run cypress-intercept-6.2.1

[...]
    ✖  1 of 1 failed (100%)                     00:10        3        1        2        -        - 

@jennifer-shehane
Copy link
Member

Is this issue localized to Docker? I'm trying to recreate the issue from the repo you provided, but I see the provided tests always failing in Cypress 6.2.0, 6.2.1, and 6.3.0.

@javitonino
Copy link
Author

javitonino commented Jan 20, 2021

Is this issue localized to Docker?

No, it's not. I just used Docker to try to isolate as many factors as possible but I can reproduce without it. In my case, by running the repo outside docker I see the same behaviour (works in 6.2.0, breaks in 6.2.1). I just tested 6.3.0 and it breaks (same as 6.2.1) for me.

Are you running the built in upload server (node server.js). It's executed by the node run run task, but if you are running it manually, you may have skipped it.

@javitonino
Copy link
Author

So, I debugged a bit what is happening here, and the problem seems to be the following:

  1. The cypress server receives a request. Since it's multipart, it sets the body to the buffer: https://github.com/cypress-io/cypress/blob/v6.2.1/packages/net-stubbing/lib/server/intercept-request.ts#L116
  2. It's sent to the driver
  3. The driver passes it to the user code. In my case, I do nothing.
  4. The driver sends the modified request back to the cypress server. In doing that, it converts objects to JSON https://github.com/cypress-io/cypress/blob/v6.2.1/packages/driver/src/cy/net-stubbing/events/request-received.ts#L142-L144. Converting a buffer with JSON.stringify results in a string like {}
  5. The server receives the new requests and sends it to the origin. The body is {} which is incorrect

This only happens when you intercept() a multipart/form-data request and do not modify the request and let it go to the origin.

It did not happen in 6.2.0 because the body was always converted to string before sending it to the driver and, since strings are not objects, the driver did not run JSON.stringify before returning the modified request to the server.

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs information Not enough info to reproduce the issue labels Jan 21, 2021
@jennifer-shehane jennifer-shehane added stage: needs information Not enough info to reproduce the issue type: bug and removed stage: needs information Not enough info to reproduce the issue labels Jan 21, 2021
@yoavniran
Copy link

Also had this problem upgrading from cypress 4.x. It certainly made upgrading to 6.5 a lot more difficult than expected.

I wrote a small command that makes it easy to work with multipart requests and make assertions on the body,

IMO it should have been handled by Cypress, but until then I'm using: https://www.npmjs.com/package/cypress-intercept-formdata

@ppiyush13
Copy link

We also impacted by this issue. Anyone find any workaround ?

@jonaspu
Copy link

jonaspu commented Oct 28, 2021

We are also impacted by this issue, to the point of having to revert back to an older Version of Cypress.

We have to intercept every single request in order to add an Authentication header.

Therefore this makes Cypress 6.2.1+ unusable.

@flotwig
Copy link
Contributor

flotwig commented Feb 21, 2022

This appears to not be an issue in the newest release of Cypress (the repro no longer fails). Please open a new issue if you are encountering issues with multipart and cy.intercept().

@flotwig flotwig closed this as completed Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants