Comparing version
@@ -397,3 +397,3 @@ 'use strict'; | ||
else { | ||
if (self._assertions[matchIndex].sendResponse(res)) { | ||
if (self._assertions[matchIndex].sendResponse(req, res)) { | ||
self._assertions.splice(matchIndex, 1)[0]; | ||
@@ -400,0 +400,0 @@ } |
@@ -27,6 +27,7 @@ 'use strict'; | ||
function createResponse(response) { | ||
function createResponse(request, response) { | ||
var self = this; | ||
var headers = this.response.headers || this._defaultReplyHeaders; | ||
var headers = typeof this.response.headers === 'function' ? this.response.headers(request) : this.response.headers || this._defaultReplyHeaders; | ||
this.response.body = typeof this.response.body === 'function' ? this.response.body(request) : this.response.body; | ||
@@ -309,5 +310,6 @@ response.writeHead(this.response.statusCode, headers); | ||
* | ||
* @param {object} request The request object from the hock server | ||
* @param {object} response The response object from the hock server | ||
*/ | ||
Request.prototype.sendResponse = function(response) { | ||
Request.prototype.sendResponse = function(request, response) { | ||
var self = this; | ||
@@ -318,7 +320,7 @@ this._count++; | ||
setTimeout(function() { | ||
createResponse.call(self, response); | ||
createResponse.call(self, request, response); | ||
}, this._delay); | ||
} | ||
else { | ||
createResponse.call(this, response); | ||
createResponse.call(this, request, response); | ||
} | ||
@@ -325,0 +327,0 @@ |
{ | ||
"name": "hock", | ||
"description": "A mocking server for HTTP requests", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"author": "Maciej MaĆecki <me@mmalecki.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -90,2 +90,17 @@ # hock [](http://travis-ci.org/mmalecki/hock) [](https://gitter.im/mmalecki/hock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
You can also provide functions instead of concrete values. These functions will be called with the matching incoming http request, and it useful in cases where the response body or headers need to be constructed based on the incoming request data: | ||
```Javascript | ||
// returns the current hockServer instance | ||
req.reply( | ||
statusCode, | ||
function replyWithBody(request) { | ||
return body; | ||
}, | ||
function replyWithHeaders(request) { | ||
return responseHeaders; | ||
} | ||
); | ||
``` | ||
## Multiple matching requests | ||
@@ -92,0 +107,0 @@ |
var http = require('http'), | ||
url = require('url'), | ||
should = require('should'), | ||
@@ -194,2 +195,41 @@ shouldHttp = require('should-http'), | ||
it('should work with response body function', function(done) { | ||
hockInstance | ||
.get('/url?key=value') | ||
.reply(200, function(request) { | ||
const query = url.parse(request.url, true).query; | ||
return { 'hock': 'ok', key: query.key }; | ||
}); | ||
request('http://localhost:' + PORT + '/url?key=value', function(err, res, body) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.statusCode.should.equal(200); | ||
JSON.parse(body).should.eql({ 'hock': 'ok', key: 'value' }); | ||
done(); | ||
}); | ||
}); | ||
it('should work with response header function', function(done) { | ||
hockInstance | ||
.get('/url?key=value') | ||
.reply(200, { 'hock': 'ok' }, function(request) { | ||
const query = url.parse(request.url, true).query; | ||
return { | ||
'x-request-key': query.key, | ||
}; | ||
}); | ||
request('http://localhost:' + PORT + '/url?key=value', function(err, res, body) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.statusCode.should.equal(200); | ||
res.headers['x-request-key'].should.eql('value'); | ||
done(); | ||
}); | ||
}); | ||
after(function (done) { | ||
@@ -196,0 +236,0 @@ httpServer.close(done); |
53217
3.99%1428
2.51%256
6.22%