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.request: RequestBody type not aligned with RFC (e.g. boolean) #28789

Closed
PeterRistMHP opened this issue Jan 24, 2024 · 4 comments · Fixed by #28835
Closed

cy.request: RequestBody type not aligned with RFC (e.g. boolean) #28789

PeterRistMHP opened this issue Jan 24, 2024 · 4 comments · Fixed by #28835
Labels
good first issue Good for newcomers type: typings Issue related to Cypress types (for TypeScript)

Comments

@PeterRistMHP
Copy link

Current behavior

I've stumbled on this problem while trying to send only a boolean in a PUT request.

Currently the type for RequestBody is typed as type RequestBody = string | object [1]. However this doesn't align with the current RFC 8259 for allowed values in JSON [2].

[1] https://github.com/cypress-io/cypress/blob/develop/cli/types/cypress.d.ts#L10
[2] https://datatracker.ietf.org/doc/html/rfc8259#section-3

Desired behavior

All values defined in the RFC should be accepted as valid values for the body in a cy.request().

Test code to reproduce

cy.request({ method: "PUT", url: ANY_URL_HERE, body: true })

Cypress Version

v13.16.1

Node version

v20.10.0

Operating System

macOS 14.2

Debug Logs

No response

Other

No response

@jennifer-shehane
Copy link
Member

@PeterRistMHP That's fun. We'd be open to a pull request to fix the types.

@jennifer-shehane jennifer-shehane added type: typings Issue related to Cypress types (for TypeScript) good first issue Good for newcomers stage: ready for work The issue is reproducible and in scope labels Jan 24, 2024
@PeterRistMHP
Copy link
Author

Hi @jennifer-shehane,

I'm afraid it's not only a type issue. I've changed the type, but sending the request with body: true crashes cypress with the following stacktrace:

The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type boolean (true)
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type boolean (true)
    at new NodeError (node:internal/errors:399:5)
    at write_ (node:_http_outgoing:884:11)
    at ClientRequest.write (node:_http_outgoing:849:15)
    at ee.write (<embedded>:2050:109573)
    at e (<embedded>:2050:96908)
    at Immediate.<anonymous> (<embedded>:2050:97414)
    at process.processImmediate (node:internal/timers:476:21)
The Test Runner unexpectedly exited via a exit event with signal SIGABRT

I haven't found the source of this exception yet.

@TheoAnastasiadis
Copy link
Contributor

This is a problem with the @cypress/request-promise library. Indeed the JSON spec allows for boolean and null value literals so you should be able to use this as cy.request('POST', 'https://example.com', true).

My idea for a fix would be to

  • change the driver so that it sets json = true in these cases.
  • change the server to convert boolean and null value literals to string as specified in RFC 8259.
  • fix the types of RequestOptions.

I can open a PR on this 😀

TheoAnastasiadis added a commit to TheoAnastasiadis/cypress that referenced this issue Jan 31, 2024
TheoAnastasiadis added a commit to TheoAnastasiadis/cypress that referenced this issue Jan 31, 2024
jennifer-shehane added a commit that referenced this issue Feb 23, 2024
…ies (#28835)

* fix(types): RequestBody type should be able to accept booleans and null values, which are all valid JSON literals

* refactor: boolean literals are valid JSON objects. Null values should also be considered valid when explicitly passed to the request function.

* refactor: body is explicitly defined when passed as positional argument or when supplied through the options object

* test: JSON literals should be parsed as valid JSON and set json=true

* docs: issue reference

* fix: boolean and null literal should be send to request promise as strings

* docs: fixes #28789 -- added issue reference

* test: tests proper conversion of JSON literals to strings.

* docs: added isssue reference

* docs: fixes #28789 -- changelog entry

* refactor: change isValidJsonObj to isValidBody

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* refactor: change isValidJsonObj to isValidBody

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* refactor: use lodash utils

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* Update cli/CHANGELOG.md

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* docs: moved entry to 13.6.5

* docs: fixed changelog entry

* Update CHANGELOG.md

---------

Co-authored-by: Bill Glesias <bglesias@gmail.com>
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
@jennifer-shehane jennifer-shehane removed the stage: ready for work The issue is reproducible and in scope label Feb 27, 2024
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Mar 13, 2024

Released in 13.7.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Mar 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers type: typings Issue related to Cypress types (for TypeScript)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants