Skip to content

Commit

Permalink
Merge 2642935 into 66cd477
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 24, 2017
2 parents 66cd477 + 2642935 commit 12f9e72
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ node_js:
install:
- travis_retry npm install

script:
- npm run lint
- npm run unit
- npm run integration
after_script:
- npm run coverage
- npm run coveralls
Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
"unit": "tap --harmony ./tests/test_*.js",
"preintegration": "./tests/bin/install_sub_deps versioned",
"integration": "tap --harmony ./tests/versioned/*/*.tap.js",
"test": "npm run lint && npm run unit && npm run integration",
"test": "tap --only tests/test_intercept.js",
"coverage": "nyc tap --harmony ./tests/test_*.js",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "eslint '**/*.js'",
Expand Down
50 changes: 49 additions & 1 deletion tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -3605,7 +3605,7 @@ test('issue #163 - Authorization header isn\'t mocked', {skip: process.env.AIRPL
});
});

test('define() uses reqheaders', function(t) {
test('define() uses reqheaders', {only: true}, function(t) {
var nockDef = {
"scope":"http://example.com",
"method":"GET",
Expand Down 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', {only: true}, 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 12f9e72

Please sign in to comment.