diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index 036d39680ff..c8c0ed1eef1 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -358,5 +358,6 @@ module.exports = { buildMockDispatch, checkNetConnect, buildMockOptions, - getHeaderByName + getHeaderByName, + buildHeadersFromArray } diff --git a/test/mock-agent.js b/test/mock-agent.js index ab852896daf..eb58afc6ad1 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -2282,7 +2282,7 @@ test('MockAgent - disableNetConnect should throw if dispatch not found by net co }) test('MockAgent - headers function interceptor', async (t) => { - t = tspl(t, { plan: 7 }) + t = tspl(t, { plan: 8 }) const server = createServer((req, res) => { t.fail('should not be called') @@ -2310,7 +2310,7 @@ test('MockAgent - headers function interceptor', async (t) => { t.strictEqual(typeof headers, 'object') return !Object.keys(headers).includes('authorization') } - }).reply(200, 'foo').times(2) + }).reply(200, 'foo').times(3) await t.rejects(request(`${baseUrl}/foo`, { method: 'GET', @@ -2319,6 +2319,11 @@ test('MockAgent - headers function interceptor', async (t) => { } }), new MockNotMatchedError(`Mock dispatch not matched for headers '{"Authorization":"Bearer foo"}' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect disabled)`)) + await t.rejects(request(`${baseUrl}/foo`, { + method: 'GET', + headers: ['Authorization', 'Bearer foo'] + }), new MockNotMatchedError(`Mock dispatch not matched for headers '["Authorization","Bearer foo"]' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect disabled)`)) + { const { statusCode } = await request(`${baseUrl}/foo`, { method: 'GET', diff --git a/test/mock-utils.js b/test/mock-utils.js index 744011c70b8..f27a6763ae9 100644 --- a/test/mock-utils.js +++ b/test/mock-utils.js @@ -8,7 +8,8 @@ const { getMockDispatch, getResponseData, getStatusText, - getHeaderByName + getHeaderByName, + buildHeadersFromArray } = require('../lib/mock/mock-utils') test('deleteMockDispatch - should do nothing if not able to find mock dispatch', (t) => { @@ -210,3 +211,16 @@ test('getHeaderByName', (t) => { t.end() }) + +describe('buildHeadersFromArray', () => { + test('it should build headers from array', (t) => { + t = tspl(t, { plan: 2 }) + + const headers = buildHeadersFromArray([ + 'key', 'value' + ]) + + t.deepStrictEqual(Object.keys(headers).length, 1) + t.strictEqual(headers.key, 'value') + }) +})