Comparing version 0.16.0 to 0.17.0
@@ -5,2 +5,3 @@ var path = require('path') | ||
, mixin = require('./mixin') | ||
, matchBody = require('./match_body') | ||
, assert = require('assert') | ||
@@ -49,12 +50,4 @@ , url = require('url'); | ||
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) { | ||
@@ -146,5 +139,5 @@ this.statusCode = statusCode; | ||
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; | ||
@@ -191,2 +184,3 @@ } | ||
_key: key | ||
, _requestBody: requestBody | ||
, reply: reply | ||
@@ -193,0 +187,0 @@ , replyWithFile: replyWithFile |
{ "name" : "nock" | ||
, "description" : "HTTP Server mocking for Node.js" | ||
, "tags" : ["Mock", "HTTP", "testing", "isolation"] | ||
, "version" : "0.16.0" | ||
, "version" : "0.17.0" | ||
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>" | ||
@@ -37,3 +37,3 @@ , "contributors" : | ||
, "devDependencies": { | ||
"tap": "0.4.x", | ||
"tap": "*", | ||
"request": "*" | ||
@@ -40,0 +40,0 @@ } |
@@ -6,9 +6,10 @@ var nock = require('../.') | ||
var events = require('events'); | ||
var tap = require('tap'); | ||
var test = require('tap').test; | ||
var mikealRequest = require('request'); | ||
tap.test("get gets mocked", function(t) { | ||
test("get gets mocked", function(t) { | ||
var dataCalled = false | ||
var scope = nock('http://www.google.com') | ||
.log(console.log) | ||
.get('/') | ||
@@ -40,3 +41,3 @@ .reply(200, "Hello World!"); | ||
tap.test("not mocked should work in http", function(t) { | ||
test("not mocked should work in http", function(t) { | ||
var dataCalled = false; | ||
@@ -80,3 +81,3 @@ | ||
tap.test("post", function(t) { | ||
test("post", function(t) { | ||
var dataCalled = false; | ||
@@ -114,3 +115,3 @@ | ||
tap.test("post with empty response body", function(t) { | ||
test("post with empty response body", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -140,3 +141,3 @@ .post('/form') | ||
tap.test("post, lowercase", function(t) { | ||
test("post, lowercase", function(t) { | ||
var dataCalled = false; | ||
@@ -171,3 +172,3 @@ | ||
tap.test("get with reply callback", function(t) { | ||
test("get with reply callback", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -196,3 +197,3 @@ .get('/') | ||
tap.test("get with reply callback returning object", function(t) { | ||
test("get with reply callback returning object", function(t) { | ||
var scope = nock('http://www.googlezzzz.com') | ||
@@ -221,3 +222,3 @@ .get('/') | ||
tap.test("post with reply callback, uri, and request body", function(t) { | ||
test("post with reply callback, uri, and request body", function(t) { | ||
var input = 'key=val'; | ||
@@ -250,3 +251,3 @@ | ||
tap.test("reply with callback and filtered path and body", function(t) { | ||
test("reply with callback and filtered path and body", function(t) { | ||
var noPrematureExecution = false; | ||
@@ -283,3 +284,3 @@ | ||
tap.test("isDone", function(t) { | ||
test("isDone", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -307,3 +308,3 @@ .get('/') | ||
tap.test("requireDone", function(t) { | ||
test("requireDone", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -324,3 +325,3 @@ .get('/', false, { requireDone: false }) | ||
tap.test("request headers exposed", function(t) { | ||
test("request headers exposed", function(t) { | ||
@@ -347,3 +348,3 @@ var scope = nock('http://www.headdy.com') | ||
tap.test("headers work", function(t) { | ||
test("headers work", function(t) { | ||
@@ -372,3 +373,3 @@ var scope = nock('http://www.headdy.com') | ||
tap.test("match headers", function(t) { | ||
test("match headers", function(t) { | ||
var scope = nock('http://www.headdy.com') | ||
@@ -401,3 +402,3 @@ .get('/') | ||
tap.test("match headers with regexp", function(t) { | ||
test("match headers with regexp", function(t) { | ||
var scope = nock('http://www.headier.com') | ||
@@ -430,3 +431,3 @@ .get('/') | ||
tap.test("match all headers", function(t) { | ||
test("match all headers", function(t) { | ||
var scope = nock('http://api.headdy.com') | ||
@@ -482,3 +483,3 @@ .matchHeader('accept', 'application/json') | ||
tap.test("header manipulation", function(t) { | ||
test("header manipulation", function(t) { | ||
var scope = nock('http://example.com') | ||
@@ -505,3 +506,3 @@ .get('/accounts') | ||
tap.test("head", function(t) { | ||
test("head", function(t) { | ||
var dataCalled = false; | ||
@@ -530,3 +531,3 @@ | ||
tap.test("body data is differentiating", function(t) { | ||
test("body data is differentiating", function(t) { | ||
var doneCount = 0 | ||
@@ -596,3 +597,3 @@ , scope = nock('http://www.boddydiff.com') | ||
tap.test("chaining", function(t) { | ||
test("chaining", function(t) { | ||
var repliedCount = 0; | ||
@@ -665,3 +666,3 @@ var scope = nock('http://www.spiffy.com') | ||
tap.test("encoding", function(t) { | ||
test("encoding", function(t) { | ||
var dataCalled = false | ||
@@ -698,3 +699,3 @@ | ||
tap.test("reply with file", function(t) { | ||
test("reply with file", function(t) { | ||
var dataCalled = false | ||
@@ -730,3 +731,3 @@ | ||
tap.test("reply with file and pipe response", function(t) { | ||
test("reply with file and pipe response", function(t) { | ||
var scope = nock('http://www.files.com') | ||
@@ -762,3 +763,3 @@ .get('/') | ||
tap.test("reply with file with mikeal/request", function(t) { | ||
test("reply with file with mikeal/request", function(t) { | ||
var scope = nock('http://www.files.com') | ||
@@ -783,3 +784,3 @@ .get('/') | ||
tap.test("reply with JSON", function(t) { | ||
test("reply with JSON", function(t) { | ||
var dataCalled = false | ||
@@ -815,3 +816,3 @@ | ||
tap.test("filter path with function", function(t) { | ||
test("filter path with function", function(t) { | ||
var scope = nock('http://www.filterurls.com') | ||
@@ -840,3 +841,3 @@ .filteringPath(function(path) { | ||
tap.test("filter path with regexp", function(t) { | ||
test("filter path with regexp", function(t) { | ||
var scope = nock('http://www.filterurlswithregexp.com') | ||
@@ -863,3 +864,3 @@ .filteringPath(/\d/g, '3') | ||
tap.test("filter body with function", function(t) { | ||
test("filter body with function", function(t) { | ||
var scope = nock('http://www.filterboddiez.com') | ||
@@ -889,3 +890,3 @@ .filteringRequestBody(function(body) { | ||
tap.test("filter body with regexp", function(t) { | ||
test("filter body with regexp", function(t) { | ||
var scope = nock('http://www.filterboddiezregexp.com') | ||
@@ -912,3 +913,3 @@ .filteringRequestBody(/mia/, 'nostra') | ||
tap.test("abort request", function(t) { | ||
test("abort request", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -940,3 +941,3 @@ .get('/hey') | ||
tap.test("pause response before data", function(t) { | ||
test("pause response before data", function(t) { | ||
var scope = nock('http://www.mouse.com') | ||
@@ -973,3 +974,3 @@ .get('/pauser') | ||
tap.test("pause response after data", function(t) { | ||
test("pause response after data", function(t) { | ||
var scope = nock('http://pauseme.com') | ||
@@ -1002,3 +1003,3 @@ .get('/') | ||
tap.test("response pipe", function(t) { | ||
test("response pipe", function(t) { | ||
var dest = (function() { | ||
@@ -1054,3 +1055,3 @@ function Constructor() { | ||
tap.test("response pipe without implicit end", function(t) { | ||
test("response pipe without implicit end", function(t) { | ||
var dest = (function() { | ||
@@ -1106,3 +1107,3 @@ function Constructor() { | ||
tap.test("chaining API", function(t) { | ||
test("chaining API", function(t) { | ||
var scope = nock('http://chainchomp.com') | ||
@@ -1146,3 +1147,3 @@ .get('/one') | ||
tap.test("same URI", function(t) { | ||
test("same URI", function(t) { | ||
var scope = nock('http://sameurii.com') | ||
@@ -1180,3 +1181,3 @@ .get('/abc') | ||
tap.test("can use hostname instead of host", function(t) { | ||
test("can use hostname instead of host", function(t) { | ||
var scope = nock('http://www.google.com') | ||
@@ -1201,3 +1202,3 @@ .get('/') | ||
tap.test("can take a port", function(t) { | ||
test("can take a port", function(t) { | ||
var scope = nock('http://www.myserver.com:3333') | ||
@@ -1223,3 +1224,3 @@ .get('/') | ||
tap.test("can use https", function(t) { | ||
test("can use https", function(t) { | ||
var dataCalled = false | ||
@@ -1251,3 +1252,3 @@ | ||
tap.test("complaints if https route is missing", function(t) { | ||
test("complaints if https route is missing", function(t) { | ||
var dataCalled = false | ||
@@ -1274,3 +1275,3 @@ | ||
tap.test("can use ClientRequest using GET", function(t) { | ||
test("can use ClientRequest using GET", function(t) { | ||
@@ -1306,3 +1307,3 @@ var dataCalled = false | ||
tap.test("can use ClientRequest using POST", function(t) { | ||
test("can use ClientRequest using POST", function(t) { | ||
@@ -1340,3 +1341,3 @@ var dataCalled = false | ||
tap.test("same url matches twice", function(t) { | ||
test("same url matches twice", function(t) { | ||
var scope = nock('http://www.twicematcher.com') | ||
@@ -1386,3 +1387,3 @@ .get('/hey') | ||
tap.test("scopes are independent", function(t) { | ||
test("scopes are independent", function(t) { | ||
var scope1 = nock('http://www34.google.com') | ||
@@ -1410,3 +1411,3 @@ .get('/') | ||
tap.test("two scopes with the same request are consumed", function(t) { | ||
test("two scopes with the same request are consumed", function(t) { | ||
var scope1 = nock('http://www36.google.com') | ||
@@ -1441,3 +1442,3 @@ .get('/') | ||
tap.test("allow unmocked option works", function(t) { | ||
test("allow unmocked option works", function(t) { | ||
var scope = nock('http://www.google.com', {allowUnmocked: true}) | ||
@@ -1484,3 +1485,3 @@ .get('/abc') | ||
tap.test("default reply headers work", function(t) { | ||
test("default reply headers work", function(t) { | ||
var scope = nock('http://default.reply.headers.com') | ||
@@ -1502,3 +1503,3 @@ .defaultReplyHeaders({'X-Powered-By': 'Meeee', 'X-Another-Header': 'Hey man!'}) | ||
tap.test('clean all works', function(t) { | ||
test('clean all works', function(t) { | ||
var scope = nock('http://amazon.com') | ||
@@ -1524,3 +1525,3 @@ .get('/nonexistent') | ||
tap.test('username and password works', function(t) { | ||
test('username and password works', function(t) { | ||
var scope = nock('http://passwordyy.com') | ||
@@ -1541,3 +1542,3 @@ .get('/') | ||
tap.test('works with mikeal/request and username and password', function(t) { | ||
test('works with mikeal/request and username and password', function(t) { | ||
var scope = nock('http://passwordyyyyy.com') | ||
@@ -1556,3 +1557,3 @@ .get('/abc') | ||
tap.test('different ports work works', function(t) { | ||
test('different ports work works', function(t) { | ||
var scope = nock('http://abc.portyyyy.com:8081') | ||
@@ -1572,3 +1573,3 @@ .get('/pathhh') | ||
tap.test('different ports work work with Mikeal request', function(t) { | ||
test('different ports work work with Mikeal request', function(t) { | ||
var scope = nock('http://abc.portyyyy.com:8082') | ||
@@ -1586,3 +1587,3 @@ .get('/pathhh') | ||
tap.test('explicitly specifiying port 80 works', function(t) { | ||
test('explicitly specifiying port 80 works', function(t) { | ||
var scope = nock('http://abc.portyyyy.com:80') | ||
@@ -1602,3 +1603,3 @@ .get('/pathhh') | ||
tap.test('post with object', function(t) { | ||
test('post with object', function(t) { | ||
var scope = nock('http://uri') | ||
@@ -1620,3 +1621,3 @@ .post('/claim', {some_data: "something"}) | ||
tap.test('accept string as request target', function(t) { | ||
test('accept string as request target', function(t) { | ||
var scope = nock('http://www.example.com') | ||
@@ -1643,3 +1644,3 @@ .get('/') | ||
tap.test('request has path', function(t) { | ||
test('request has path', function(t) { | ||
var scope = nock('http://haspath.com') | ||
@@ -1662,3 +1663,3 @@ .get('/the/path/to/infinity') | ||
tap.test('persists interceptors', function(t) { | ||
test('persists interceptors', function(t) { | ||
var scope = nock('http://persisssists.con') | ||
@@ -1678,3 +1679,3 @@ .persist() | ||
tap.test('(re-)activate after restore', function(t) { | ||
test('(re-)activate after restore', function(t) { | ||
var scope = nock('http://google.com') | ||
@@ -1686,3 +1687,4 @@ .get('/') | ||
http.get('http://google.com', function(res) { | ||
http.get('http://google.com/', function(res) { | ||
res.resume(); | ||
res.on('end', function() { | ||
@@ -1693,2 +1695,3 @@ t.ok(! scope.isDone()); | ||
http.get('http://google.com', function(res) { | ||
res.resume(); | ||
res.on('end', function() { | ||
@@ -1703,3 +1706,3 @@ t.ok(scope.isDone()); | ||
tap.test("allow unmocked option works with https", function(t) { | ||
test("allow unmocked option works with https", function(t) { | ||
t.plan(5) | ||
@@ -1718,3 +1721,3 @@ var scope = nock('https://www.google.com', {allowUnmocked: true}) | ||
}, function(res) { | ||
console.log('OK 2'); | ||
res.resume(); | ||
t.ok(true, 'Google replied to /'); | ||
@@ -1732,3 +1735,2 @@ res.destroy(); | ||
}, function(res) { | ||
console.log('OK 1'); | ||
t.equal(503, res.statusCode, 'real google response status code'); | ||
@@ -1749,3 +1751,4 @@ res.on('data', function() {}); | ||
tap.test('allow unmocked post with json data', function(t) { | ||
test('allow unmocked post with json data', function(t) { | ||
var scope = nock('https://httpbin.org', { allowUnmocked: true }). | ||
@@ -1765,2 +1768,49 @@ get("/abc"). | ||
}); | ||
}); | ||
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(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
75374
14
2237