Permalink
Browse files

Added request header matching support to the nano test suite. It make…

…s the code in helper.js slightly more complex but imho more extendable. Request header matching can be done in the fixtures by specifying a reqHeaders field (value is a json), see tests/opts/headers.json.
  • Loading branch information...
streunerlein committed May 29, 2012
1 parent afac0e2 commit 0a9bd64f344d75c85a00bdd702d170faa5c1a77a
Showing with 23 additions and 14 deletions.
  1. +23 −14 tests/helpers.js
View
@@ -55,16 +55,17 @@ helpers.nock = function helpersNock(url, fixture) {
, nocks = helpers.loadFixture(fixture + '.json', true)
;
nocks.forEach(function(n) {
- var path = n.path
- , method = n.method || "get"
- , status = n.status || 200
- , response = n.buffer
- ? new Buffer(n.buffer, 'base64')
- : n.response || ""
- , headers = n.headers || {}
- , body = n.base64
- ? new Buffer(n.base64, 'base64').toString()
- : n.body || ""
+ var path = n.path
+ , method = n.method || "get"
+ , status = n.status || 200
+ , response = n.buffer
+ ? new Buffer(n.buffer, 'base64')
+ : n.response || ""
+ , headers = n.headers || {}
+ , reqHeaders = n.reqHeaders || {}
+ , body = n.base64
+ ? new Buffer(n.base64, 'base64').toString()
+ : n.body || ""
;
if(typeof response === "string" && endsWith(response, '.json')) {
@@ -73,13 +74,21 @@ helpers.nock = function helpersNock(url, fixture) {
if(typeof headers === "string" && endsWith(headers, '.json')) {
headers = helpers.loadFixture(path.join(fixture, headers));
}
+ var n = nock(url);
+ var b = body;
if(body==="*") {
- nock(url).filteringRequestBody(function(path) {
+ n = n.filteringRequestBody(function(path) {
return "*";
- })[method](path, "*").reply(status, response, headers);
- } else {
- nock(url)[method](path, body).reply(status, response, headers);
+ });
+ b = "*";
}
+ if(reqHeaders==={}) {
+ for (var k in reqHeaders) {
+ n = n.matchHeader(k, reqHeaders[k]);
+ }
+ }
+
+ n[method](path, b).reply(status, response, headers);
});
nock(url).log(console.log);
return nock(url);

0 comments on commit 0a9bd64

Please sign in to comment.