Skip to content

Commit

Permalink
mock: improve test coverage of buildHeadersFromArray (#2872)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Feb 28, 2024
1 parent 19b4d39 commit 53df078
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,6 @@ module.exports = {
buildMockDispatch,
checkNetConnect,
buildMockOptions,
getHeaderByName
getHeaderByName,
buildHeadersFromArray
}
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 53df078

Please sign in to comment.