New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

nock

Package Overview
Dependencies
Maintainers
1
Versions
438
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.13.6 to 0.13.7

4

lib/intercept.js

@@ -7,2 +7,3 @@ var RequestOverrider = require('./request_overrider'),

http = require('http'),
parse = require('url').parse
ClientRequest = http.ClientRequest;

@@ -97,2 +98,3 @@

if (typeof options === 'string') { options = parse(options); }
options.proto = proto;

@@ -133,2 +135,2 @@ interceptors = interceptorsFor(options);

module.exports.removeAll = removeAll;
module.exports.isOn = isOn;
module.exports.isOn = isOn;
{ "name" : "nock"
, "description" : "HTTP Server mocking for Node.js"
, "tags" : ["Mock", "HTTP", "testing", "isolation"]
, "version" : "0.13.6"
, "version" : "0.13.7"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"

@@ -6,0 +6,0 @@ , "contributors" :

@@ -11,3 +11,5 @@ # Nock [![Build Status](https://secure.travis-ci.org/flatiron/nock.png)](http://travis-ci.org/flatiron/nock)

$ npm install nock
```sh
$ npm install nock
```

@@ -18,7 +20,9 @@ # Use

var nock = require('nock');
```js
var nock = require('nock');
var couchdb = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte', email: 'pedro.teixeira@gmail.com'});
var couchdb = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte', email: 'pedro.teixeira@gmail.com'});
```

@@ -41,5 +45,7 @@ This setup says that we will intercept every HTTP call to `http://myapp.iriscouch.com`.

var scope = nock('http://myapp.iriscouch.com')
.post('/users', {username: 'pgte', email: 'pedro.teixeira@gmail.com'})
.reply(201, {ok: true, id: "123ABC", rev: "946B7D1C"});
```js
var scope = nock('http://myapp.iriscouch.com')
.post('/users', {username: 'pgte', email: 'pedro.teixeira@gmail.com'})
.reply(201, {ok: true, id: "123ABC", rev: "946B7D1C"});
```

@@ -52,32 +58,42 @@ The request body can be a string or a JSON object.

var scope = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(404);
```js
var scope = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(404);
```
You can also specify the reply body as a string:
var scope = nock('http://www.google.com')
.get('/')
.reply(200, "Hello from Google!");
```js
var scope = nock('http://www.google.com')
.get('/')
.reply(200, "Hello from Google!");
```
or as a JSON-encoded object:
var scope = nock('http://myapp.iriscouch.com')
.get('/')
.reply(200, {username: 'pgte', email: 'pedro.teixeira@gmail.com', _id: "4324243fsd"});
```js
var scope = nock('http://myapp.iriscouch.com')
.get('/')
.reply(200, {username: 'pgte', email: 'pedro.teixeira@gmail.com', _id: "4324243fsd"});
```
or even as a file:
var scope = nock('http://myapp.iriscouch.com')
.get('/')
.replyWithFile(200, __dirname + '/replies/user.json');
```js
var scope = nock('http://myapp.iriscouch.com')
.get('/')
.replyWithFile(200, __dirname + '/replies/user.json');
```
Instead of an object or a buffer you can also pass in a callback to be evaluated for the value of the response body:
var scope = nock('http://www.google.com')
.filteringRequestBody(/.*/, '*')
.post('/echo', '*')
.reply(201, function(uri, requestBody) {
return requestBody;
});
```js
var scope = nock('http://www.google.com')
.filteringRequestBody(/.*/, '*')
.post('/echo', '*')
.reply(201, function(uri, requestBody) {
return requestBody;
});
```

@@ -88,5 +104,7 @@ ### Specifying Reply Headers

var scope = nock('http://www.headdy.com')
.get('/')
.reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
```js
var scope = nock('http://www.headdy.com')
.get('/')
.reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
```

@@ -97,6 +115,8 @@ ### Default Reply Headers

var scope = nock('http://www.headdy.com')
.defaultReplyHeaders({'X-Powered-By': 'Rails', 'Content-Type': 'application/json'})
.get('/')
.reply(200, 'The default headers should come too');
```js
var scope = nock('http://www.headdy.com')
.defaultReplyHeaders({'X-Powered-By': 'Rails', 'Content-Type': 'application/json'})
.get('/')
.reply(200, 'The default headers should come too');
```

@@ -109,5 +129,7 @@ ## HTTP Verbs

scope('http://my.domain.com')
.intercept('/path', 'PATCH')
.reply(304);
```js
scope('http://my.domain.com')
.intercept('/path', 'PATCH')
.reply(304);
```

@@ -118,4 +140,6 @@ ## Support for HTTP and HTTPS

var scope = nock('https://secure.my.server.com')
// ...
```js
var scope = nock('https://secure.my.server.com')
// ...
```

@@ -126,4 +150,6 @@ ## Non-standard ports

var scope = nock('http://my.server.com:8081')
...
```js
var scope = nock('http://my.server.com:8081')
...
```

@@ -134,11 +160,12 @@ ## Chaining

var scope = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(404)
.post('/users', {username: 'pgte', email: 'pedro.teixeira@gmail.com'})
.reply(201, {ok: true, id: "123ABC", rev: "946B7D1C"})
.get('/users/123ABC')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte', email: 'pedro.teixeira@gmail.com'});
```js
var scope = nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(404)
.post('/users', {username: 'pgte', email: 'pedro.teixeira@gmail.com'})
.reply(201, {ok: true, id: "123ABC", rev: "946B7D1C"})
.get('/users/123ABC')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte', email: 'pedro.teixeira@gmail.com'});
```
## Path filtering

@@ -152,15 +179,19 @@

var scope = nock('http://api.myservice.com')
.filteringPath(/password=[^&]*/g, 'password=XXX')
.get('/users/1?password=XXX')
.reply(200, 'user');
```js
var scope = nock('http://api.myservice.com')
.filteringPath(/password=[^&]*/g, 'password=XXX')
.get('/users/1?password=XXX')
.reply(200, 'user');
```
Or you can use a function:
var scope = nock('http://api.myservice.com')
.filteringPath(function(path) {
return '/ABC';
})
.get('/ABC')
.reply(200, 'user');
```js
var scope = nock('http://api.myservice.com')
.filteringPath(function(path) {
return '/ABC';
})
.get('/ABC')
.reply(200, 'user');
```

@@ -175,15 +206,19 @@ ## Request Body filtering

var scope = nock('http://api.myservice.com')
.filteringRequestBody(/password=[^&]*/g, 'password=XXX')
.post('/users/1', 'data=ABC&password=XXX')
.reply(201, 'OK');
```js
var scope = nock('http://api.myservice.com')
.filteringRequestBody(/password=[^&]*/g, 'password=XXX')
.post('/users/1', 'data=ABC&password=XXX')
.reply(201, 'OK');
```
Or you can use a function:
var scope = nock('http://api.myservice.com')
.filteringRequestBody(function(path) {
return 'ABC';
})
.post('/', 'ABC')
.reply(201, 'OK');
```js
var scope = nock('http://api.myservice.com')
.filteringRequestBody(function(path) {
return 'ABC';
})
.post('/', 'ABC')
.reply(201, 'OK');
```

@@ -194,6 +229,8 @@ ## Request Headers Matching

var scope = nock('http://api.myservice.com')
.matchHeader('accept', 'application/json')
.get('/')
.reply(200, {data: "hello world"})
```js
var scope = nock('http://api.myservice.com')
.matchHeader('accept', 'application/json')
.get('/')
.reply(200, {data: "hello world"})
```

@@ -204,9 +241,11 @@ ## Allow __unmocked__ requests on a mocked hostname

options = {allowUnmocked: true};
var scope = nock('http://my.existing.service.com', options)
.get('/my/url')
.reply(200, 'OK!');
```js
options = {allowUnmocked: true};
var scope = nock('http://my.existing.service.com', options)
.get('/my/url')
.reply(200, 'OK!');
GET /my/url => goes through nock
GET /other/url => actually makes request to the server
// GET /my/url => goes through nock
// GET /other/url => actually makes request to the server
```

@@ -221,11 +260,13 @@ # Expectations

var google = nock('http://google.com')
.get('/')
.reply(200, 'Hello from Google!');
```js
var google = nock('http://google.com')
.get('/')
.reply(200, 'Hello from Google!');
// do some stuff
// do some stuff
setTimeout(function() {
google.done(); // will throw an assertion error if meanwhile a "GET http://google.com" was not performed.
}, 5000);
setTimeout(function() {
google.done(); // will throw an assertion error if meanwhile a "GET http://google.com" was not performed.
}, 5000);
```

@@ -240,3 +281,5 @@ ## .isDone()

nock.cleanAll();
```js
nock.cleanAll();
```

@@ -247,5 +290,8 @@ # Logging

var google = nock('http://google.com')
.log(console.log)
...
```js
var google = nock('http://google.com')
.log(console.log)
...
```
# Restoring

@@ -255,3 +301,5 @@

nock.restore();
```js
nock.restore();
```

@@ -264,3 +312,5 @@ # Turning Nock Off (experimental!)

$ NOCK_OFF=true node my_test.js
```js
$ NOCK_OFF=true node my_test.js
```

@@ -275,11 +325,15 @@ # Recording

nock.recorder.rec();
// Some HTTP calls happen and the nock code necessary to mock
// those calls will be outputted to console
```js
nock.recorder.rec();
// Some HTTP calls happen and the nock code necessary to mock
// those calls will be outputted to console
```
If you just want to capture the generated code into a var as an array you can use:
nock.recorder.rec(true); // :no_output = true
// ... some HTTP calls
var nockCalls = nock.recorder.play();
```js
nock.recorder.rec(true); // :no_output = true
// ... some HTTP calls
var nockCalls = nock.recorder.play();
```

@@ -300,8 +354,10 @@ The `nockCalls` var will contain an array of strings representing the generated code you need.

var scope = nock('http://api.myservice.com')
.filteringRequestBody(function(path) {
return '*';
})
.post('/some_uri', '*')
.reply(200, 'OK');
```js
var scope = nock('http://api.myservice.com')
.filteringRequestBody(function(path) {
return '*';
})
.post('/some_uri', '*')
.reply(200, 'OK');
```

@@ -308,0 +364,0 @@ # License

@@ -1535,2 +1535,24 @@ var nock = require('../.')

tap.test('accept string as request target', function(t) {
var scope = nock('http://www.example.com')
.get('/')
.reply(200, "Hello World!");
http.get('http://www.example.com', function(res) {
t.equal(res.statusCode, 200);
res.on('data', function(data) {
dataCalled = true;
t.ok(data instanceof Buffer, "data should be buffer");
t.equal(data.toString(), "Hello World!", "response should match");
});
res.on('end', function() {
t.ok(dataCalled);
scope.done();
t.end();
});
});
});
tap.test('request has path', function(t) {

@@ -1552,3 +1574,2 @@ var scope = nock('http://haspath.com')

req.end();
});
});
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