Comparing version 12.0.3 to 13.0.0-beta.1
@@ -76,3 +76,3 @@ 'use strict' | ||
const nockDone = function() { | ||
const nockDone = function () { | ||
_mode.finish(fixture, options, context) | ||
@@ -96,3 +96,3 @@ } | ||
const wild = { | ||
setup: function() { | ||
setup: function () { | ||
cleanAll() | ||
@@ -104,7 +104,7 @@ recorder.restore() | ||
start: function() { | ||
start: function () { | ||
return load() // don't load anything but get correct context | ||
}, | ||
finish: function() { | ||
finish: function () { | ||
// nothing to do | ||
@@ -115,3 +115,3 @@ }, | ||
const dryrun = { | ||
setup: function() { | ||
setup: function () { | ||
recorder.restore() | ||
@@ -124,3 +124,3 @@ cleanAll() | ||
start: function(fixture, options) { | ||
start: function (fixture, options) { | ||
const contexts = load(fixture, options) | ||
@@ -132,3 +132,3 @@ | ||
finish: function() { | ||
finish: function () { | ||
// nothing to do | ||
@@ -139,3 +139,3 @@ }, | ||
const record = { | ||
setup: function() { | ||
setup: function () { | ||
recorder.restore() | ||
@@ -148,3 +148,3 @@ recorder.clear() | ||
start: function(fixture, options) { | ||
start: function (fixture, options) { | ||
if (!fs) { | ||
@@ -168,3 +168,3 @@ throw new Error('no fs') | ||
finish: function(fixture, options, context) { | ||
finish: function (fixture, options, context) { | ||
if (context.isRecording) { | ||
@@ -188,3 +188,3 @@ let outputs = recorder.outputs() | ||
const lockdown = { | ||
setup: function() { | ||
setup: function () { | ||
recorder.restore() | ||
@@ -197,7 +197,7 @@ recorder.clear() | ||
start: function(fixture, options) { | ||
start: function (fixture, options) { | ||
return load(fixture, options) | ||
}, | ||
finish: function() { | ||
finish: function () { | ||
// nothing to do | ||
@@ -210,3 +210,3 @@ }, | ||
scopes: [], | ||
assertScopesFinished: function() { | ||
assertScopesFinished: function () { | ||
assertScopes(this.scopes, fixture) | ||
@@ -276,3 +276,3 @@ }, | ||
Back.setMode = function(mode) { | ||
Back.setMode = function (mode) { | ||
if (!(mode in Modes)) { | ||
@@ -279,0 +279,0 @@ throw new Error(`Unknown mode: ${mode}`) |
@@ -31,3 +31,3 @@ 'use strict' | ||
/// lowercase host names | ||
;['hostname', 'host'].forEach(function(attr) { | ||
;['hostname', 'host'].forEach(function (attr) { | ||
if (options[attr]) { | ||
@@ -70,3 +70,3 @@ options[attr] = options[attr].toLowerCase() | ||
debug('overriding requests') | ||
;['http', 'https'].forEach(function(proto) { | ||
;['http', 'https'].forEach(function (proto) { | ||
debug('- overriding request for', proto) | ||
@@ -95,3 +95,3 @@ | ||
// https://nodejs.org/api/http.html#http_http_request_url_options_callback | ||
module.request = function(input, options, callback) { | ||
module.request = function (input, options, callback) { | ||
return newRequest(proto, overriddenRequest.bind(module), [ | ||
@@ -104,3 +104,3 @@ input, | ||
// https://nodejs.org/api/http.html#http_http_get_options_callback | ||
module.get = function(input, options, callback) { | ||
module.get = function (input, options, callback) { | ||
const req = newRequest(proto, overriddenGet.bind(module), [ | ||
@@ -413,7 +413,4 @@ input, | ||
function percentEncode(str) { | ||
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { | ||
return `%${c | ||
.charCodeAt(0) | ||
.toString(16) | ||
.toUpperCase()}` | ||
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { | ||
return `%${c.charCodeAt(0).toString(16).toUpperCase()}` | ||
}) | ||
@@ -460,3 +457,3 @@ } | ||
case Array.isArray(value): { | ||
value = value.map(function(val, idx) { | ||
value = value.map(function (val, idx) { | ||
return formatQueryValue(idx, val, stringFormattingFn)[1] | ||
@@ -467,3 +464,3 @@ }) | ||
case typeof value === 'object': { | ||
value = Object.entries(value).reduce(function(acc, [subKey, subVal]) { | ||
value = Object.entries(value).reduce(function (acc, [subKey, subVal]) { | ||
const subPair = formatQueryValue(subKey, subVal, stringFormattingFn) | ||
@@ -470,0 +467,0 @@ acc[subPair[0]] = subPair[1] |
@@ -24,7 +24,7 @@ 'use strict' | ||
if (common.isStream(body)) { | ||
body.on('data', function(chunk) { | ||
body.on('data', function (chunk) { | ||
data += Buffer.isBuffer(chunk) ? chunk.toString() : chunk | ||
}) | ||
body.once('end', function() { | ||
body.once('end', function () { | ||
ended = true | ||
@@ -38,5 +38,5 @@ }) | ||
// the `if` statement above. | ||
common.setTimeout(function() { | ||
common.setTimeout(function () { | ||
if (common.isStream(body) && !ended) { | ||
body.once('end', function() { | ||
body.once('end', function () { | ||
self.end(data) | ||
@@ -43,0 +43,0 @@ }) |
@@ -124,3 +124,3 @@ 'use strict' | ||
// matching instances. | ||
interceptors.some(function(thisInterceptor, i) { | ||
interceptors.some(function (thisInterceptor, i) { | ||
return thisInterceptor === interceptor ? interceptors.splice(i, 1) : false | ||
@@ -131,4 +131,4 @@ }) | ||
function removeAll() { | ||
Object.keys(allInterceptors).forEach(function(key) { | ||
allInterceptors[key].interceptors.forEach(function(interceptor) { | ||
Object.keys(allInterceptors).forEach(function (key) { | ||
allInterceptors[key].interceptors.forEach(function (interceptor) { | ||
interceptor.scope.keyedInterceptors = {} | ||
@@ -242,3 +242,3 @@ }) | ||
process.nextTick( | ||
function() { | ||
function () { | ||
this.emit('error', error) | ||
@@ -305,3 +305,3 @@ }.bind(this) | ||
common.setImmediate( | ||
function() { | ||
function () { | ||
const error = new NetConnectNotAllowedError( | ||
@@ -375,3 +375,3 @@ options.host, | ||
common.overrideRequests(function(proto, overriddenRequest, args) { | ||
common.overrideRequests(function (proto, overriddenRequest, args) { | ||
// NOTE: overriddenRequest is already bound to its module. | ||
@@ -378,0 +378,0 @@ |
@@ -134,3 +134,3 @@ 'use strict' | ||
common.setImmediate(function() { | ||
common.setImmediate(function () { | ||
req.emit('drain') | ||
@@ -137,0 +137,0 @@ }) |
@@ -235,6 +235,6 @@ 'use strict' | ||
responseBody.pause() | ||
responseBody.on('data', function(d) { | ||
responseBody.on('data', function (d) { | ||
response.push(d) | ||
}) | ||
responseBody.on('end', function() { | ||
responseBody.on('end', function () { | ||
response.push(null) | ||
@@ -244,3 +244,3 @@ // https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_message_complete | ||
}) | ||
responseBody.on('error', function(err) { | ||
responseBody.on('error', function (err) { | ||
response.emit('error', err) | ||
@@ -247,0 +247,0 @@ }) |
@@ -214,3 +214,3 @@ 'use strict' | ||
// We override the requests so that we can save information on them before executing. | ||
common.overrideRequests(function(proto, overriddenRequest, rawArgs) { | ||
common.overrideRequests(function (proto, overriddenRequest, rawArgs) { | ||
const { options, callback } = common.normalizeClientRequestArgs(...rawArgs) | ||
@@ -227,7 +227,7 @@ const bodyChunks = [] | ||
const req = overriddenRequest(options, function(res) { | ||
const req = overriddenRequest(options, function (res) { | ||
debug(thisRecordingId, 'intercepting', proto, 'request to record') | ||
// We put our 'end' listener to the front of the listener array. | ||
res.once('end', function() { | ||
res.once('end', function () { | ||
debug(thisRecordingId, proto, 'intercepted request ended') | ||
@@ -289,3 +289,3 @@ | ||
const { setEncoding } = res | ||
res.setEncoding = function(newEncoding) { | ||
res.setEncoding = function (newEncoding) { | ||
encoding = newEncoding | ||
@@ -298,3 +298,3 @@ return setEncoding.apply(this, arguments) | ||
const origResPush = res.push | ||
res.push = function(data) { | ||
res.push = function (data) { | ||
if (data) { | ||
@@ -336,3 +336,3 @@ if (encoding) { | ||
const oldWrite = req.write | ||
req.write = function(chunk, encoding) { | ||
req.write = function (chunk, encoding) { | ||
if (typeof chunk !== 'undefined') { | ||
@@ -350,3 +350,3 @@ recordChunk(chunk, encoding) | ||
const oldEnd = req.end | ||
req.end = function(chunk, encoding, callback) { | ||
req.end = function (chunk, encoding, callback) { | ||
debug('req.end') | ||
@@ -353,0 +353,0 @@ if (typeof chunk === 'function') { |
@@ -12,3 +12,2 @@ 'use strict' | ||
const { EventEmitter } = require('events') | ||
const util = require('util') | ||
const Interceptor = require('./interceptor') | ||
@@ -174,3 +173,3 @@ | ||
if (arguments[0] instanceof RegExp) { | ||
return function(candidate) { | ||
return function (candidate) { | ||
/* istanbul ignore if */ | ||
@@ -311,13 +310,6 @@ if (typeof candidate !== 'string') { | ||
// Use a noop deprecate util instead calling emitWarning directly so we get --no-deprecation and single warning behavior for free. | ||
const emitAsteriskDeprecation = util.deprecate( | ||
() => {}, | ||
'Skipping body matching using "*" is deprecated. Set the definition body to undefined instead.', | ||
'NOCK1579' | ||
) | ||
function define(nockDefs) { | ||
const scopes = [] | ||
nockDefs.forEach(function(nockDef) { | ||
nockDefs.forEach(function (nockDef) { | ||
const nscope = getScopeFromDefinition(nockDef) | ||
@@ -341,13 +333,2 @@ const npath = nockDef.path | ||
let { body } = nockDef | ||
if (body === '*') { | ||
// In previous versions, it was impossible to NOT filter on request bodies. This special value | ||
// is sniffed out for users manipulating the definitions and not wanting to match on the | ||
// request body. For newer versions, users should remove the `body` key or set to `undefined` | ||
// to achieve the same affect. Maintaining legacy behavior for now. | ||
emitAsteriskDeprecation() | ||
body = undefined | ||
} | ||
// Response is not always JSON as it could be a string or binary data or | ||
@@ -382,3 +363,5 @@ // even an array of binary buffers (e.g. when content is encoded). | ||
scope.intercept(npath, method, body).reply(status, response, rawHeaders) | ||
scope | ||
.intercept(npath, method, nockDef.body) | ||
.reply(status, response, rawHeaders) | ||
@@ -385,0 +368,0 @@ scopes.push(scope) |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "12.0.3", | ||
"version": "13.0.0-beta.1", | ||
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -32,2 +32,3 @@ "repository": { | ||
"devDependencies": { | ||
"@sinonjs/fake-timers": "^6.0.0", | ||
"assert-rejects": "^1.0.0", | ||
@@ -46,7 +47,6 @@ "chai": "^4.1.2", | ||
"got": "^10.5.2", | ||
"@sinonjs/fake-timers": "^6.0.0", | ||
"mocha": "^7.0.1", | ||
"npm-run-all": "^4.1.5", | ||
"nyc": "^15.0.0", | ||
"prettier": "1.19.0", | ||
"prettier": "2.0.2", | ||
"proxyquire": "^2.1.0", | ||
@@ -53,0 +53,0 @@ "request": "^2.83.0", |
@@ -92,2 +92,3 @@ # Nock | ||
- [Axios](#axios) | ||
- [Memory issues with Jest](#memory-issues-with-jest) | ||
- [Debugging](#debugging) | ||
@@ -264,5 +265,3 @@ - [Contributing](#contributing) | ||
```js | ||
nock('http://example.com') | ||
.get('/users?foo=bar') | ||
.reply(200) | ||
nock('http://example.com').get('/users?foo=bar').reply(200) | ||
``` | ||
@@ -299,6 +298,3 @@ | ||
nock('http://example.com') | ||
.get('/') | ||
.query(params) | ||
.reply(200) | ||
nock('http://example.com').get('/').query(params).reply(200) | ||
``` | ||
@@ -344,5 +340,3 @@ | ||
```js | ||
const scope = nock('http://myapp.iriscouch.com') | ||
.get('/users/1') | ||
.reply(404) | ||
const scope = nock('http://myapp.iriscouch.com').get('/users/1').reply(404) | ||
``` | ||
@@ -361,9 +355,7 @@ | ||
```js | ||
const scope = nock('http://myapp.iriscouch.com') | ||
.get('/') | ||
.reply(200, { | ||
username: 'pgte', | ||
email: 'pedro.teixeira@gmail.com', | ||
_id: '4324243fsd', | ||
}) | ||
const scope = nock('http://myapp.iriscouch.com').get('/').reply(200, { | ||
username: 'pgte', | ||
email: 'pedro.teixeira@gmail.com', | ||
_id: '4324243fsd', | ||
}) | ||
``` | ||
@@ -451,3 +443,3 @@ | ||
.get('/cat-poems') | ||
.reply(function(uri, requestBody) { | ||
.reply(function (uri, requestBody) { | ||
console.log('path:', this.req.path) | ||
@@ -474,8 +466,6 @@ console.log('headers:', this.req.headers) | ||
```js | ||
nock('http://www.google.com') | ||
.get('/cat-poems') | ||
.replyWithError({ | ||
message: 'something awful happened', | ||
code: 'AWFUL_ERROR', | ||
}) | ||
nock('http://www.google.com').get('/cat-poems').replyWithError({ | ||
message: 'something awful happened', | ||
code: 'AWFUL_ERROR', | ||
}) | ||
``` | ||
@@ -662,6 +652,3 @@ | ||
```js | ||
nock('http://zombo.com') | ||
.get('/') | ||
.times(4) | ||
.reply(200, 'Ok') | ||
nock('http://zombo.com').get('/').times(4).reply(200, 'Ok') | ||
@@ -678,14 +665,5 @@ http.get('http://zombo.com/') // respond body "Ok" | ||
```js | ||
nock('http://zombo.com') | ||
.get('/') | ||
.once() | ||
.reply(200, 'Ok') | ||
nock('http://zombo.com') | ||
.get('/') | ||
.twice() | ||
.reply(200, 'Ok') | ||
nock('http://zombo.com') | ||
.get('/') | ||
.thrice() | ||
.reply(200, 'Ok') | ||
nock('http://zombo.com').get('/').once().reply(200, 'Ok') | ||
nock('http://zombo.com').get('/').twice().reply(200, 'Ok') | ||
nock('http://zombo.com').get('/').thrice().reply(200, 'Ok') | ||
``` | ||
@@ -926,6 +904,3 @@ | ||
example | ||
.get('/pathB') | ||
.optionally() | ||
.reply(200) | ||
example.get('/pathB').optionally().reply(200) | ||
example.pendingMocks() // [] | ||
@@ -937,6 +912,3 @@ | ||
const getMock = optional => | ||
example | ||
.get('/pathC') | ||
.optionally(optional) | ||
.reply(200) | ||
example.get('/pathC').optionally(optional).reply(200) | ||
@@ -991,5 +963,3 @@ getMock(true) | ||
```js | ||
const scope = nock('http://google.com') | ||
.get('/') | ||
.reply(200) | ||
const scope = nock('http://google.com').get('/').reply(200) | ||
@@ -1037,6 +1007,3 @@ scope.isDone() // will return false | ||
```js | ||
const scope = nock('http://example.com') | ||
.persist() | ||
.get('/') | ||
.reply(200, 'ok') | ||
const scope = nock('http://example.com').persist().get('/').reply(200, 'ok') | ||
@@ -1276,3 +1243,3 @@ // Do some tests ... | ||
nocks = nock.load(pathToJson) | ||
nocks.forEach(function(nock) { | ||
nocks.forEach(function (nock) { | ||
nock.filteringRequestBody = (body, aRecordedBody) => { | ||
@@ -1286,3 +1253,7 @@ if (typeof body !== 'string' || typeof aRecordedBody !== 'string') { | ||
const recordedTimestamp = recordedBodyResult[1] | ||
return body.replace(/(timestamp):([0-9]+)/g, function(match, key, value) { | ||
return body.replace(/(timestamp):([0-9]+)/g, function ( | ||
match, | ||
key, | ||
value | ||
) { | ||
return key + ':' + recordedTimestamp | ||
@@ -1443,3 +1414,3 @@ }) | ||
// usage of the created fixture | ||
nockBack('zomboFixture.json', function(nockDone) { | ||
nockBack('zomboFixture.json', function (nockDone) { | ||
http.get('http://zombo.com/').end() // respond body "Ok" | ||
@@ -1585,2 +1556,9 @@ | ||
### Memory issues with Jest | ||
Memory issues can be avoided by calling [`nock.restore()`](#restoring) after each test suite. | ||
One of the core principles of [Jest](https://jestjs.io/) is that it runs tests in isolation. | ||
It does this by manipulating the modules cache of Node in a way that conflicts with how Nock monkey patches the builtin `http` and `https` modules. | ||
[Related issue with more details](https://github.com/nock/nock/issues/1817). | ||
## Debugging | ||
@@ -1587,0 +1565,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169843
3380
2
1596