Skip to content

Commit

Permalink
Merge e2bb871 into 66cd477
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 24, 2017
2 parents 66cd477 + e2bb871 commit 731709c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ function setHeader(request, name, value) {
// request headers).
function setRequestHeaders(req, options, interceptor) {
// We mock request headers if these were specified.
if (interceptor.reqheaders) {
_.forOwn(interceptor.reqheaders, function(val, key) {
if (!_.isFunction(val))
setHeader(req, key, val);
});
}
// if (interceptor.reqheaders) {
// _.forOwn(interceptor.reqheaders, function(val, key) {
// if (!_.isFunction(val))
// setHeader(req, key, val);
// });
// }

// If a filtered scope is being used we have to use scope's host
// in the header, otherwise 'host' header won't match.
Expand Down Expand Up @@ -236,6 +236,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
/// like to change request.path in mid-flight.
options.path = req.path;

// TODO: sets accept header for first request to value of 2nd mock ¯\_(ツ)_/¯
interceptors.forEach(function(interceptor) {
// For correct matching we need to have correct request headers - if these were specified.
setRequestHeaders(req, options, interceptor);
Expand Down
48 changes: 48 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -5122,6 +5122,54 @@ test('match multiple paths to domain using regexp with allowUnmocked (#835)', fu
});
});

test('multiple interceptors override headers from unrelated request', function (t) {
nock.cleanAll();

nock.define([
{
scope: 'https://api.github.com:443',
method: 'get',
path: '/bar',
reqheaders: {
'x-foo': 'bar'
},
status: 200,
response: {}
},
{
scope: 'https://api.github.com:443',
method: 'get',
path: '/baz',
reqheaders: {
'x-foo': 'baz'
},
status: 200,
response: {}
}
])

mikealRequest({
url: 'https://api.github.com/bar',
headers: {
'x-foo': 'bar'
}
}, function (err, res, body) {
t.error(err);
t.equal(res.statusCode, 200);

mikealRequest.get({
url: 'https://api.github.com/baz',
headers: {
'x-foo': 'baz'
}
}, function (err, res, body) {
t.error(err);
t.equal(res.statusCode, 200);
t.end();
});
});
});

test("teardown", function(t) {
var leaks = Object.keys(global)
.splice(globalCount, Number.MAX_VALUE);
Expand Down

0 comments on commit 731709c

Please sign in to comment.