Skip to content

Commit 53df078

Browse files
authored
mock: improve test coverage of buildHeadersFromArray (#2872)
1 parent 19b4d39 commit 53df078

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/mock/mock-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,6 @@ module.exports = {
358358
buildMockDispatch,
359359
checkNetConnect,
360360
buildMockOptions,
361-
getHeaderByName
361+
getHeaderByName,
362+
buildHeadersFromArray
362363
}

test/mock-agent.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ test('MockAgent - disableNetConnect should throw if dispatch not found by net co
22822282
})
22832283

22842284
test('MockAgent - headers function interceptor', async (t) => {
2285-
t = tspl(t, { plan: 7 })
2285+
t = tspl(t, { plan: 8 })
22862286

22872287
const server = createServer((req, res) => {
22882288
t.fail('should not be called')
@@ -2310,7 +2310,7 @@ test('MockAgent - headers function interceptor', async (t) => {
23102310
t.strictEqual(typeof headers, 'object')
23112311
return !Object.keys(headers).includes('authorization')
23122312
}
2313-
}).reply(200, 'foo').times(2)
2313+
}).reply(200, 'foo').times(3)
23142314

23152315
await t.rejects(request(`${baseUrl}/foo`, {
23162316
method: 'GET',
@@ -2319,6 +2319,11 @@ test('MockAgent - headers function interceptor', async (t) => {
23192319
}
23202320
}), 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)`))
23212321

2322+
await t.rejects(request(`${baseUrl}/foo`, {
2323+
method: 'GET',
2324+
headers: ['Authorization', 'Bearer foo']
2325+
}), 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)`))
2326+
23222327
{
23232328
const { statusCode } = await request(`${baseUrl}/foo`, {
23242329
method: 'GET',

test/mock-utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const {
88
getMockDispatch,
99
getResponseData,
1010
getStatusText,
11-
getHeaderByName
11+
getHeaderByName,
12+
buildHeadersFromArray
1213
} = require('../lib/mock/mock-utils')
1314

1415
test('deleteMockDispatch - should do nothing if not able to find mock dispatch', (t) => {
@@ -210,3 +211,16 @@ test('getHeaderByName', (t) => {
210211

211212
t.end()
212213
})
214+
215+
describe('buildHeadersFromArray', () => {
216+
test('it should build headers from array', (t) => {
217+
t = tspl(t, { plan: 2 })
218+
219+
const headers = buildHeadersFromArray([
220+
'key', 'value'
221+
])
222+
223+
t.deepStrictEqual(Object.keys(headers).length, 1)
224+
t.strictEqual(headers.key, 'value')
225+
})
226+
})

0 commit comments

Comments
 (0)