Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove circular dependency from debugging code #1354

Merged
merged 2 commits into from Jan 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion index.js
Expand Up @@ -172,5 +172,15 @@ request.forever = function (agentOptions, optionsArg) {

module.exports = request
request.Request = require('./request')
request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
request.initParams = initParams

// Backwards compatibility for request.debug
Object.defineProperty(request, 'debug', {
enumerable : true,
get : function() {
return request.Request.debug
},
set : function(debug) {
request.Request.debug = debug
}
})
11 changes: 0 additions & 11 deletions lib/debug.js

This file was deleted.

9 changes: 8 additions & 1 deletion request.js
Expand Up @@ -23,7 +23,6 @@ var http = require('http')
, FormData = require('form-data')
, cookies = require('./lib/cookies')
, copy = require('./lib/copy')
, debug = require('./lib/debug')
, net = require('net')
, CombinedStream = require('combined-stream')
, isstream = require('isstream')
Expand Down Expand Up @@ -240,6 +239,14 @@ function Request (options) {

util.inherits(Request, stream.Stream)

// Debugging
Request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
function debug() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd expect to see the debug function up with the other inlined functions, instead of here in the Request prototype methods

if (Request.debug) {
console.error('REQUEST %s', util.format.apply(util, arguments))
}
}

Request.prototype.setupTunnel = function () {
// Set up the tunneling agent if necessary
// Only send the proxy whitelisted header names.
Expand Down
16 changes: 12 additions & 4 deletions tests/test-node-debug.js
Expand Up @@ -12,18 +12,21 @@ var s = http.createServer(function(req, res) {
var stderr = []
, prevStderrLen = 0

process.stderr.write = function(string, encoding, fd) {
stderr.push(string)
}

tape('setup', function(t) {
process.stderr._oldWrite = process.stderr.write
process.stderr.write = function(string, encoding, fd) {
stderr.push(string)
}

s.listen(6767, function() {
t.end()
})
})

tape('a simple request should not fail with debugging enabled', function(t) {
request.debug = true
t.equal(request.Request.debug, true, 'request.debug sets request.Request.debug')
t.equal(request.debug, true, 'request.debug gets request.Request.debug')
stderr = []

request('http://localhost:6767', function(err, res, body) {
Expand Down Expand Up @@ -68,6 +71,8 @@ tape('there should be no further lookups on process.env', function(t) {

tape('it should be possible to disable debugging at runtime', function(t) {
request.debug = false
t.equal(request.Request.debug, false, 'request.debug sets request.Request.debug')
t.equal(request.debug, false, 'request.debug gets request.Request.debug')
stderr = []

request('http://localhost:6767', function(err, res, body) {
Expand All @@ -79,6 +84,9 @@ tape('it should be possible to disable debugging at runtime', function(t) {
})

tape('cleanup', function(t) {
process.stderr.write = process.stderr._oldWrite
delete process.stderr._oldWrite

s.close(function() {
t.end()
})
Expand Down