Permalink
Browse files

Allowing unmocked requests on the same hostname

  • Loading branch information...
pgte committed Feb 10, 2012
1 parent 2c53976 commit 796898c86529b17f8ddc433a7f5ea82d8a267361
Showing with 86 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +12 −0 README.md
  3. +13 −0 lib/intercept.js
  4. +30 −7 lib/scope.js
  5. +1 −1 package.json
  6. +29 −0 tests/test_intercept.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -141,6 +141,18 @@ If you need to match requests only if certain request headers match, you can.
.get('/')
.reply(200, {data: "hello world"})
+## 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
Every time an HTTP request is performed for a scope that is mocked, Nock expects to find a handler for it. If it doesn't, it will throw an error.
View
@@ -86,6 +86,19 @@ http.ClientRequest = OverridenClientRequest;
interceptors = interceptorsFor(options);
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();
res = RequestOverrider(req, options, interceptors, remove);
if (callback) {
View
@@ -5,19 +5,21 @@ var path = require('path')
var noop = function() {};
-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 || {};
function add(key, interceptor) {
if (! interceptors.hasOwnProperty(key)) {
interceptors[key] = [];
}
interceptors[key].push(interceptor);
+ interceptor.options = scopeOptions;
globalIntercept(basePath, interceptor);
}
@@ -102,6 +104,26 @@ function startScope(basePath) {
logger('matching ' + matchKey + ' to ' + this._key + ': ' + matches);
return matches;
}
+
+ 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.indexOf(matchKey) === 0;
+ }
function filteringPath() {
if (typeof arguments[0] === 'function') {
@@ -125,6 +147,7 @@ function startScope(basePath) {
, replyWithFile: replyWithFile
, discard: discard
, match: match
+ , matchIndependentOfBody: matchIndependentOfBody
, filteringPath: filteringPath
, matchHeader: matchHeader
};
View
@@ -1,7 +1,7 @@
{ "name" : "nock"
, "description" : "HTTP Server mocking for Node.js"
, "tags" : ["Mock", "HTTP", "testing", "isolation"]
-, "version" : "0.8.4"
+, "version" : "0.9.0"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "contributors" :
[ {"name": "Roly Fentanes" }
View
@@ -1061,4 +1061,33 @@ tap.test("two scopes with the same request are consumed", function(t) {
req.end();
}
+});
+
+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 firstIsDone() {
+ console.log('ended ---- ');
+ 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');
+ t.end();
+ }).end();
+ }
+
+ http.request({
+ host: "www.google.com"
+ , path: "/abc"
+ , port: 80
+ }, function(res) {
+ res.on('end', firstIsDone);
+ }).end();
});

0 comments on commit 796898c

Please sign in to comment.