Skip to content

Commit

Permalink
mock: fix mocking of Uint8Array and ArrayBuffers as provided mock-rep…
Browse files Browse the repository at this point in the history
…onses (nodejs#3662)
  • Loading branch information
Uzlopak authored and flakey5 committed Oct 8, 2024
1 parent 85824f0 commit 5448e78
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ function matchKey (mockDispatch, { path, method, body, headers }) {
function getResponseData (data) {
if (Buffer.isBuffer(data)) {
return data
} else if (data instanceof Uint8Array) {
return data
} else if (data instanceof ArrayBuffer) {
return data
} else if (typeof data === 'object') {
return JSON.stringify(data)
} else {
Expand Down
49 changes: 49 additions & 0 deletions test/mock-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { MockInterceptor, MockScope } = require('../lib/mock/mock-interceptor')
const MockAgent = require('../lib/mock/mock-agent')
const { kDispatchKey } = require('../lib/mock/mock-symbols')
const { InvalidArgumentError } = require('../lib/core/errors')
const { fetch } = require('../lib/web/fetch/index')

describe('MockInterceptor - path', () => {
test('should remove hash fragment from paths', t => {
Expand Down Expand Up @@ -257,3 +258,51 @@ describe('MockInterceptor - replyContentLength', () => {
t.ok(result instanceof MockInterceptor)
})
})

describe('MockInterceptor - different payloads', () => {
[
// Buffer
['arrayBuffer', 'ArrayBuffer', 'ArrayBuffer', new TextEncoder().encode('{"test":true}').buffer, new TextEncoder().encode('{"test":true}').buffer],
['json', 'ArrayBuffer', 'Object', new TextEncoder().encode('{"test":true}').buffer, { test: true }],
['bytes', 'ArrayBuffer', 'Uint8Array', new TextEncoder().encode('{"test":true}').buffer, new TextEncoder().encode('{"test":true}')],
['text', 'ArrayBuffer', 'string', new TextEncoder().encode('{"test":true}').buffer, '{"test":true}'],

// Buffer
['arrayBuffer', 'Buffer', 'ArrayBuffer', Buffer.from('{"test":true}'), new TextEncoder().encode('{"test":true}').buffer],
['json', 'Buffer', 'Object', Buffer.from('{"test":true}'), { test: true }],
['bytes', 'Buffer', 'Uint8Array', Buffer.from('{"test":true}'), new TextEncoder().encode('{"test":true}')],
['text', 'Buffer', 'string', Buffer.from('{"test":true}'), '{"test":true}'],

// Uint8Array
['arrayBuffer', 'Uint8Array', 'ArrayBuffer', new TextEncoder().encode('{"test":true}'), new TextEncoder().encode('{"test":true}').buffer],
['json', 'Uint8Array', 'Object', new TextEncoder().encode('{"test":true}'), { test: true }],
['bytes', 'Uint8Array', 'Uint8Array', new TextEncoder().encode('{"test":true}'), new TextEncoder().encode('{"test":true}')],
['text', 'Uint8Array', 'string', new TextEncoder().encode('{"test":true}'), '{"test":true}'],

// string
['arrayBuffer', 'string', 'ArrayBuffer', '{"test":true}', new TextEncoder().encode('{"test":true}').buffer],
['json', 'string', 'Object', '{"test":true}', { test: true }],
['bytes', 'string', 'Uint8Array', '{"test":true}', new TextEncoder().encode('{"test":true}')],
['text', 'string', 'string', '{"test":true}', '{"test":true}'],

// object
['arrayBuffer', 'Object', 'ArrayBuffer', { test: true }, new TextEncoder().encode('{"test":true}').buffer],
['json', 'Object', 'Object', { test: true }, { test: true }],
['bytes', 'Object', 'Uint8Array', { test: true }, new TextEncoder().encode('{"test":true}')],
['text', 'Object', 'string', { test: true }, '{"test":true}']
].forEach(([method, inputType, outputType, input, output]) => {
test(`${inputType} will be returned as ${outputType} via ${method}()`, async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/' }).reply(200, input)

const response = await fetch('https://localhost', { dispatcher: mockAgent })

t.deepStrictEqual(await response[method](), output)
})
})
})
12 changes: 12 additions & 0 deletions test/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ describe('getResponseData', () => {
const responseData = getResponseData(Buffer.from('test'))
t.ok(Buffer.isBuffer(responseData))
})

test('it should return Uint8Array untouched', (t) => {
t = tspl(t, { plan: 1 })
const responseData = getResponseData(new TextEncoder().encode('{"test":true}'))
t.ok(responseData instanceof Uint8Array)
})

test('it should return ArrayBuffers untouched', (t) => {
t = tspl(t, { plan: 1 })
const responseData = getResponseData(new TextEncoder().encode('{"test":true}').buffer)
t.ok(responseData instanceof ArrayBuffer)
})
})

test('getStatusText', (t) => {
Expand Down

0 comments on commit 5448e78

Please sign in to comment.