Comparing version 0.1.0 to 0.1.2
@@ -27,4 +27,4 @@ var path = require('path') | ||
req.end = function(buffer) { | ||
req.write(buffer); | ||
req.end = function(buffer, encoding) { | ||
req.write(buffer, encoding); | ||
req.emit('end'); | ||
@@ -39,8 +39,8 @@ }; | ||
, interceptor; | ||
requestBody = requestBodyBuffers.map(function(buffer) { | ||
buffer.toString(encoding); | ||
return buffer.toString(encoding); | ||
}).join(''); | ||
body = undefined; // we don't need the request body buffers any more | ||
interceptors = interceptors.filter(function(interceptor) { | ||
@@ -47,0 +47,0 @@ return interceptor.match(options, requestBody); |
@@ -8,3 +8,5 @@ var path = require('path') | ||
var interceptors = {} | ||
, scope; | ||
, scope | ||
, transformPathFunction | ||
, transformRequestBodyFunction; | ||
@@ -29,2 +31,10 @@ function add(key, interceptor) { | ||
var key = method.toUpperCase() + ' ' + basePath + uri; | ||
if (typeof(requestBody) !== 'string') { | ||
try { | ||
requestBody = JSON.stringify(requestBody); | ||
} catch(err) { | ||
throw new Error('Error encoding request body into JSON'); | ||
} | ||
} | ||
if (requestBody) { key += (' ' + requestBody); } | ||
@@ -55,3 +65,9 @@ | ||
var method = options.method || 'GET'; | ||
var matchKey = method + ' http://' + options.host + options.path; | ||
var path = options.path; | ||
if (transformPathFunction) { path = transformPathFunction(path); } | ||
if (typeof(body) !== 'string') { | ||
body = body.toString(); | ||
} | ||
if (transformRequestBodyFunction) { body = transformRequestBodyFunction(body); } | ||
var matchKey = method + ' http://' + options.host + path; | ||
if (body) { matchKey += (' ' + body); } | ||
@@ -61,2 +77,9 @@ return matchKey === this._key; | ||
function filteringPath() { | ||
if (typeof arguments[0] === 'function') { | ||
this.transformFunction = arguments[0]; | ||
} | ||
return this; | ||
} | ||
function discard() { | ||
@@ -72,2 +95,3 @@ remove(this._key, this); | ||
, match: match | ||
, filteringPath: filteringPath | ||
}; | ||
@@ -94,2 +118,42 @@ | ||
function filteringPath() { | ||
var filteringArguments = arguments; | ||
if (arguments[0] instanceof RegExp) { | ||
transformPathFunction = function(path) { | ||
if (path) { | ||
path = path.replace(filteringArguments[0], filteringArguments[1]); | ||
} | ||
return path; | ||
} | ||
} else { | ||
if (typeof (arguments[0]) === 'function') { | ||
transformPathFunction = arguments[0]; | ||
} else { | ||
throw new Error('Invalid arguments: filtering path should be a function or a regular expression'); | ||
} | ||
} | ||
return this; | ||
} | ||
function filteringRequestBody() { | ||
var filteringArguments = arguments; | ||
if (arguments[0] instanceof RegExp) { | ||
transformRequestBodyFunction = function(path) { | ||
if (path) { | ||
path = path.replace(filteringArguments[0], filteringArguments[1]); | ||
} | ||
return path; | ||
} | ||
} else { | ||
if (typeof (arguments[0]) === 'function') { | ||
transformRequestBodyFunction = arguments[0]; | ||
} else { | ||
throw new Error('Invalid arguments: filtering request body should be a function or a regular expression'); | ||
} | ||
} | ||
return this; | ||
} | ||
scope = { | ||
@@ -99,2 +163,4 @@ get: get | ||
, done: done | ||
, filteringPath: filteringPath | ||
, filteringRequestBody: filteringRequestBody | ||
}; | ||
@@ -101,0 +167,0 @@ |
{ "name" : "nock" | ||
, "description" : "HTTP Server mocking for Node.js" | ||
, "tags" : ["Mock", "HTTP", "testing", "isolation"] | ||
, "version" : "0.1.0" | ||
, "version" : "0.1.2" | ||
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "repository" : |
@@ -33,23 +33,23 @@ # Nock | ||
var couchdb = nock('http://myapp.iriscouch.com') | ||
.get('/users/1') | ||
.reply(404); | ||
var scope = nock('http://myapp.iriscouch.com') | ||
.get('/users/1') | ||
.reply(404); | ||
You can also specify the reply body as a string: | ||
var couchdb = nock('http://www.google.com') | ||
.get('/') | ||
.reply(200, "Hello from Google!"); | ||
var scope = nock('http://www.google.com') | ||
.get('/') | ||
.reply(200, "Hello from Google!"); | ||
as a JSON-encoded object: | ||
or as a JSON-encoded object: | ||
var couchdb = nock('http://myapp.iriscouch.com') | ||
.get('/') | ||
.reply(200, {username: 'pgte', email: 'pedro.teixeira@gmail.com', _id: "4324243fsd"}); | ||
var scope = nock('http://myapp.iriscouch.com') | ||
.get('/') | ||
.reply(200, {username: 'pgte', email: 'pedro.teixeira@gmail.com', _id: "4324243fsd"}); | ||
or even as a file: | ||
var couchdb = nock('http://myapp.iriscouch.com') | ||
.get('/') | ||
.replyWithFile(200, __dirname + '/replies/user.json'); | ||
var scope = nock('http://myapp.iriscouch.com') | ||
.get('/') | ||
.replyWithFile(200, __dirname + '/replies/user.json'); | ||
@@ -60,3 +60,3 @@ ## Chaining | ||
var couchdb = nock('http://myapp.iriscouch.com') | ||
var scope = nock('http://myapp.iriscouch.com') | ||
.get('/users/1') | ||
@@ -70,2 +70,46 @@ .reply(404) | ||
## Path filtering | ||
You can also filter the URLs based on a function. | ||
This can be useful, for instance, if you have random or time-dependent data in your URL. | ||
You can use a regexp for replacement, just like String.prototype.replace: | ||
var scope = nock('http://api.myservice.com') | ||
.filterPath(/password=[^&]*/g, 'password=XXX') | ||
.get('/users/1?password=XXX') | ||
.reply(200, 'user'); | ||
Or you can use a function: | ||
var scope = nock('http://api.myservice.com') | ||
.filterPath(function(path) { | ||
return '/ABC'; | ||
}) | ||
.get('/ABC') | ||
.reply(200, 'user'); | ||
## Request Body filtering | ||
You can also filter the request body based on a function. | ||
This can be useful, for instance, if you have random or time-dependent data in your URL. | ||
You can use a regexp for replacement, just like String.prototype.replace: | ||
var scope = nock('http://api.myservice.com') | ||
.filterRequestBody(/password=[^&]*/g, 'password=XXX') | ||
.post('/users/1', 'data=ABC&password=XXX') | ||
.reply(201, 'OK'); | ||
Or you can use a function: | ||
var scope = nock('http://api.myservice.com') | ||
.filterRequestBody(function(path) { | ||
return 'ABC'; | ||
}) | ||
.post('/', 'ABC') | ||
.reply(201, 'OK'); | ||
# Expectations | ||
@@ -87,3 +131,3 @@ | ||
google.done(); // will throw an assertion error if meanwhile a "GET http://google.com" was not performed. | ||
}); | ||
}, 5000); | ||
@@ -90,0 +134,0 @@ # How does it work? |
@@ -21,2 +21,3 @@ var nock = require('../.') | ||
t.ok(dataCalled); | ||
scope.done(); | ||
t.end(); | ||
@@ -28,3 +29,2 @@ }); | ||
t.equal(data.toString(), "Hello World!", "response should match"); | ||
scope.done(); | ||
}); | ||
@@ -91,2 +91,3 @@ | ||
t.ok(dataCalled); | ||
scope.done(); | ||
t.end(); | ||
@@ -98,3 +99,2 @@ }); | ||
t.equal(data.toString(), "OK!", "response should match"); | ||
scope.done(); | ||
}); | ||
@@ -108,7 +108,87 @@ | ||
tap.test("headers work", function(t) { | ||
t.end(); | ||
var scope = nock('http://www.headdy.com') | ||
.get('/') | ||
.reply(200, "Hello World!", {'X-My-Headers': 'My Header value'}); | ||
var req = http.request({ | ||
host: "www.headdy.com" | ||
, method: 'GET' | ||
, path: '/' | ||
, port: 80 | ||
}, function(res) { | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
t.similar(res.headers, {'X-My-Headers': 'My Header value'}); | ||
t.end(); | ||
}); | ||
}); | ||
req.end(); | ||
}); | ||
tap.test("body data is differentiating", function(t) { | ||
t.end(); | ||
var doneCount = 0 | ||
, scope = nock('http://www.boddydiff.com') | ||
.post('/', 'abc') | ||
.reply(200, "Hey 1") | ||
.post('/', 'def') | ||
.reply(200, "Hey 2"); | ||
function done(t) { | ||
doneCount += 1; | ||
if (doneCount === 2) { | ||
scope.di | ||
} | ||
t.end(); | ||
}; | ||
t.test("A", function(t) { | ||
var req = http.request({ | ||
host: "www.boddydiff.com" | ||
, method: 'POST' | ||
, path: '/' | ||
, port: 80 | ||
}, function(res) { | ||
var dataCalled = false; | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
t.ok(dataCalled); | ||
done(t); | ||
}); | ||
res.on('data', function(data) { | ||
dataCalled = true; | ||
t.ok(data instanceof Buffer, "data should be buffer"); | ||
t.equal(data.toString(), "Hey 1", "response should match"); | ||
}); | ||
}); | ||
req.end('abc'); | ||
}); | ||
t.test("B", function(t) { | ||
var req = http.request({ | ||
host: "www.boddydiff.com" | ||
, method: 'POST' | ||
, path: '/' | ||
, port: 80 | ||
}, function(res) { | ||
var dataCalled = false; | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
t.ok(dataCalled); | ||
done(t); | ||
}); | ||
res.on('data', function(data) { | ||
dataCalled = true; | ||
t.ok(data instanceof Buffer, "data should be buffer"); | ||
t.equal(data.toString(), "Hey 2", "response should match"); | ||
}); | ||
}); | ||
req.end('def'); | ||
}); | ||
}); | ||
@@ -167,2 +247,3 @@ | ||
t.ok(dataCalled); | ||
scope.done(); | ||
t.end(); | ||
@@ -174,3 +255,2 @@ }); | ||
t.equal(data.toString(), "Hello World!", "response should match"); | ||
scope.done(); | ||
}); | ||
@@ -202,2 +282,3 @@ | ||
t.ok(dataCalled); | ||
scope.done(); | ||
t.end(); | ||
@@ -209,3 +290,2 @@ }); | ||
t.equal(data, "SGVsbG8gV29ybGQh", "response should match base64 encoding"); | ||
scope.done(); | ||
}); | ||
@@ -278,2 +358,95 @@ | ||
}); | ||
tap.test("filter path with function", function(t) { | ||
var scope = nock('http://www.filterurls.com') | ||
.filteringPath(function(path) { | ||
return '/?a=2&b=1'; | ||
}) | ||
.get('/?a=2&b=1') | ||
.reply(200, "Hello World!"); | ||
var req = http.request({ | ||
host: "www.filterurls.com" | ||
, method: 'GET' | ||
, path: '/?a=1&b=2' | ||
, port: 80 | ||
}, function(res) { | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
req.end(); | ||
}); | ||
tap.test("filter path with regexp", function(t) { | ||
var scope = nock('http://www.filterurlswithregexp.com') | ||
.filteringPath(/\d/g, '3') | ||
.get('/?a=3&b=3') | ||
.reply(200, "Hello World!"); | ||
var req = http.request({ | ||
host: "www.filterurlswithregexp.com" | ||
, method: 'GET' | ||
, path: '/?a=1&b=2' | ||
, port: 80 | ||
}, function(res) { | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
req.end(); | ||
}); | ||
tap.test("filter body with function", function(t) { | ||
var scope = nock('http://www.filterboddiez.com') | ||
.filteringRequestBody(function(body) { | ||
t.equal(body, 'mamma mia'); | ||
return 'mamma tua'; | ||
}) | ||
.post('/', 'mamma tua') | ||
.reply(200, "Hello World!"); | ||
var req = http.request({ | ||
host: "www.filterboddiez.com" | ||
, method: 'POST' | ||
, path: '/' | ||
, port: 80 | ||
}, function(res) { | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
req.end('mamma mia'); | ||
}); | ||
tap.test("filter body with regexp", function(t) { | ||
var scope = nock('http://www.filterboddiezregexp.com') | ||
.filteringRequestBody(/mia/, 'nostra') | ||
.post('/', 'mamma nostra') | ||
.reply(200, "Hello World!"); | ||
var req = http.request({ | ||
host: "www.filterboddiezregexp.com" | ||
, method: 'POST' | ||
, path: '/' | ||
, port: 80 | ||
}, function(res) { | ||
t.equal(res.statusCode, 200); | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
req.end('mamma mia'); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21313
578
132
2