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.3.0 to 0.3.1

131

lib/intercept.js

@@ -140,2 +140,3 @@ var path = require('path')

response.headers = interceptor.headers || {};
response.readable = true;
responseBody = interceptor.body;

@@ -162,2 +163,132 @@ if (! Buffer.isBuffer(responseBody)) {

response.pipe = function(dest, options) {
var source = this;
function ondata(chunk) {
if (dest.writable) {
if (false === dest.write(chunk)) source.pause();
}
}
function ondrain() {
if (source.readable) source.resume();
}
source.on('data', ondata);
dest.on('drain', ondrain);
// If the 'end' option is not supplied, dest.end() will be called when
// source gets the 'end' or 'close' events. Only dest.end() once, and
// only when all sources have ended.
if (!options || options.end !== false) {
dest._pipeCount = dest._pipeCount || 0;
dest._pipeCount++;
source.on('end', onend);
source.on('close', onclose);
}
var didOnEnd = false;
function onend() {
if (didOnEnd) return;
didOnEnd = true;
dest._pipeCount--;
// remove the listeners
cleanup();
if (dest._pipeCount > 0) {
// waiting for other incoming streams to end.
return;
}
dest.end();
}
function onclose() {
if (didOnEnd) return;
didOnEnd = true;
dest._pipeCount--;
// remove the listeners
cleanup();
if (dest._pipeCount > 0) {
// waiting for other incoming streams to end.
return;
}
dest.destroy();
}
// don't leave dangling pipes when there are errors.
function onerror(er) {
cleanup();
if (this.listeners('error').length === 0) {
throw er; // Unhandled stream error in pipe.
}
}
source.on('error', onerror);
dest.on('error', onerror);
// guarantee that source streams can be paused and resumed, even
// if the only effect is to proxy the event back up the pipe chain.
if (!source.pause) {
source.pause = function() {
source.emit('pause');
};
}
if (!source.resume) {
source.resume = function() {
source.emit('resume');
};
}
function onpause() {
source.pause();
}
dest.on('pause', onpause);
function onresume() {
if (source.readable) source.resume();
}
dest.on('resume', onresume);
// remove all the event listeners that were added.
function cleanup() {
source.removeListener('data', ondata);
dest.removeListener('drain', ondrain);
source.removeListener('end', onend);
source.removeListener('close', onclose);
dest.removeListener('pause', onpause);
dest.removeListener('resume', onresume);
source.removeListener('error', onerror);
dest.removeListener('error', onerror);
source.removeListener('end', cleanup);
source.removeListener('close', cleanup);
dest.removeListener('end', cleanup);
dest.removeListener('close', cleanup);
}
source.on('end', cleanup);
source.on('close', cleanup);
dest.on('end', cleanup);
dest.on('close', cleanup);
dest.emit('pipe', source);
};
next.push(function() {

@@ -164,0 +295,0 @@ if (encoding) {

4

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

@@ -22,2 +22,2 @@ , "contributors" :

, "scripts": { "test": "node_modules/.bin/tap tests" }
}
}

@@ -1,4 +0,6 @@

var nock = require('../.')
var http = require('http');
var tap = require('tap');
var nock = require('../.')
var http = require('http');
var util = require('util');
var events = require('events');
var tap = require('tap');

@@ -117,3 +119,2 @@ tap.test("get gets mocked", function(t) {

console.dir(req._headers)
t.equivalent(req._headers, {'x-my-headers': 'My custom Header value', 'host': 'www.headdy.com'});

@@ -551,2 +552,104 @@ t.end();

tap.test("response pipe", function(t) {
var dest = (function() {
function Constructor() {
events.EventEmitter.call(this);
this.buffer = new Buffer(0);
this.writable = true;
}
util.inherits(Constructor, events.EventEmitter);
Constructor.prototype.end = function() {
this.emit('end');
};
Constructor.prototype.write = function(chunk) {
var buf = new Buffer(this.buffer.length + chunk.length);
this.buffer.copy(buf);
chunk.copy(buf, this.buffer.length);
this.buffer = buf;
return true;
};
return new Constructor();
})();
var scope = nock('http://pauseme.com')
.get('/')
.reply(200, 'nobody');
var req = http.get({
host: 'pauseme.com'
, path: '/'
}, function(res) {
dest.on('pipe', function() {
t.pass('should emit "pipe" event')
});
dest.on('end', function() {
scope.done();
t.equal(dest.buffer.toString(), 'nobody');
t.end();
});
res.pipe(dest);
});
});
tap.test("response pipe without implicit end", function(t) {
var dest = (function() {
function Constructor() {
events.EventEmitter.call(this);
this.buffer = new Buffer(0);
this.writable = true;
}
util.inherits(Constructor, events.EventEmitter);
Constructor.prototype.end = function() {
this.emit('end');
};
Constructor.prototype.write = function(chunk) {
var buf = new Buffer(this.buffer.length + chunk.length);
this.buffer.copy(buf);
chunk.copy(buf, this.buffer.length);
this.buffer = buf;
return true;
};
return new Constructor();
})();
var scope = nock('http://pauseme.com')
.get('/')
.reply(200, 'nobody');
var req = http.get({
host: 'pauseme.com'
, path: '/'
}, function(res) {
dest.on('end', function() {
t.fail('should not call end implicitly');
});
res.on('end', function() {
scope.done();
t.pass('should emit end event');
t.end();
});
res.pipe(dest, {end: false});
});
});
tap.test("chaining API", function(t) {

@@ -553,0 +656,0 @@ var scope = nock('http://chainchomp.com')

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