Comparing version 0.6.3 to 0.7.0
@@ -81,2 +81,6 @@ var path = require('path') | ||
options.getHeader = function(name) { | ||
return getHeader(req, name); | ||
}; | ||
if (options.host && !getHeader(req, 'host')) { | ||
@@ -83,0 +87,0 @@ var hostHeader = options.host; |
@@ -13,2 +13,3 @@ var path = require('path') | ||
, transformRequestBodyFunction | ||
, matchHeaders = [] | ||
, logger = noop; | ||
@@ -33,2 +34,3 @@ | ||
function intercept(uri, method, requestBody) { | ||
var interceptorMatchHeaders = []; | ||
var key = method.toUpperCase() + ' ' + basePath + uri; | ||
@@ -89,2 +91,12 @@ if (typeof(requestBody) !== 'string') { | ||
if (transformRequestBodyFunction) { body = transformRequestBodyFunction(body); } | ||
var checkHeaders = function(header) { | ||
return options.getHeader(header.name) === header.value; | ||
}; | ||
if (!matchHeaders.every(checkHeaders) || | ||
!interceptorMatchHeaders.every(checkHeaders)) { | ||
return false; | ||
} | ||
var matchKey = method + ' ' + proto + '://' + options.host + path; | ||
@@ -108,2 +120,7 @@ if (body) { matchKey += (' ' + body); } | ||
function matchHeader(name, value) { | ||
interceptorMatchHeaders.push({ name: name, value: value }); | ||
return this; | ||
} | ||
var interceptor = { | ||
@@ -116,2 +133,3 @@ _key: key | ||
, filteringPath: filteringPath | ||
, matchHeader: matchHeader | ||
}; | ||
@@ -189,2 +207,7 @@ | ||
} | ||
function matchHeader(name, value) { | ||
matchHeaders.push({ name: name, value: value }); | ||
return this; | ||
} | ||
@@ -205,2 +228,3 @@ function log(newLogger) { | ||
, filteringRequestBody: filteringRequestBody | ||
, matchHeader: matchHeader | ||
, log: log | ||
@@ -207,0 +231,0 @@ }; |
{ "name" : "nock" | ||
, "description" : "HTTP Server mocking for Node.js" | ||
, "tags" : ["Mock", "HTTP", "testing", "isolation"] | ||
, "version" : "0.6.3" | ||
, "version" : "0.7.0" | ||
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "contributors" : |
@@ -135,2 +135,11 @@ # Nock [![Build Status](https://secure.travis-ci.org/pgte/nock.png)](http://travis-ci.org/pgte/nock) | ||
## Request Headers Matching | ||
If you need to match requests only if certain request headers match, you can. | ||
var scope = nock('http://api.myservice.com') | ||
.matchHeader('accept', 'application/json') | ||
.get('/') | ||
.reply(200, {data: "hello world"}) | ||
# Expectations | ||
@@ -212,2 +221,2 @@ | ||
FUCK YEAH. | ||
FUCK YEAH. |
@@ -135,3 +135,3 @@ var nock = require('../.') | ||
var req = http.request({ | ||
var req = http.get({ | ||
host: "www.headdy.com" | ||
@@ -142,6 +142,10 @@ , method: 'GET' | ||
, headers: {'X-My-Headers': 'My custom Header value'} | ||
}, function(res) { | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
t.equivalent(req._headers, {'x-my-headers': 'My custom Header value', 'host': 'www.headdy.com'}); | ||
t.end(); | ||
}); | ||
@@ -164,2 +168,3 @@ | ||
t.equivalent(res.headers, {'x-my-headers': 'My Header value'}); | ||
scope.done(); | ||
t.end(); | ||
@@ -173,2 +178,88 @@ }); | ||
tap.test("match headers", function(t) { | ||
var scope = nock('http://www.headdy.com') | ||
.get('/') | ||
.matchHeader('x-my-headers', 'My custom Header value') | ||
.reply(200, "Hello World!"); | ||
http.get({ | ||
host: "www.headdy.com" | ||
, method: 'GET' | ||
, path: '/' | ||
, port: 80 | ||
, headers: {'X-My-Headers': 'My custom Header value'} | ||
}, function(res) { | ||
res.setEncoding('utf8'); | ||
t.equal(res.statusCode, 200); | ||
res.on('data', function(data) { | ||
t.equal(data, 'Hello World!'); | ||
}); | ||
res.on('end', function() { | ||
scope.done(); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
tap.test("match all headers", function(t) { | ||
var scope = nock('http://api.headdy.com') | ||
.matchHeader('accept', 'application/json') | ||
.get('/one') | ||
.reply(200, { hello: "world" }) | ||
.get('/two') | ||
.reply(200, { a: 1, b: 2, c: 3 }); | ||
var ended = 0; | ||
var end = function() { | ||
scope.done(); | ||
t.end(); | ||
}; | ||
http.get({ | ||
host: "api.headdy.com" | ||
, method: 'GET' | ||
, path: '/one' | ||
, port: 80 | ||
, headers: {'Accept': 'application/json'} | ||
}, function(res) { | ||
res.setEncoding('utf8'); | ||
t.equal(res.statusCode, 200); | ||
res.on('data', function(data) { | ||
t.equal(data, '{"hello":"world"}'); | ||
}); | ||
res.on('end', function() { | ||
if (++ended === 2) { | ||
end(); | ||
} | ||
}); | ||
}); | ||
http.get({ | ||
host: "api.headdy.com" | ||
, method: 'GET' | ||
, path: '/two' | ||
, port: 80 | ||
, headers: {'accept': 'application/json'} | ||
}, function(res) { | ||
res.setEncoding('utf8'); | ||
t.equal(res.statusCode, 200); | ||
res.on('data', function(data) { | ||
t.equal(data, '{"a":1,"b":2,"c":3}'); | ||
}); | ||
res.on('end', function() { | ||
if (++ended === 2) { | ||
end(); | ||
} | ||
}); | ||
}); | ||
}); | ||
tap.test("body data is differentiating", function(t) { | ||
@@ -175,0 +266,0 @@ var doneCount = 0 |
45652
1333
221