Skip to content

Commit

Permalink
previously only addressed the JSON encoding for reply callbacks. Now …
Browse files Browse the repository at this point in the history
…we're covered.
  • Loading branch information
Spencer Alger committed Dec 13, 2013
1 parent f2f9ed5 commit 879d029
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 37 deletions.
80 changes: 43 additions & 37 deletions lib/scope.js
@@ -1,4 +1,4 @@
/**
/**
* @module nock/scope
*/
var path = require('path')
Expand Down Expand Up @@ -26,17 +26,17 @@ function startScope(basePath, options) {
urlParts = url.parse(basePath),
port = urlParts.port || ((urlParts.protocol === 'http:') ? 80 : 443),
persist = false;

basePath = urlParts.protocol + '//' + urlParts.hostname + ':' + port;

function add(key, interceptor, scope) {
if (! interceptors.hasOwnProperty(key)) {
interceptors[key] = [];
}
interceptors[key].push(interceptor);
globalIntercept(basePath, interceptor, scope);
}

function remove(key, interceptor) {
if (persist) return;
var arr = interceptors[key];
Expand All @@ -45,7 +45,7 @@ function startScope(basePath, options) {
if (arr.length === 0) { delete interceptors[key]; }
}
}

function intercept(uri, method, requestBody, interceptorOptions) {
var interceptorMatchHeaders = [];
var key = method.toUpperCase() + ' ' + basePath + uri;
Expand All @@ -60,14 +60,6 @@ function startScope(basePath, options) {
}
}

if (typeof(body) !== 'string' && typeof(body) !== 'function' && !Buffer.isBuffer(body) && !isStream(body)) {
try {
body = JSON.stringify(body);
} catch(err) {
throw new Error('Error encoding response body into JSON');
}
}

if (scope._defaultReplyHeaders) {
headers || (headers = {});
headers = mixin(scope._defaultReplyHeaders, headers);
Expand All @@ -84,18 +76,32 @@ function startScope(basePath, options) {
}
}

if (typeof(body) !== 'string' && typeof(body) !== 'function' && !Buffer.isBuffer(body) && !isStream(body)) {
try {
body = JSON.stringify(body);
if (!this.headers) {
this.headers = {};
}
if (!this.headers['content-type']) {
this.headers['content-type'] = 'application/json';
}
} catch(err) {
throw new Error('Error encoding response body into JSON');
}
}

this.body = body;
add(key, this, scope);
return scope;
}

function replyWithFile(statusCode, filePath, headers) {
var readStream = fs.createReadStream(filePath);
readStream.pause();
this.filePath = filePath;
return reply.call(this, statusCode, readStream, headers);
}

var matchStringOrRegexp = function(target, pattern) {
if (pattern instanceof RegExp) {
return target.match(pattern);
Expand All @@ -111,7 +117,7 @@ function startScope(basePath, options) {
, path = options.path
, matches
, proto = options.proto;

if (transformPathFunction) { path = transformPathFunction(path); }
if (typeof(body) !== 'string') {
body = body.toString();
Expand All @@ -122,7 +128,7 @@ function startScope(basePath, options) {
var checkHeaders = function(header) {
return matchStringOrRegexp(options.getHeader(header.name), header.value);
};

if (!matchHeaders.every(checkHeaders) ||
!interceptorMatchHeaders.every(checkHeaders)) {
return false;
Expand All @@ -148,7 +154,7 @@ function startScope(basePath, options) {
, path = options.path
, matches
, proto = options.proto;

if (transformPathFunction) { path = transformPathFunction(path); }

var checkHeaders = function(header) {
Expand All @@ -162,14 +168,14 @@ function startScope(basePath, options) {
var matchKey = method + ' ' + proto + '://' + options.host + path;
return this._key === matchKey
}

function filteringPath() {
if (typeof arguments[0] === 'function') {
this.transformFunction = arguments[0];
}
return this;
}

function discard() {
if (persist && this.filePath) {
this.body = fs.createReadStream(this.filePath);
Expand All @@ -183,7 +189,7 @@ function startScope(basePath, options) {
interceptorMatchHeaders.push({ name: name, value: value });
return this;
}

/**
* Set number of times will repeat the interceptor
* @name times
Expand All @@ -195,12 +201,12 @@ function startScope(basePath, options) {
*/
function times(newCounter) {
if (newCounter < 1) return this;

this.counter = newCounter;

return this;
}

/**
* An sugar sintaxe for times(1)
* @name once
Expand All @@ -224,7 +230,7 @@ function startScope(basePath, options) {
function twice() {
return this.times(2);
}

/**
* An sugar sintaxe for times(3).
* @name thrice
Expand Down Expand Up @@ -253,44 +259,44 @@ function startScope(basePath, options) {
, twice: twice
, thrice: thrice
};

return interceptor;
}

function get(uri, requestBody, options) {
return intercept(uri, 'GET', requestBody, options);
}

function post(uri, requestBody, options) {
return intercept(uri, 'POST', requestBody, options);
}

function put(uri, requestBody, options) {
return intercept(uri, 'PUT', requestBody, options);
}

function head(uri, requestBody, options) {
return intercept(uri, 'HEAD', requestBody, options);
}

function patch(uri, requestBody, options) {
return intercept(uri, 'PATCH', requestBody, options);
}

function merge(uri, requestBody, options) {
return intercept(uri, 'MERGE', requestBody, options);
}

function _delete(uri, requestBody, options) {
return intercept(uri, 'DELETE', requestBody, options);
}

function pendingMocks() {
return Object.keys(interceptors);
}

function isDone() {

// if nock is turned off, it always says it's done
if (! globalIntercept.isOn()) { return true; }

Expand All @@ -317,14 +323,14 @@ function startScope(basePath, options) {
return (doneHostCount === keys.length);
}
}

function done() {
assert.ok(isDone(), "Mocks not yet satisfied:\n" + pendingMocks().join("\n"));
}

function buildFilter() {
var filteringArguments = arguments;

if (arguments[0] instanceof RegExp) {
return function(path) {
if (path) {
Expand All @@ -343,7 +349,7 @@ function startScope(basePath, options) {
throw new Error('Invalid arguments: filtering path should be a function or a regular expression');
return this;
}

function filteringRequestBody() {
transformRequestBodyFunction = buildFilter.apply(undefined, arguments);
if (!transformRequestBodyFunction)
Expand All @@ -360,7 +366,7 @@ function startScope(basePath, options) {
this._defaultReplyHeaders = headers;
return this;
}

function log(newLogger) {
logger = newLogger;
return this;
Expand All @@ -374,7 +380,7 @@ function startScope(basePath, options) {
function shouldPersist() {
return persist;
}

scope = {
get: get
, post: post
Expand Down
43 changes: 43 additions & 0 deletions tests/test_intercept.js
Expand Up @@ -1523,6 +1523,49 @@ test("default reply headers work", function(t) {
}, done).end();
});

test("JSON encoded replies set the content-type header", function(t) {
var scope = nock('http://localhost')
.get('/')
.reply(200, {
A: 'b'
});

function done(res) {
scope.done();
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'application/json');
t.end();
}

http.request({
host: 'localhost'
, path: '/'
}, done).end();
});


test("JSON encoded replies does not overwrite existing content-type header", function(t) {
var scope = nock('http://localhost')
.get('/')
.reply(200, {
A: 'b'
}, {
'Content-Type': 'unicorns'
});

function done(res) {
scope.done();
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'unicorns');
t.end();
}

http.request({
host: 'localhost'
, path: '/'
}, done).end();
});

test('clean all works', function(t) {
var scope = nock('http://amazon.com')
.get('/nonexistent')
Expand Down

0 comments on commit 879d029

Please sign in to comment.