Comparing version
@@ -103,2 +103,10 @@ var path = require('path') | ||
var matchStringOrRegexp = function(target, pattern) { | ||
if (pattern instanceof RegExp) { | ||
return target.match(pattern); | ||
} else { | ||
return target === pattern; | ||
} | ||
} | ||
function match(options, body) { | ||
@@ -118,3 +126,3 @@ var method = options.method || 'GET' | ||
var checkHeaders = function(header) { | ||
return options.getHeader(header.name) === header.value; | ||
return matchStringOrRegexp(options.getHeader(header.name), header.value); | ||
}; | ||
@@ -151,3 +159,3 @@ | ||
var checkHeaders = function(header) { | ||
return options.getHeader && options.getHeader(header.name) === header.value; | ||
return options.getHeader && matchStringOrRegexp(options.getHeader(header.name), header.value); | ||
}; | ||
@@ -154,0 +162,0 @@ if (!matchHeaders.every(checkHeaders) || |
{ "name" : "nock" | ||
, "description" : "HTTP Server mocking for Node.js" | ||
, "tags" : ["Mock", "HTTP", "testing", "isolation"] | ||
, "version" : "0.14.4" | ||
, "version" : "0.15.0" | ||
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "contributors" : |
@@ -224,2 +224,11 @@ # Nock [](http://travis-ci.org/flatiron/nock) | ||
You can also use a regexp for the header body. | ||
```js | ||
var scope = nock('http://api.myservice.com') | ||
.matchHeader('User-Agent', /Mozilla\/.*/) | ||
.get('/') | ||
.reply(200, {data: "hello world"}) | ||
``` | ||
## Allow __unmocked__ requests on a mocked hostname | ||
@@ -226,0 +235,0 @@ |
@@ -388,2 +388,30 @@ var nock = require('../.') | ||
tap.test("match headers with regexp", function(t) { | ||
var scope = nock('http://www.headier.com') | ||
.get('/') | ||
.matchHeader('x-my-headers', /My He.d.r [0-9.]+/) | ||
.reply(200, "Hello World!"); | ||
http.get({ | ||
host: "www.headier.com" | ||
, method: 'GET' | ||
, path: '/' | ||
, port: 80 | ||
, headers: {'X-My-Headers': 'My Header 1.0'} | ||
}, 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) { | ||
@@ -390,0 +418,0 @@ var scope = nock('http://api.headdy.com') |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
71362
1.51%2081
1.46%373
2.47%24
4.35%