Skip to content

API GET requests contain empty body causing errors when mocked with nock@beta #737

@m-kusnierz

Description

@m-kusnierz

SDK you're using (please complete the following information):

  • Version 9.3.0
  • Node.js 20.18.1
  • NPM 8.19.4

Describe the bug
This is not entirely a bug. Also, it is a bit related to how the nock library works in the beta version, but so far I have only encountered this issue with the xero-node library.

I'm trying to use nock to mock some requests in my tests, but I'm having issues with some GET requests.
I'm using the beta version of nock, which is the version that supports the node native fetch API (I need that beta version because some other libraries use fetch).

The problem is that GET requests in xero-node have body defined, and this is causing the nock to throw an error.
The error is thrown because the requestStream is not defined for GET requests, and it's being used in the MockHttpSocket class from the mswjs/interceptors library which is used in the nock beta version (see the links below).

Is this something you could consider fixing in the xero-node library?
Generally it is not recommended to have body in GET requests, that's why I created this issue here.
The other option would be to support body in GET requests in the nock beta version.

To Reproduce
Steps to reproduce the behavior:

  1. Run the tests from following repo https://github.com/m-kusnierz/xero-node-nock-bug-example

Expected behavior
Mocks should work as expected and no errors should be thrown.

Screenshots

Image

Additional context
Example of request config where body (data) is defined for GET request: https://github.com/XeroAPI/xero-node/blob/8d7e222f66f80f26f5f1321cdf77c04866515c27/src/gen/api/accountingApi.ts#L6109-L6116
I know this code is generated by the openapi-generator. I tried to check if that's caused by the openapi-generator, but I couldn't find the code where it generates empty data for GET requests.

Links to mswjs/interceptors which is used in nock beta version:
Place where the error is thrown because requestStream is not defined: https://github.com/mswjs/interceptors/blob/a9fd7cf55c4413d9e3fa50f5ae7559b4c81051f7/src/interceptors/ClientRequest/MockHttpSocket.ts#L542-L546

Place where requestStream is not being defined because it's a GET request: https://github.com/mswjs/interceptors/blob/a9fd7cf55c4413d9e3fa50f5ae7559b4c81051f7/src/interceptors/ClientRequest/MockHttpSocket.ts#L475-L492

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions