Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: improve test coverage of buildHeadersFromArray #2872

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})
Loading