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

mock: fix mocking of Uint8Array and ArrayBuffers as provided mock-responses #3662

Merged
merged 1 commit into from
Oct 1, 2024
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
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
Loading