Skip to content

Commit

Permalink
fix(mock utils): set Readable.abort (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jul 14, 2022
1 parent 22e2f39 commit 6c9e634
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit 6c9e634

Please sign in to comment.