Browse files

added defaultReplyHeaders() to scope

  • Loading branch information...
1 parent 92dda46 commit f2ca4f31341ec4c60f5e7cff5c80ad3b7249d92d @pgte pgte committed Feb 12, 2012
Showing with 46 additions and 4 deletions.
  1. +15 −0 lib/mixin.js
  2. +14 −4 lib/scope.js
  3. +17 −0 tests/test_intercept.js
View
15 lib/mixin.js
@@ -0,0 +1,15 @@
+function clone(o) {
+ return JSON.parse(JSON.stringify(o));
+}
+
+function mixin(a, b) {
+ if (! a) { a = {}; }
+ if (! b) {b = {}; }
+ a = clone(a);
+ for(var prop in b) {
+ a[prop] = b[prop];
+ }
+ return a;
+}
+
+module.exports = mixin;
View
18 lib/scope.js
@@ -1,6 +1,7 @@
var path = require('path')
, fs = require('fs')
, globalIntercept = require('./intercept')
+ , mixin = require('./mixin')
, assert = require('assert')
var noop = function() {};
@@ -54,14 +55,17 @@ function startScope(basePath, options) {
throw new Error('Error encoding response body into JSON');
}
}
-
-
+
+ if (scope._defaultReplyHeaders) {
+ headers || (headers = {});
+ headers = mixin(scope._defaultReplyHeaders, headers);
+ }
+
if (headers !== undefined) {
this.headers = {};
// makes sure all keys in headers are in lower case
- var key2;
- for (key2 in headers) {
+ for (var key2 in headers) {
if (headers.hasOwnProperty(key2)) {
this.headers[key2.toLowerCase()] = headers[key2];
}
@@ -227,6 +231,11 @@ function startScope(basePath, options) {
matchHeaders.push({ name: name, value: value });
return this;
}
+
+ function defaultReplyHeaders(headers) {
+ this._defaultReplyHeaders = headers;
+ return this;
+ }
function log(newLogger) {
logger = newLogger;
@@ -244,6 +253,7 @@ function startScope(basePath, options) {
, filteringPath: filteringPath
, filteringRequestBody: filteringRequestBody
, matchHeader: matchHeader
+ , defaultReplyHeaders: defaultReplyHeaders
, log: log
};
View
17 tests/test_intercept.js
@@ -1125,3 +1125,20 @@ tap.test("allow unmocked option works", function(t) {
res.on('end', firstIsDone);
}).end();
});
+
+tap.test("default reply headers work", function(t) {
+ var scope = nock('http://default.reply.headers.com')
+ .defaultReplyHeaders({'X-Powered-By': 'Meeee', 'X-Another-Header': 'Hey man!'})
+ .get('/')
+ .reply(200, '', {A: 'b'});
+
+ function done(res) {
+ t.deepEqual(res.headers, {'x-powered-by': 'Meeee', 'x-another-header': 'Hey man!', a: 'b'});
+ t.end();
+ }
+
+ http.request({
+ host: 'default.reply.headers.com'
+ , path: '/'
+ }, done).end();
+});

0 comments on commit f2ca4f3

Please sign in to comment.