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

fix(mock utils): set Readable.abort #1549

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
kOrigin,
kGetNetConnect
} = require('./mock-symbols')
const { buildURL } = require('../core/util')
const { buildURL, nop } = require('../core/util')

function matchValue (match, value) {
if (typeof match === 'string') {
Expand Down Expand Up @@ -288,6 +288,7 @@ function mockDispatch (opts, handler) {
const responseHeaders = generateKeyValues(headers)
const responseTrailers = generateKeyValues(trailers)

handler.abort = nop
handler.onHeaders(statusCode, responseHeaders, resume, getStatusText(statusCode))
handler.onData(Buffer.from(responseData))
handler.onComplete(responseTrailers)
Expand Down
25 changes: 23 additions & 2 deletions test/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { test } = require('tap')
const { createServer } = require('http')
const { promisify } = require('util')
const { MockAgent, MockPool, setGlobalDispatcher, request } = require('..')
const { MockAgent, MockPool, getGlobalDispatcher, setGlobalDispatcher, request } = require('..')
const { kUrl } = require('../lib/core/symbols')
const { kDispatches } = require('../lib/mock/mock-symbols')
const { InvalidArgumentError } = require('../lib/core/errors')
Expand Down Expand Up @@ -303,8 +303,29 @@ test('MockPool - basic intercept with MockPool.request', async (t) => {
})
})

// https://github.com/nodejs/undici/issues/1546
test('MockPool - correct errors when consuming invalid JSON body', async (t) => {
const oldDispatcher = getGlobalDispatcher()

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)

t.teardown(() => setGlobalDispatcher(oldDispatcher))

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')
await t.rejects(body.json(), SyntaxError)

t.end()
})

test('MockPool - allows matching headers in fetch', { skip: nodeMajor < 16 }, async (t) => {
const { fetch, getGlobalDispatcher, setGlobalDispatcher } = require('../index')
const { fetch } = require('../index')

const oldDispatcher = getGlobalDispatcher()

Expand Down