Skip to content

Commit

Permalink
bug: handle content-type request headers when arrays
Browse files Browse the repository at this point in the history
Fixes #1642
  • Loading branch information
mastermatt authored and gr2m committed Jul 24, 2019
1 parent 87cac20 commit e744b0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/common.js
Expand Up @@ -186,7 +186,7 @@ function contentEncoding(headers, encoder) {

function isJSONContent(headers) {
// https://tools.ietf.org/html/rfc8259
const contentType = (headers['content-type'] || '').toLowerCase()
const contentType = String(headers['content-type'] || '').toLowerCase()
return contentType.startsWith('application/json')
}

Expand Down
23 changes: 23 additions & 0 deletions tests/test_reply_function_sync.js
Expand Up @@ -145,6 +145,29 @@ test('when content-type is json, reply function receives parsed body', async t =
scope.done()
})

// Regression test for https://github.com/nock/nock/issues/1642
test('when content-type is json (as array), reply function receives parsed body', async t => {
t.plan(4)
const exampleRequestBody = JSON.stringify({ id: 1, name: 'bob' })

const scope = nock('http://example.test')
.post('/')
.reply(201, (uri, requestBody) => {
t.type(requestBody, 'object')
t.deepEqual(requestBody, JSON.parse(exampleRequestBody))
})

const { statusCode, body } = await got('http://example.test/', {
// Providing the field value as an array is probably a bug on the callers behalf,
// but it is still allowed by Node
headers: { 'Content-Type': ['application/json', 'charset=utf8'] },
body: exampleRequestBody,
})
t.is(statusCode, 201)
t.equal(body, '')
scope.done()
})

test('without content-type header, body sent to reply function is not parsed', async t => {
t.plan(4)
const exampleRequestBody = JSON.stringify({ id: 1, name: 'bob' })
Expand Down

0 comments on commit e744b0a

Please sign in to comment.