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

When mocking a response with MockAgent errors are different from a real request #1546

Closed
JekRock opened this issue Jul 14, 2022 · 2 comments · Fixed by #1549
Closed

When mocking a response with MockAgent errors are different from a real request #1546

JekRock opened this issue Jul 14, 2022 · 2 comments · Fixed by #1549
Labels
bug Something isn't working

Comments

@JekRock
Copy link

JekRock commented Jul 14, 2022

Bug Description

When mocking a response with MockAgent errors are different from a real request. For example, if you try to get body.json() from a non-JSON body, MockAgent will throw TypeError: this[kAbort] is not a function while real request will return SyntaxError: Unexpected token < in JSON at position 0

Reproducible By

The following code will throw

/tmp/test/node_modules/undici/lib/api/readable.js:52
      this[kAbort]()
                  ^

TypeError: this[kAbort] is not a function
    at BodyReadable.destroy (/tmp/test/node_modules/undici/lib/api/readable.js:52:19)
    at consumeEnd (/tmp/test/node_modules/undici/lib/api/readable.js:257:12)
    at BodyReadable.<anonymous> (/tmp/test/node_modules/undici/lib/api/readable.js:219:7)
    at BodyReadable.emit (node:events:527:28)
    at BodyReadable.emit (/tmp/test/node_modules/undici/lib/api/readable.js:66:18)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
import { request, MockAgent, setGlobalDispatcher } from 'undici'

const mockAgent = new MockAgent();

setGlobalDispatcher(mockAgent);

const mockPool = mockAgent.get('https://google.com');
mockPool.intercept({
  path: 'https://google.com'
}).reply(200, "it's just a text");

const {
  body
} = await request('https://google.com');

const json = await body.json();

console.log(json);

But if you remove mocking, then the result will be

undefined:1
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at consumeEnd (/tmp/test/node_modules/undici/lib/api/readable.js:237:20)
    at BodyReadable.<anonymous> (/tmp/test/node_modules/undici/lib/api/readable.js:219:7)
    at BodyReadable.emit (node:events:527:28)
    at BodyReadable.emit (/tmp/test/node_modules/undici/lib/api/readable.js:66:18)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
import { request } from 'undici'

const {
  body
} = await request('https://google.com');

const json = await body.json();

console.log(json);

Expected Behavior

Both real and mocked responses should return the same error in the situation described above.

Environment

  • Node: v16.15.0
  • undici: 5.7.0
@JekRock JekRock added the bug Something isn't working label Jul 14, 2022
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@JekRock
Copy link
Author

JekRock commented Jul 14, 2022

Sure. I will try to find time this weekend for that

KhafraDev added a commit to KhafraDev/undici that referenced this issue Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants