Comparing version 13.0.0-beta.2 to 13.0.0-beta.3
@@ -162,3 +162,3 @@ 'use strict' | ||
if (filteringScope && filteringScope(basePath)) { | ||
debug('found matching scope interceptor') | ||
interceptor.scope.logger('found matching scope interceptor') | ||
@@ -165,0 +165,0 @@ // Keep the filtered scope (its key) to signal the rest of the module |
@@ -313,3 +313,5 @@ 'use strict' | ||
if (matchedInterceptor) { | ||
debug('interceptor identified, starting mocking') | ||
matchedInterceptor.scope.logger( | ||
'interceptor identified, starting mocking' | ||
) | ||
@@ -316,0 +318,0 @@ // wait to emit the finish event until we know for sure an Interceptor is going to playback. |
'use strict' | ||
const debug = require('debug')('nock.interceptor') | ||
const stringify = require('json-stringify-safe') | ||
@@ -183,4 +182,4 @@ const querystring = require('querystring') | ||
debug('reply.headers:', this.headers) | ||
debug('reply.rawHeaders:', this.rawHeaders) | ||
this.scope.logger('reply.headers:', this.headers) | ||
this.scope.logger('reply.rawHeaders:', this.rawHeaders) | ||
@@ -231,3 +230,8 @@ this.body = body | ||
debug("request header field doesn't match:", key, header, reqHeader) | ||
this.scope.logger( | ||
"request header field doesn't match:", | ||
key, | ||
header, | ||
reqHeader | ||
) | ||
return false | ||
@@ -237,4 +241,9 @@ } | ||
match(req, options, body) { | ||
if (debug.enabled) { | ||
debug('match %s, body = %s', stringify(options), stringify(body)) | ||
// check if the logger is enabled because the stringifies can be expensive. | ||
if (this.scope.logger.enabled) { | ||
this.scope.logger( | ||
'attempting match %s, body = %s', | ||
stringify(options), | ||
stringify(body) | ||
) | ||
} | ||
@@ -249,3 +258,3 @@ | ||
if (this.method !== method) { | ||
debug( | ||
this.scope.logger( | ||
`Method did not match. Request ${method} Interceptor ${this.method}` | ||
@@ -282,2 +291,3 @@ ) | ||
if (!reqHeadersMatch) { | ||
this.scope.logger("headers don't match") | ||
return false | ||
@@ -290,10 +300,14 @@ } | ||
) { | ||
this.scope.logger( | ||
'matching failed because Scope.conditionally() did not validate' | ||
) | ||
return false | ||
} | ||
const reqContainsBadHeaders = this.badheaders.some( | ||
const badHeaders = this.badheaders.filter( | ||
header => header in options.headers | ||
) | ||
if (reqContainsBadHeaders) { | ||
if (badHeaders.length) { | ||
this.scope.logger('request contains bad headers', ...badHeaders) | ||
return false | ||
@@ -304,3 +318,3 @@ } | ||
if (this.queries === null) { | ||
debug('query matching skipped') | ||
this.scope.logger('query matching skipped') | ||
} else { | ||
@@ -311,3 +325,5 @@ // can't rely on pathname or search being in the options, but path has a default | ||
debug(matchQueries ? 'query matching succeeded' : 'query matching failed') | ||
this.scope.logger( | ||
matchQueries ? 'query matching succeeded' : 'query matching failed' | ||
) | ||
@@ -415,4 +431,4 @@ if (!matchQueries) { | ||
const reqQueries = querystring.parse(options.search) | ||
debug('Interceptor queries: %j', this.queries) | ||
debug(' Request queries: %j', reqQueries) | ||
this.scope.logger('Interceptor queries: %j', this.queries) | ||
this.scope.logger(' Request queries: %j', reqQueries) | ||
@@ -419,0 +435,0 @@ if (typeof this.queries === 'function') { |
@@ -86,2 +86,4 @@ 'use strict' | ||
}) { | ||
const { logger } = interceptor.scope | ||
function emitError(error) { | ||
@@ -118,3 +120,3 @@ process.nextTick(() => { | ||
response.rawHeaders = [...interceptor.rawHeaders] | ||
debug('response.rawHeaders:', response.rawHeaders) | ||
logger('response.rawHeaders:', response.rawHeaders) | ||
@@ -218,6 +220,6 @@ if (interceptor.replyFunction) { | ||
if (responseBody !== undefined) { | ||
debug('transform the response body') | ||
logger('transform the response body') | ||
if (interceptor.delayInMs) { | ||
debug( | ||
logger( | ||
'delaying the response for', | ||
@@ -236,3 +238,3 @@ interceptor.delayInMs, | ||
if (common.isStream(responseBody)) { | ||
debug('response body is a stream') | ||
logger('response body is a stream') | ||
responseBody.pause() | ||
@@ -298,7 +300,7 @@ responseBody.on('data', function (d) { | ||
debug('emitting response') | ||
logger('emitting response') | ||
req.emit('response', response) | ||
if (common.isStream(responseBody)) { | ||
debug('resuming response stream') | ||
logger('resuming response stream') | ||
responseBody.resume() | ||
@@ -308,3 +310,3 @@ } else { | ||
if (typeof responseBody !== 'undefined') { | ||
debug('adding body to buffer list') | ||
logger('adding body to buffer list') | ||
responseBuffers.push(responseBody) | ||
@@ -318,7 +320,7 @@ } | ||
if (chunk) { | ||
debug('emitting response chunk') | ||
logger('emitting response chunk') | ||
response.push(chunk) | ||
common.setImmediate(emitChunk) | ||
} else { | ||
debug('ending response stream') | ||
logger('ending response stream') | ||
response.push(null) | ||
@@ -325,0 +327,0 @@ // https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_message_complete |
@@ -42,3 +42,2 @@ 'use strict' | ||
this.matchHeaders = [] | ||
this.logger = debug | ||
this.scopeOptions = options || {} | ||
@@ -54,2 +53,4 @@ this.urlParts = {} | ||
let logNamespace = String(basePath) | ||
if (!(basePath instanceof RegExp)) { | ||
@@ -61,3 +62,6 @@ this.urlParts = url.parse(basePath) | ||
this.basePath = `${this.urlParts.protocol}//${this.urlParts.hostname}:${this.port}` | ||
logNamespace = this.urlParts.host | ||
} | ||
this.logger = debug.extend(logNamespace) | ||
} | ||
@@ -224,7 +228,2 @@ | ||
log(newLogger) { | ||
this.logger = newLogger | ||
return this | ||
} | ||
persist(flag = true) { | ||
@@ -231,0 +230,0 @@ if (typeof flag !== 'boolean') { |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "13.0.0-beta.2", | ||
"version": "13.0.0-beta.3", | ||
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -13,0 +13,0 @@ "repository": { |
@@ -66,3 +66,2 @@ # Nock | ||
- [.isActive()](#isactive) | ||
- [Logging](#logging) | ||
- [Restoring](#restoring) | ||
@@ -1052,12 +1051,2 @@ - [Activating](#activating) | ||
## Logging | ||
Nock can log matches if you pass in a log function like this: | ||
```js | ||
const scope = nock('http://google.com') | ||
.log(console.log) | ||
... | ||
``` | ||
## Restoring | ||
@@ -1553,6 +1542,24 @@ | ||
```console | ||
user@local$ DEBUG=nock.* node my_test.js | ||
``` | ||
Each step in the matching process is logged this way and can be useful when determining why a request was not intercepted by Nock. | ||
For example the following shows that matching failed because the request had an extra search parameter. | ||
```js | ||
$ DEBUG=nock.* node my_test.js | ||
nock('http://example.com').get('/').query({ foo: 'bar' }).reply() | ||
await got('http://example.com/?foo=bar&baz=foz') | ||
``` | ||
```console | ||
user@local$ DEBUG=nock.scope:example.com node my_test.js | ||
... | ||
nock.scope:example.com Interceptor queries: {"foo":"bar"} +1ms | ||
nock.scope:example.com Request queries: {"foo":"bar","baz":"foz"} +0ms | ||
nock.scope:example.com query matching failed +0ms | ||
``` | ||
## Contributing | ||
@@ -1559,0 +1566,0 @@ |
@@ -123,3 +123,2 @@ // TypeScript Version: 3.5 | ||
log(out: (message: any, ...optionalParams: any[]) => void): this | ||
persist(flag?: boolean): this | ||
@@ -126,0 +125,0 @@ replyContentLength(): this |
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
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
174620
3490
1603