Skip to content

Commit

Permalink
BREAKING CHANGE: Remove responseBody argument to header functions
Browse files Browse the repository at this point in the history
Closes #1542
  • Loading branch information
paulmelnikow committed May 6, 2019
1 parent 6a26f41 commit 8dcb2cd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
10 changes: 2 additions & 8 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,9 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

// Evaluate functional headers.
const evaluatedHeaders = {}
Object.keys(response.headers).forEach(function(key) {
const value = response.headers[key]

Object.entries(response.headers).forEach(([key, value]) => {
if (typeof value === 'function') {
response.headers[key] = evaluatedHeaders[key] = value(
req,
response,
responseBody
)
response.headers[key] = evaluatedHeaders[key] = value(req, response)
}
})

Expand Down
11 changes: 4 additions & 7 deletions tests/test_reply_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,18 @@ test('reply header function is evaluated and the result sent in the mock respons
})

test('reply header function receives the correct arguments', async t => {
t.plan(4)
t.plan(3)

const { ClientRequest: OverriddenClientRequest } = require('http')
const scope = nock('http://example.test')
.post('/')
.reply(200, 'boo!', {
'X-My-Headers': (req, res, body) => {
'X-My-Headers': (req, res, ...rest) => {
t.type(req, OverriddenClientRequest)
t.type(res, IncomingMessage)
// TODO The current behavior is to pass the response body as a buffer.
// This doesn't seem at all helpful and seems like it is probably a
// bug.
// The third argument was once a buffer, though no longer.
// https://github.com/nock/nock/issues/1542
t.type(body, Buffer)
t.true(Buffer.from('boo!').equals(body))
t.deepEqual(rest, [])
return 'gotcha'
},
})
Expand Down

0 comments on commit 8dcb2cd

Please sign in to comment.