Skip to content

Commit

Permalink
mock: improve buildHeadersFromArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 28, 2024
1 parent 5600aa1 commit c656825
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
12 changes: 12 additions & 0 deletions benchmarks/mock/build-headers-from-array.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { bench, group, run } from 'mitata'
import { buildHeadersFromArray } from '../../lib/mock/mock-utils.js'

const headers = ['key', 'value', 'key2', 'value2']

group('buildHeadersFromArray', () => {
bench(`buildHeadersFromArray ${JSON.stringify(headers)}`, () => {
buildHeadersFromArray(headers)
})
})

await run()
13 changes: 7 additions & 6 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
kOrigin,
kGetNetConnect
} = require('./mock-symbols')
const { NullObject } = require('../util/null-object')
const { buildURL, nop } = require('../core/util')
const { STATUS_CODES } = require('node:http')
const {
Expand Down Expand Up @@ -59,12 +60,11 @@ function getHeaderByName (headers, key) {

/** @param {string[]} headers */
function buildHeadersFromArray (headers) { // fetch HeadersList
const clone = headers.slice()
const entries = []
for (let index = 0; index < clone.length; index += 2) {
entries.push([clone[index], clone[index + 1]])
const result = new NullObject()
for (let i = 0; i < headers.length; i += 2) {
result[headers[i]] = headers[i + 1]
}
return Object.fromEntries(entries)
return result
}

function matchHeaders (mockDispatch, headers) {
Expand Down Expand Up @@ -358,5 +358,6 @@ module.exports = {
buildMockDispatch,
checkNetConnect,
buildMockOptions,
getHeaderByName
getHeaderByName,
buildHeadersFromArray
}
8 changes: 8 additions & 0 deletions lib/util/null-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const NullObject = function () { }
NullObject.prototype = Object.create(null)

module.exports = {
NullObject
}
9 changes: 7 additions & 2 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
16 changes: 15 additions & 1 deletion test/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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')
})
})

0 comments on commit c656825

Please sign in to comment.