Socket
Socket
Sign inDemoInstall

nock

Package Overview
Dependencies
Maintainers
1
Versions
430
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nock - npm Package Compare versions

Comparing version 0.2.2 to 0.3.0

38

lib/intercept.js

@@ -43,2 +43,19 @@ var path = require('path')

function getHeader(request, name) {
if (!request._headers) return;
var key = name.toLowerCase();
return request._headers[key];
}
function setHeader(request, name, value) {
var key = name.toLowerCase();
request._headers = request._headers || {};
request._headerNames = request._headerNames || {};
request._headers[key] = value;
request._headerNames[key] = name;
}
function processRequest(interceptors, options, callback) {

@@ -52,2 +69,23 @@ var req = new EventEmitter()

if (options.headers) {
var headers = options.headers;
var keys = Object.keys(headers);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
setHeader(req, key, headers[key]);
};
}
if (options.host && !getHeader(req, 'host')) {
var hostHeader = options.host;
if (options.host && +options.port !== 80) {
hostHeader += ':' + options.port;
}
setHeader(req, 'Host', hostHeader);
}
req.write = function(buffer, encoding) {

@@ -54,0 +92,0 @@ if (buffer && !aborted) {

3

package.json
{ "name" : "nock"
, "description" : "HTTP Server mocking for Node.js"
, "tags" : ["Mock", "HTTP", "testing", "isolation"]
, "version" : "0.2.2"
, "version" : "0.3.0"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "contributors" :
[ "Roly Fentanes"
, "Alexander Simmerl"
]

@@ -9,0 +10,0 @@ , "repository" :

@@ -29,2 +29,13 @@ # Nock

## Specifying request body
You can specify the request body to be matched as the second argument to the `get`, `post`, `put` or `delete` specifications like this:
var scope = nock('http://myapp.iriscouch.com')
.post('/users', {username: 'pgte', email: 'pedro.teixeira@gmail.com'})
.reply(201, {ok: true, id: "123ABC", rev: "946B7D1C"});
The request body can be a string or a JSON object.
## Specifying replies

@@ -56,2 +67,10 @@

### Specifying reply headers
You can specify the reply headers like this:
var scope = nock('http://www.headdy.com')
.get('/')
.reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
## HTTP Verbs

@@ -58,0 +77,0 @@

@@ -103,2 +103,21 @@ var nock = require('../.')

tap.test("request headers exposed", function(t) {
var scope = nock('http://www.headdy.com')
.get('/')
.reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
var req = http.request({
host: "www.headdy.com"
, method: 'GET'
, path: '/'
, port: 80
, headers: {'X-My-Headers': 'My custom Header value'}
});
console.dir(req._headers)
t.equivalent(req._headers, {'x-my-headers': 'My custom Header value', 'host': 'www.headdy.com'});
t.end();
});
tap.test("headers work", function(t) {

@@ -105,0 +124,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc