Comparing version
var http = require('http'), | ||
Request = require('./request'), | ||
_ = require('underscore'); | ||
deepEqual = require('deep-equal'); | ||
@@ -54,2 +54,31 @@ /** | ||
/** | ||
* Hock.hasRoute | ||
* | ||
* @description test if there is a request on the assertions queue | ||
* | ||
* @param {String} method the method of the request to match | ||
* @param {String} url the route of the request to match | ||
* @param {String} [body] optionally - use if you set a body | ||
* @param {object} [headers] optionally - use if you set a header | ||
* @returns {Boolean} | ||
*/ | ||
Hock.prototype.hasRoute = function (method, url, body, headers) { | ||
if (!body) { | ||
body = ''; | ||
} | ||
if (!headers) { | ||
headers = {}; | ||
} | ||
var found = this._assertions.filter(function(request) { | ||
return request.method === method | ||
&& request.url === url | ||
&& request.body === body | ||
&& deepEqual(request.headers, headers); | ||
}) | ||
return !!found.length; | ||
}; | ||
/** | ||
* Hock.done | ||
@@ -56,0 +85,0 @@ * |
var fs = require('fs'), | ||
Stream = require('stream'), | ||
_ = require('underscore'); | ||
Stream = require('stream'); | ||
@@ -33,5 +32,7 @@ /** | ||
_.forEach(_.keys(this.headers), function(key) { | ||
Object.keys(this.headers).forEach(function (key) { | ||
self.headers[key.toLowerCase()] = self.headers[key]; | ||
delete self.headers[key]; | ||
if (key.toLowerCase() !== key) { | ||
delete self.headers[key]; | ||
} | ||
}); | ||
@@ -211,4 +212,4 @@ | ||
var match = true; | ||
_.forEach(_.keys(self.headers), function (key) { | ||
if (request.headers[key] && self.headers[key] !== request.headers[key]) { | ||
Object.keys(self.headers).forEach(function (key) { | ||
if (self.headers[key] && self.headers[key] !== request.headers[key]) { | ||
match = false; | ||
@@ -215,0 +216,0 @@ } |
{ | ||
"name": "hock", | ||
"description": "A mocking server for HTTP requests", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"author": "Maciej Małecki <me@mmalecki.com>", | ||
@@ -17,7 +17,6 @@ "contributors": [ | ||
"keywords": [ | ||
"mock", "http", "test" | ||
"mock", | ||
"http", | ||
"test" | ||
], | ||
"dependencies": { | ||
"underscore": "1.4.x" | ||
}, | ||
"devDependencies": { | ||
@@ -34,3 +33,6 @@ "request": "2.20.x", | ||
"node": ">=0.8.x" | ||
}, | ||
"dependencies": { | ||
"deep-equal": "0.2.1" | ||
} | ||
} |
@@ -216,2 +216,61 @@ var should = require('should'), | ||
}); | ||
describe("test if route exists", function() { | ||
before(function(done) { | ||
hock.createHock(PORT, function(err, hockServer) { | ||
should.not.exist(err); | ||
should.exist(hockServer); | ||
server = hockServer; | ||
done(); | ||
}); | ||
}); | ||
it('should allow testing for url', function(done) { | ||
server | ||
.get('/url?password=foo') | ||
.reply(200, { 'hock': 'ok' }) | ||
.get('/arti') | ||
.reply(200, { 'hock': 'ok' }); | ||
server.hasRoute('GET', '/url?password=foo').should.equal(true); | ||
server.hasRoute('GET', '/arti').should.equal(true); | ||
server.hasRoute('GET', '/notexist').should.equal(false); | ||
done(); | ||
}); | ||
it('matches the header', function(done) { | ||
server | ||
.get('/url?password=foo') | ||
.reply(200, { 'hock': 'ok' }) | ||
.get('/artischocko', { 'foo-type': 'artischocke' }) | ||
.reply(200, { 'hock': 'ok' }); | ||
server | ||
.hasRoute('GET', '/bla?password=foo', null, { 'content-type': 'plain/text' }) | ||
.should.equal(false); | ||
server | ||
.hasRoute('GET', '/artischocko', null, { 'foo-type': 'artischocke' }) | ||
.should.equal(true); | ||
done(); | ||
}); | ||
it('matches the body', function(done) { | ||
server | ||
.get('/url?password=foo') | ||
.reply(200, { 'hock': 'ok' }) | ||
.post('/artischocko', 'enteente') | ||
.reply(200, { 'hock': 'ok' }); | ||
server.hasRoute('GET', '/bla?password=foo', 'testing').should.equal(false); | ||
server.hasRoute('POST', '/artischocko', 'enteente').should.equal(true); | ||
done(); | ||
}); | ||
after(function(done) { | ||
server.close(done); | ||
}); | ||
}); | ||
}); |
44168
8.37%12
9.09%1188
9.7%+ Added
+ Added
- Removed
- Removed