Skip to content

Commit

Permalink
Clarify that header function receives the response body (#1544)
Browse files Browse the repository at this point in the history
Closes #1542
  • Loading branch information
paulmelnikow committed May 20, 2019
1 parent 4a36b2c commit 900d96d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,15 @@ const scope = nock('https://api.github.com')
```

Or you can use a function to generate the headers values. The function will be
passed the request, response, and body (if available). The body will be either a
buffer, a stream, or undefined.
passed the request, response, and response body (if available). The body will
be either a buffer, a stream, or undefined.

```js
const scope = nock('http://www.headdy.com')
.get('/')
.reply(200, 'Hello World!', {
'X-My-Headers': (req, res, body) => body.toString(),
'Content-Length': (req, res, body) => body.length,
ETag: () => `${Date.now()}`,
})
```

Expand All @@ -556,9 +557,7 @@ Or you can use a function to generate the default headers values:
```js
const scope = nock('http://www.headdy.com')
.defaultReplyHeaders({
'Content-Length': function(req, res, body) {
return body.length
},
'Content-Length': (req, res, body) => body.length,
})
.get('/')
.reply(200, 'The default headers should come too')
Expand Down
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

// Evaluate functional headers.
const evaluatedHeaders = {}
Object.entries(response.headers).forEach(function([key, value]) {
Object.entries(response.headers).forEach(([key, value]) => {
if (typeof value === 'function') {
response.headers[key] = evaluatedHeaders[key] = value(
req,
Expand Down
4 changes: 0 additions & 4 deletions tests/test_reply_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ test('reply header function receives the correct arguments', async t => {
'X-My-Headers': (req, res, body) => {
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.
// https://github.com/nock/nock/issues/1542
t.type(body, Buffer)
t.true(Buffer.from('boo!').equals(body))
return 'gotcha'
Expand Down

0 comments on commit 900d96d

Please sign in to comment.