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

[7.0.1]Bug: cy.intercept POST terminates cypress process #15901

Closed
njh7799 opened this issue Apr 9, 2021 · 11 comments · Fixed by #15946
Closed

[7.0.1]Bug: cy.intercept POST terminates cypress process #15901

njh7799 opened this issue Apr 9, 2021 · 11 comments · Fixed by #15946

Comments

@njh7799
Copy link

njh7799 commented Apr 9, 2021

Simplified reproduction

https://github.com/bahmutov/cypress-15901-bug

Current behavior

presettings

  • The code includes POST request with Chineese words inside it's body (Chineese words should be long enough)
<script>
    fetch('http://localhost:5000/api/sample', {
    method: 'POST', body: JSON.stringify({first: '東京都新東', second: '東京都新'})})
</script>
  • use cy.intercept in the test with the API above
cy.intercept('POST', 'http://localhost:5000/api/sample');

Result

If I run Cypress, it's process is terminated with error messages
cypress-intercept-bug

TypeError [ERR_INVALID_ARG_TYPE] [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received type number (123)
    at write_ (_http_outgoing.js:696:11)
    at ClientRequest.write (_http_outgoing.js:661:15)
    at Request.write (/Users/user/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:1496:27)
    at /Users/user/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:546:20
    at Array.forEach (<anonymous>:null:null)
    at end (/Users/user/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:545:23)
    at Immediate._onImmediate (/Users/user/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:578:7)
    at processImmediate (internal/timers.js:461:21)
 {
  code: 'ERR_INVALID_ARG_TYPE'
}

image

Desired behavior

Cypress test process must not be terminated.

Test code to reproduce

https://github.com/njh7799/cypress7.0-intercept-post-bug

Versions

Cypress version: 7.0.1
OS: MAC Big Sir

@bahmutov
Copy link
Contributor

bahmutov commented Apr 9, 2021

Interesting, confirmed it crashes. Also, it crashes when the page posts

JSON.stringify({first: '東京都新東'})

but does not crash if we post

JSON.stringify({first: 'hello'})

@bahmutov bahmutov added type: bug topic: cy.intercept() stage: ready for work The issue is reproducible and in scope labels Apr 9, 2021
@bahmutov
Copy link
Contributor

bahmutov commented Apr 9, 2021

Works in 6.8.0, crashes in v7

@bahmutov bahmutov added the v7.0.0 🐛 Issue present since 7.0.0 label Apr 9, 2021
@flotwig flotwig changed the title [7.0.1]Bug: cy.intercept POST terminates cypress process [7.0.0]Bug: cy.intercept response stub with unicode body terminates cypress process Apr 9, 2021
@flotwig flotwig changed the title [7.0.0]Bug: cy.intercept response stub with unicode body terminates cypress process [7.0.1]Bug: cy.intercept POST terminates cypress process Apr 9, 2021
@edant92
Copy link

edant92 commented Apr 12, 2021

Just to confirm, I also have this issue on Ubuntu 18.04 in 7.0.0 and 7.0.1

Edit: using Latin (english) characters

@donalmurtagh
Copy link

I don't think the Chinese characters are relevant. I observed the same problem when making a HTTP POST containing only Latin (English) characters. The problem occurs after upgrading from 6.8.0 to 7.0.1. I've described the problem in a bit more detail on Stackoverflow

@bahmutov
Copy link
Contributor

bahmutov commented Apr 12, 2021

Comparing the debug messages during crash vs non-crash

Crash

  engine:socket flushing buffer to transport +0ms
  engine:ws writing "451-["net:event","before:request",{"eventId":"event2","subscription":{"eventName":"before:request","await":false,"routeId":"1618242756221-4"},"requestId":"interceptedRequest1","data":{"headers":{"host":"localhost:5000","proxy-connection":"keep-alive","content-length":"27","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0-development Chrome/89.0.4389.90 Electron/12.0.2 Safari/537.36","content-type":"text/plain;charset=UTF-8","accept":"*/*","origin":"http://localhost:64365","sec-fetch-site":"same-site","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:64365/","accept-encoding":"gzip, deflate, br","accept-language":"en-US"},"url":"http://localhost:5000/api/sample","method":"POST","httpVersion":"1.1","body":{"_placeholder":true,"num":0}}}]" +81ms
  engine:socket sending packet "message" ({"first":"東京都新東"}) +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'RedirectToClientRouteIfUnloaded' } +5ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'EndRequestsToBlockedHosts' } +1ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'StripUnsupportedAcceptEncoding' } +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'MaybeSetBasicAuthHeaders' } +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'SendRequestOutgoing' } +1ms
  engine:socket flushing buffer to transport +5ms
  engine:ws writing "{"first":"東京都新東"}" +5ms


piping request to get the body
got the req.body <Buffer 7b 22 66 69 72 73 74 22 3a 22 e6 9d b1 e4 ba ac e9 83 bd e6 96 b0 e6 9d b1 22 7d>
  cypress:net-stubbing:server:intercept-request req.body encoding binary

Notice the "body":{"_placeholder":true,"num":0}}}] part

No crash

  engine:socket sending packet "message" (2["net:event","before:request",{"eventId":"event2","subscription":{"eventName":"before:request","await":false,"routeId":"1618242780023-4"},"requestId":"interceptedRequest1","data":{"headers":{"host":"localhost:5000","proxy-connection":"keep-alive","content-length":"17","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0-development Chrome/89.0.4389.90 Electron/12.0.2 Safari/537.36","content-type":"text/plain;charset=UTF-8","accept":"*/*","origin":"http://localhost:64384","sec-fetch-site":"same-site","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:64384/","accept-encoding":"gzip, deflate, br","accept-language":"en-US"},"url":"http://localhost:5000/api/sample","method":"POST","httpVersion":"1.1","body":"{\"first\":\"hello\"}"}}]) +82ms
  engine:socket flushing buffer to transport +1ms
  engine:ws writing "42["net:event","before:request",{"eventId":"event2","subscription":{"eventName":"before:request","await":false,"routeId":"1618242780023-4"},"requestId":"interceptedRequest1","data":{"headers":{"host":"localhost:5000","proxy-connection":"keep-alive","content-length":"17","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0-development Chrome/89.0.4389.90 Electron/12.0.2 Safari/537.36","content-type":"text/plain;charset=UTF-8","accept":"*/*","origin":"http://localhost:64384","sec-fetch-site":"same-site","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://localhost:64384/","accept-encoding":"gzip, deflate, br","accept-language":"en-US"},"url":"http://localhost:5000/api/sample","method":"POST","httpVersion":"1.1","body":"{\"first\":\"hello\"}"}}]" +83ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'RedirectToClientRouteIfUnloaded' } +5ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'EndRequestsToBlockedHosts' } +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'StripUnsupportedAcceptEncoding' } +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'MaybeSetBasicAuthHeaders' } +0ms
  cypress:proxy:http Running middleware { stage: 'IncomingRequest', middlewareName: 'SendRequestOutgoing' } +1ms
  cypress:network:agent addRequest called { isHttps: false, href: 'http://localhost:5000/api/sample' } +110ms
  cypress:network:agent got family { family: 4, href: 'http://localhost:5000/api/sample' } +0ms
  engine:ws received "42["mocha","pass",{"id":"r3","order":1,"title":"dd","state":"passed","body":"() => {\n    cy.intercept('POST', 'http://localhost:5000/api/sample'); // cy.visit('http://localhost:5000')\n\n    cy.visit('public/index.html');\n  }","type":"test","duration":202,"wallClockStartedAt":"2021-04-12T15:52:59.992Z","timings":{"lifecycle":26,"test":{"fnDuration":175,"afterFnDuration":1}},"file":null,"invocationDetails":{"function":"Suite.eval","fileUrl":"http://localhost:64384/__cypress/tests?p=cypress/integration/spec.js","originalFile":"webpack:///cypress/integration/spec.js","relativeFile":"cypress/integration/spec.js","absoluteFile":"/Users/gleb/git/cypress7.0-intercept-post-bug/cypress/integration/spec.js","line":2,"column":5,"whitespace":"    "},"final":true,"currentRetry":0,"retries":0}]" +17ms

piping request to get the body
got the req.body <Buffer 7b 22 66 69 72 73 74 22 3a 22 68 65 6c 6c 6f 22 7d>
  cypress:net-stubbing:server:intercept-request req.body encoding utf8 +1ms

Notice the same message with "body":"{\"first\":\"hello\"}"}

Running with debug logs DEBUG=engine:socket,cypress:proxy*,cypress:network*,engine:ws

@bahmutov
Copy link
Contributor

bahmutov commented Apr 12, 2021

Digging under the hood, we decide to treat the crashing JSON object as binary, which breaks the request. If we treat the posted req.body as utf8 then everything is fine. We determine if the buffer is binary or utf8 by using module istextorbinary

piping request to get the body
got the req.body <Buffer 7b 22 66 69 72 73 74 22 3a 22 e6 9d b1 e4 ba ac e9 83 bd e6 96 b0 e6 9d b1 e6 9d b1 22 7d>
getEncoding <Buffer 7b 22 66 69 72 73 74 22 3a 22 e6 9d b1 e4 ba ac e9 83 bd e6 96 b0 e6 9d b1 e6 9d b1 22 7d> opts undefined
getEncoding <Buffer 7b 22 66 69 72 73 74 22 3a 22 e6 9d b1 e4 ba ac e9 83 bd e6 96 b0 e6 9d b1 e6 9d b1 22 7d> opts { chunkLength: 24, chunkBegin: 0 }
contentChunkUTF8 {"first":"東京都新� chunkEnd 24
0 code 123
1 code 34
2 code 102
3 code 105
4 code 114
5 code 115
6 code 116
7 code 34
8 code 58
9 code 34
10 code 26481
11 code 20140
12 code 37117
13 code 26032
14 code 65533
start encoding binary

which has a nice open issue with exactly our scenario bevry/istextorbinary#13

@smfelsher
Copy link

I was running into this issue in v7.0.1 while POSTing FormData (testing file uploads). As a temporary workaround, I used cy.server() and cy.route() to spy on the response. Cypress did not crash using these (deprecated) commands.

@johnnyperkins
Copy link

johnnyperkins commented Apr 21, 2021

It's a shame cy.route() is being deprecated as it's been nothing but issues since switching to intercepts

Getting same issue when trying to spy on a PUT request with binary data.
Thought this would get fixed with 7.
Before it was modifying the request data. #15038

@flotwig
Copy link
Contributor

flotwig commented Apr 22, 2021

@johnnyperkins cy.route will continue to be available as a plugin even once it's removed from core, don't feel like you must convert everything to cy.intercept. cy.intercept is a huge new API surface and we appreciate your patience as we continue to fix issues and improve the experience.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Apr 22, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 22, 2021

The code for this is done in cypress-io/cypress#15946, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Apr 22, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 26, 2021

Released in 7.2.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants