Skip to content

Commit

Permalink
json and form encoding body matching
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Apr 1, 2013
1 parent 4d57224 commit 30b8308
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
42 changes: 42 additions & 0 deletions lib/match_body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var deepEqual = require('assert').deepEqual;
var qs = require('querystring');

module.exports =
function matchBody(spec, body) {
if (typeof spec == 'undefined') return true;
var options = this || {};

if (Buffer.isBuffer(body)) {
body = body.toString();
}

var json;
try {
json = JSON.parse(body);
} catch(err) {
// do nothing
}
if (typeof json !== 'undefined') body = json;
else {
if (
(typeof spec == 'object') &&
options.headers &&
options.headers['content-type'] &&
options.headers['content-type'].match(/application\/x-www-form-urlencoded/)) {
body = qs.parse(body);
}

}

if (typeof spec == 'string') {
return body.toString() === spec;
}

try {
deepEqual(spec, body);
return true;
} catch(err) {
return false;
}

};
16 changes: 5 additions & 11 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path')
, fs = require('fs')
, globalIntercept = require('./intercept')
, mixin = require('./mixin')
, matchBody = require('./match_body')
, assert = require('assert')
, url = require('url');

Expand Down Expand Up @@ -45,16 +46,8 @@ function startScope(basePath, options) {
function intercept(uri, method, requestBody, interceptorOptions) {
var interceptorMatchHeaders = [];
var key = method.toUpperCase() + ' ' + basePath + uri;
if (typeof(requestBody) != 'undefined' && typeof(requestBody) !== 'string') {
try {
requestBody = JSON.stringify(requestBody);
} catch(err) {
throw new Error('Error encoding request body into JSON');
}
}

if (requestBody) { key += (' ' + requestBody); }

var body = requestBody;

function reply(statusCode, body, headers) {
this.statusCode = statusCode;

Expand Down Expand Up @@ -141,9 +134,9 @@ function startScope(basePath, options) {
matchKey += ":" + options.port;
}
matchKey += path;
if (body) { matchKey += (' ' + body); }
matches = matchKey === this._key;
logger('matching ' + matchKey + ' to ' + this._key + ': ' + matches);
if (matches) matches = (matchBody.call(options, this._requestBody, body));
return matches;
}

Expand Down Expand Up @@ -185,6 +178,7 @@ function startScope(basePath, options) {

var interceptor = {
_key: key
, _requestBody: requestBody
, reply: reply
, replyWithFile: replyWithFile
, discard: discard
Expand Down
48 changes: 48 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test("get gets mocked", function(t) {
var dataCalled = false

var scope = nock('http://www.google.com')
.log(console.log)
.get('/')
.reply(200, "Hello World!");

Expand Down Expand Up @@ -1700,4 +1701,51 @@ test('allow unmocked post with json data', function(t) {
t.equal(200, resp.statusCode)
t.end();
});
});

test('allow unordered body with json encoding', function(t) {
var scope =
nock('http://wtfjs.org')
.log(console.log)
.post('/like-wtf', {
foo: 'bar',
bar: 'foo'
})
.reply(200, 'Heyyyy!');

mikealRequest({
uri: 'http://wtfjs.org/like-wtf',
method: 'POST',
json: {
bar: 'foo',
foo: 'bar'
}},
function (e, r, body) {
t.equal(body, 'Heyyyy!');
scope.done();
t.end();
});
});

test('allow unordered body with form encoding', function(t) {
var scope =
nock('http://wtfjs.org')
.post('/like-wtf', {
foo: 'bar',
bar: 'foo'
})
.reply(200, 'Heyyyy!');

mikealRequest({
uri: 'http://wtfjs.org/like-wtf',
method: 'POST',
form: {
bar: 'foo',
foo: 'bar'
}},
function (e, r, body) {
t.equal(body, 'Heyyyy!');
scope.done();
t.end();
});
});

1 comment on commit 30b8308

@indexzero
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

Please sign in to comment.