Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace debug with node debug (#2592)
* fix: replace debug module with node builtin debug * update readme * use %s instead of %o * remove comment * use %s instead of %o * add test * fix
- Loading branch information
Showing
15 changed files
with
125 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
const { debuglog } = require('util') | ||
|
||
module.exports.back = debuglog('nock:back') | ||
module.exports.common = debuglog('nock:common') | ||
module.exports.intercept = debuglog('nock:intercept') | ||
module.exports.request_overrider = debuglog('nock:request_overrider') | ||
module.exports.playback_interceptor = debuglog('nock:playback_interceptor') | ||
module.exports.recorder = debuglog('nock:recorder') | ||
module.exports.socket = debuglog('nock:socket') | ||
module.exports.scopeDebuglog = namespace => debuglog(`nock:scope:${namespace}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import nock from '../../../index.js' | ||
import got from 'got' | ||
|
||
nock('http://example.test').post('/deep/link').reply(200, 'Hello World!') | ||
|
||
const exampleBody = 'Hello yourself!' | ||
await got.post('http://example.test/deep/link', { body: exampleBody }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,96 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
const sinon = require('sinon') | ||
const { join } = require('node:path') | ||
const { spawn } = require('node:child_process') | ||
const { expect } = require('chai') | ||
const nock = require('../..') | ||
const got = require('./got_client') | ||
|
||
describe('Logging using the `debug` package', () => { | ||
let logFn | ||
beforeEach(() => { | ||
logFn = sinon.stub(debug, 'log') | ||
debug.enable('nock*') | ||
}) | ||
|
||
afterEach(() => { | ||
debug.disable('nock*') | ||
}) | ||
|
||
it('match debugging works', async () => { | ||
nock('http://example.test').post('/deep/link').reply(200, 'Hello World!') | ||
|
||
const exampleBody = 'Hello yourself!' | ||
await got.post('http://example.test/deep/link', { body: exampleBody }) | ||
|
||
// the log function will have been a few dozen times, there are a few specific to matching we want to validate: | ||
|
||
// the log when an interceptor is chosen | ||
expect(logFn).to.have.been.calledWith( | ||
sinon.match('matched base path (1 interceptor)'), | ||
) | ||
let resolveTest | ||
let rejectTest | ||
const p = new Promise((resolve, reject) => { | ||
resolveTest = resolve | ||
rejectTest = reject | ||
}) | ||
const assertions = [ | ||
/NOCK:BACK \d+: New nock back mode: dryrun/, | ||
/NOCK:RECORDER \d+: 0 restoring all the overridden http\/https properties/, | ||
/NOCK:COMMON \d+: restoring requests/, | ||
/NOCK:INTERCEPT \d+: restoring overridden ClientRequest/, | ||
/NOCK:INTERCEPT \d+: - ClientRequest was not overridden/, | ||
/NOCK:COMMON \d+: overriding requests/, | ||
/NOCK:COMMON \d+: - overriding request for http/, | ||
/NOCK:COMMON \d+: - overridden request for http/, | ||
/NOCK:COMMON \d+: - overriding request for https/, | ||
/NOCK:COMMON \d+: - overridden request for https/, | ||
/NOCK:INTERCEPT \d+: Overriding ClientRequest/, | ||
/NOCK:INTERCEPT \d+: ClientRequest overridden/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: reply.headers: {}/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: reply.rawHeaders: \[\]/, | ||
/NOCK:COMMON \d+: options.hostname in the end: "example.test"/, | ||
/NOCK:COMMON \d+: options.host in the end: "example.test:80"/, | ||
/NOCK:INTERCEPT \d+: interceptors for "example.test:80"/, | ||
/NOCK:INTERCEPT \d+: filtering interceptors for basepath http:\/\/example.test:80/, | ||
/NOCK:INTERCEPT \d+: matched base path \(1 interceptor\)/, | ||
/NOCK:COMMON \d+: options.host: example.test:80/, | ||
/NOCK:COMMON \d+: options.hostname in the end: "example.test"/, | ||
/NOCK:COMMON \d+: options.host in the end: "example.test:80"/, | ||
/NOCK:INTERCEPT \d+: interceptors for "example.test:80"/, | ||
/NOCK:INTERCEPT \d+: filtering interceptors for basepath http:\/\/example.test:80/, | ||
/NOCK:INTERCEPT \d+: matched base path \(1 interceptor\)/, | ||
/NOCK:INTERCEPT \d+: using 1 interceptors/, | ||
/NOCK:REQUEST_OVERRIDER \d+: request write/, | ||
/NOCK:REQUEST_OVERRIDER \d+: request end/, | ||
/NOCK:REQUEST_OVERRIDER \d+: ending/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: attempting match {"protocol":"http:","hostname":"example.test","hash":"","search":"","pathname":"\/deep\/link","path":"\/deep\/link","href":"http:\/\/example.test\/deep\/link","method":"POST","retry":{"limit":2,"methods":\["GET","PUT","HEAD","DELETE","OPTIONS","TRACE"\],"statusCodes":\[408,413,429,500,502,503,504,521,522,524\],"errorCodes":\["ETIMEDOUT","ECONNRESET","EADDRINUSE","ECONNREFUSED","EPIPE","ENOTFOUND","ENETUNREACH","EAI_AGAIN"\],"maxRetryAfter":null},"headers":{"user-agent":"got \(https:\/\/github.com\/sindresorhus\/got\)","content-length":"15","accept-encoding":"gzip, deflate, br"},"hooks":{"init":\[\],"beforeRequest":\[\],"beforeRedirect":\[\],"beforeRetry":\[\],"beforeError":\[\],"afterResponse":\[\]},"decompress":true,"throwHttpErrors":true,"followRedirect":true,"isStream":false,"responseType":"text","resolveBodyOnly":false,"maxRedirects":10,"prefixUrl":"","methodRewriting":true,"ignoreInvalidCookies":false,"http2":false,"allowGetBody":false,"pagination":{"countLimit":null,"backoff":0,"requestLimit":10000,"stackAllItems":true},"cacheOptions":{},"url":"http:\/\/example.test\/deep\/link","username":"","password":"","proto":"http","port":80,"host":"example.test:80"}, body = "Hello yourself!"/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: query matching skipped/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: matching http:\/\/example.test:80\/deep\/link to POST http:\/\/example.test:80\/deep\/link: true/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: interceptor identified, starting mocking/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: response.rawHeaders: \[\]/, | ||
/NOCK:SCOPE:EXAMPLE.TEST \d+: emitting response/, | ||
/^$/, | ||
] | ||
|
||
// the log of the Interceptor match | ||
expect(logFn).to.have.been.calledWith( | ||
// debug namespace for the scope that includes the host | ||
sinon.match('nock.scope:example.test'), | ||
// This is a JSON blob which contains, among other things the complete | ||
// request URL. | ||
sinon.match('"href":"http://example.test/deep/link"'), | ||
// This is the JSON-stringified body. | ||
`"${exampleBody}"`, | ||
) | ||
|
||
expect(logFn).to.have.been.calledWith(sinon.match('query matching skipped')) | ||
|
||
expect(logFn).to.have.been.calledWith( | ||
sinon.match( | ||
'matching http://example.test:80/deep/link to POST http://example.test:80/deep/link: true', | ||
), | ||
) | ||
expect(logFn).to.have.been.calledWith( | ||
sinon.match('interceptor identified, starting mocking'), | ||
const child = spawn( | ||
process.execPath, | ||
[join(__dirname, './fixtures/logging.mjs')], | ||
{ | ||
env: { | ||
NODE_DEBUG: 'nock:*', | ||
}, | ||
}, | ||
) | ||
const chunks = [] | ||
child.stderr.setEncoding('utf8') | ||
child.stderr.on('data', chunk => { | ||
chunks.push(chunk) | ||
}) | ||
child.stderr.on('end', () => { | ||
const lines = extractLines(chunks) | ||
try { | ||
expect(lines.length).to.be.equal(assertions.length) | ||
for (let i = 1; i < lines.length; i++) { | ||
expect(lines[i], `${i}: ${lines[i]}`).to.match(assertions[i]) | ||
} | ||
} catch (e) { | ||
rejectTest(e) | ||
return | ||
} | ||
resolveTest() | ||
}) | ||
await p | ||
}) | ||
}) | ||
|
||
const removeEscapeColorsRE = | ||
// eslint-disable-next-line no-control-regex | ||
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g | ||
|
||
function extractLines(chunks) { | ||
return chunks | ||
.join('') | ||
.split('\n') | ||
.map(v => v.replace(removeEscapeColorsRE, '')) | ||
} |