Comparing version 0.8.4 to 0.9.1
@@ -89,2 +89,15 @@ var RequestOverrider = require('./request_overrider'), | ||
if (interceptors.length) { | ||
var matches = false, | ||
allowUnmocked = false; | ||
interceptors.forEach(function(interceptor) { | ||
if (! allowUnmocked && interceptor.options.allowUnmocked) { allowUnmocked = true; } | ||
if (interceptor.matchIndependentOfBody(options)) { matches = true; } | ||
}); | ||
if (! matches && allowUnmocked) { | ||
return oldRequest.apply(module, arguments); | ||
} | ||
req = new EventEmitter(); | ||
@@ -91,0 +104,0 @@ res = RequestOverrider(req, options, interceptors, remove); |
@@ -8,9 +8,10 @@ var path = require('path') | ||
function startScope(basePath) { | ||
var interceptors = {} | ||
, scope | ||
, transformPathFunction | ||
, transformRequestBodyFunction | ||
, matchHeaders = [] | ||
, logger = noop; | ||
function startScope(basePath, options) { | ||
var interceptors = {}, | ||
scope, | ||
transformPathFunction, | ||
transformRequestBodyFunction, | ||
matchHeaders = [], | ||
logger = noop, | ||
scopeOptions = options || {}; | ||
@@ -22,2 +23,3 @@ function add(key, interceptor) { | ||
interceptors[key].push(interceptor); | ||
interceptor.options = scopeOptions; | ||
globalIntercept(basePath, interceptor); | ||
@@ -107,2 +109,22 @@ } | ||
} | ||
function matchIndependentOfBody(options) { | ||
var method = options.method || 'GET' | ||
, path = options.path | ||
, matches | ||
, proto = options.proto; | ||
if (transformPathFunction) { path = transformPathFunction(path); } | ||
var checkHeaders = function(header) { | ||
return options.getHeader && options.getHeader(header.name) === header.value; | ||
}; | ||
if (!matchHeaders.every(checkHeaders) || | ||
!interceptorMatchHeaders.every(checkHeaders)) { | ||
return false; | ||
} | ||
var matchKey = method + ' ' + proto + '://' + options.host + path; | ||
return this._key === matchKey | ||
} | ||
@@ -131,2 +153,3 @@ function filteringPath() { | ||
, match: match | ||
, matchIndependentOfBody: matchIndependentOfBody | ||
, filteringPath: filteringPath | ||
@@ -133,0 +156,0 @@ , matchHeader: matchHeader |
{ "name" : "nock" | ||
, "description" : "HTTP Server mocking for Node.js" | ||
, "tags" : ["Mock", "HTTP", "testing", "isolation"] | ||
, "version" : "0.8.4" | ||
, "version" : "0.9.1" | ||
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>" | ||
@@ -16,2 +16,3 @@ , "contributors" : | ||
, {"name": "Sascha Drews"} | ||
, {"name": "Mike Swift"} | ||
] | ||
@@ -27,5 +28,5 @@ , "repository" : | ||
, "devDependencies": { | ||
"tap": "0.0.x" | ||
"tap": "0.2.x" | ||
} | ||
, "scripts": { "test": "node node_modules/tap/bin/tap.js tests" } | ||
} |
@@ -144,2 +144,14 @@ # Nock [![Build Status](https://secure.travis-ci.org/pgte/nock.png)](http://travis-ci.org/pgte/nock) | ||
## Allow __unmocked__ requests on a mocked hostname | ||
If you need some request on the same host name to be mocked and some others to **really** go through the HTTP stack, you can use the `allowUnmocked` option like this: | ||
options = {allowUnmocked: true}; | ||
var scope = nock('http://my.existing.service.com', options) | ||
.get('/my/url') | ||
.reply(200, 'OK!'); | ||
GET /my/url => goes through nock | ||
GET /other/url => actually makes request to the server | ||
# Expectations | ||
@@ -146,0 +158,0 @@ |
@@ -1064,2 +1064,43 @@ var nock = require('../.') | ||
} | ||
}); | ||
}); | ||
tap.test("allow unmocked option works", function(t) { | ||
var scope = nock('http://www.google.com', {allowUnmocked: true}) | ||
.get('/abc') | ||
.reply(200, 'Hey!') | ||
.get('/wont/get/here') | ||
.reply(200, 'Hi!'); | ||
function secondIsDone() { | ||
t.ok(! scope.isDone()); | ||
http.request({ | ||
host: "www.google.com" | ||
, path: "/" | ||
, port: 80 | ||
}, function(res) { | ||
console.log(res.statusCode); | ||
t.assert(res.statusCode < 400 && res.statusCode >= 200, 'GET Google Home page'); | ||
t.end(); | ||
}).end(); | ||
} | ||
function firstIsDone() { | ||
t.ok(! scope.isDone()); | ||
http.request({ | ||
host: "www.google.com" | ||
, path: "/does/not/exist/dskjsakdj" | ||
, port: 80 | ||
}, function(res) { | ||
t.assert(res.statusCode === 404, 'Google say it does not exist'); | ||
res.on('end', secondIsDone); | ||
}).end(); | ||
} | ||
http.request({ | ||
host: "www.google.com" | ||
, path: "/abc" | ||
, port: 80 | ||
}, function(res) { | ||
res.on('end', firstIsDone); | ||
}).end(); | ||
}); |
53300
1578
233