Skip to content

Commit 518f154

Browse files
committed
Use functions to generate header values.
This change provides the ability to generate header values and default header values by evaluating a function. The function is passed the request, response, and body (if available). The return value is used as the header value. Closes nock#263.
1 parent f73b5c6 commit 518f154

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,20 @@ var scope = nock('http://www.headdy.com')
329329
});
330330
```
331331

332+
Or you can use a function to generate the headers values. The function will be
333+
passed the request, response, and body (if available). The body will be either a
334+
buffer, a stream, or undefined.
335+
336+
```js
337+
var scope = nock('http://www.headdy.com')
338+
.get('/')
339+
.reply(200, 'Hello World!', {
340+
'X-My-Headers': function (req, res, body) {
341+
return body.toString();
342+
}
343+
});
344+
```
345+
332346
### Default Reply Headers
333347

334348
You can also specify default reply headers for all responses like this:
@@ -343,6 +357,19 @@ var scope = nock('http://www.headdy.com')
343357
.reply(200, 'The default headers should come too');
344358
```
345359

360+
Or you can use a function to generate the default headers values:
361+
362+
```js
363+
var scope = nock('http://www.headdy.com')
364+
.defaultReplyHeaders({
365+
'Content-Length': function (req, res, body) {
366+
return body.length;
367+
}
368+
})
369+
.get('/')
370+
.reply(200, 'The default headers should come too');
371+
```
372+
346373
### Including Content-Length Header Automatically
347374

348375
When using `scope.reply()` to set a response body manually, you can have the

lib/mixin.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
'use strict';
2-
3-
function clone(o) {
4-
return JSON.parse(JSON.stringify(o));
5-
}
2+
var _ = require("lodash");
63

74
function mixin(a, b) {
85
if (! a) { a = {}; }
96
if (! b) {b = {}; }
10-
a = clone(a);
7+
a = _.cloneDeep(a);
118
for(var prop in b) {
129
a[prop] = b[prop];
1310
}
1411
return a;
1512
}
1613

17-
module.exports = mixin;
14+
module.exports = mixin;

lib/request_overrider.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,15 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
396396
authorized: true
397397
});
398398

399+
// Evaluate functional headers.
400+
Object.keys(response.headers).forEach(function (key) {
401+
var value = response.headers[key];
402+
403+
if (typeof value === "function") {
404+
response.headers[key] = value(req, response, responseBody);
405+
}
406+
});
407+
399408
process.nextTick(function() {
400409
var respond = function() {
401410
debug('emitting response');

tests/test_intercept.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,24 @@ test("reply headers work with function", function(t) {
611611
});
612612
});
613613

614+
test("reply headers as function work", function(t) {
615+
var scope = nock('http://example.com')
616+
.get('/')
617+
.reply(200, 'boo!', {
618+
'X-My-Headers': function (req, res, body) {
619+
return body.toString();
620+
}
621+
});
622+
623+
var req = http.get({
624+
host: 'example.com',
625+
path: '/'
626+
}, function (res) {
627+
t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
628+
t.end();
629+
});
630+
});
631+
614632
test("match headers", function(t) {
615633
var scope = nock('http://www.headdy.com')
616634
.get('/')
@@ -1983,6 +2001,44 @@ test("default reply headers work", function(t) {
19832001
}, done).end();
19842002
});
19852003

2004+
test("default reply headers as functions work", function(t) {
2005+
var date = (new Date()).toUTCString();
2006+
var message = 'A message.';
2007+
2008+
var scope = nock('http://default.reply.headers.com')
2009+
.defaultReplyHeaders({
2010+
'Content-Length' : function (req, res, body) {
2011+
return body.length;
2012+
},
2013+
2014+
'Date': function () {
2015+
return date;
2016+
},
2017+
2018+
'Foo': function () {
2019+
return 'foo';
2020+
}
2021+
})
2022+
.get('/')
2023+
.reply(200, message, {foo: 'bar'});
2024+
2025+
http.request({
2026+
host: 'default.reply.headers.com',
2027+
path: '/'
2028+
}, function (res) {
2029+
t.deepEqual(
2030+
res.headers,
2031+
{
2032+
'content-length': message.length,
2033+
'date': date,
2034+
'foo': 'bar'
2035+
}
2036+
);
2037+
t.end();
2038+
}
2039+
).end();
2040+
});
2041+
19862042
test("JSON encoded replies set the content-type header", function(t) {
19872043
var scope = nock('http://localhost')
19882044
.get('/')

0 commit comments

Comments
 (0)