From 1df3923b19ec1b72068998a23115d5bec80b5823 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:45:18 +0200 Subject: [PATCH] mock: fix mocking of Uint8Array and ArrayBuffers as provided mock-reponses (#3662) (#3689) (cherry picked from commit 2d524cf58a1dd21c1e6e7008d305e64e57018af3) Co-authored-by: Aras Abbasi --- lib/mock/mock-utils.js | 4 ++++ test/mock-interceptor.js | 49 ++++++++++++++++++++++++++++++++++++++++ test/mock-utils.js | 12 ++++++++++ 3 files changed, 65 insertions(+) diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index f3c284d7891..8f18db31ee2 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -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 { diff --git a/test/mock-interceptor.js b/test/mock-interceptor.js index 0d16290f579..8364fb40415 100644 --- a/test/mock-interceptor.js +++ b/test/mock-interceptor.js @@ -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 => { @@ -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) + }) + }) +}) diff --git a/test/mock-utils.js b/test/mock-utils.js index f27a6763ae9..b80db2a401c 100644 --- a/test/mock-utils.js +++ b/test/mock-utils.js @@ -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) => {