From 24f38e566a06fb129bb9c908d88e979929541db3 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 5 May 2015 15:46:12 -0500 Subject: [PATCH 01/14] Update tested node versions to (0.10, 0.12) and add io.js --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 88913ef..73e25aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: node_js node_js: - - "0.11" + - "iojs" + - "0.12" - "0.10" - - "0.8" env: - REQUEST_VERSION=2.30.0 From 3662b11e811077d0a8d8559fd5e1084b06415add Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 5 May 2015 15:48:28 -0500 Subject: [PATCH 02/14] Update tested request versions --- .travis.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 73e25aa..bae5bb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,13 +7,12 @@ node_js: env: - REQUEST_VERSION=2.30.0 - - REQUEST_VERSION=2.34.0 - REQUEST_VERSION=2.40.0 - - REQUEST_VERSION=2.41.0 - - REQUEST_VERSION=2.42.0 - - REQUEST_VERSION=2.43.0 - - REQUEST_VERSION=2.44.0 - - REQUEST_VERSION=2.45.0 + - REQUEST_VERSION=2.50.0 + - REQUEST_VERSION=2.52.0 + - REQUEST_VERSION=2.53.0 + - REQUEST_VERSION=2.54.0 + - REQUEST_VERSION=2.55.0 before_script: - rm -r node_modules/request/ From 832b9c20ac3aaca3251406d1da61d41e4abdd3e5 Mon Sep 17 00:00:00 2001 From: simov Date: Wed, 13 May 2015 15:29:47 +0300 Subject: [PATCH 03/14] Fix fixtures --- package.json | 2 +- test/basic.js | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index eb2aa51..79c4ab1 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "mocha" : "~1.21.4", "passport" : "~0.2.0", "passport-http" : "~0.2.2", - "request" : "~2.40.0", + "request" : "~2.50.0", "should" : "~4.0.4" }, "dependencies" : { diff --git a/test/basic.js b/test/basic.js index 22b65e1..6119292 100644 --- a/test/basic.js +++ b/test/basic.js @@ -124,7 +124,8 @@ describe('request-debug', function() { uri : lib.urls.http + '/bottom', method : 'GET', headers : { - host : 'localhost:' + lib.ports.http + host : 'localhost:' + lib.ports.http, + referer : 'http://localhost:8480/bottom' } } }, { @@ -182,7 +183,8 @@ describe('request-debug', function() { uri : lib.urls.http + '/bottom', method : 'GET', headers : { - host : 'localhost:' + lib.ports.http + host : 'localhost:' + lib.ports.http, + referer : 'http://localhost:8480/bottom' } } }, { @@ -245,7 +247,8 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost' + host : 'localhost', + referer : 'http://localhost:8480/auth/bottom' } } }, { @@ -308,7 +311,8 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost' + host : 'localhost', + referer : 'https://localhost:8443/auth/top/http' } } }, { @@ -333,7 +337,8 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http + host : 'localhost:' + lib.ports.http, + referer : 'http://localhost:8480/middle' } } }, { @@ -358,7 +363,8 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http + host : 'localhost:' + lib.ports.http, + referer : 'http://localhost:8480/bottom' } } }, { @@ -400,7 +406,7 @@ describe('request-debug', function() { headers : { host : 'localhost', 'content-length' : 16, - 'content-type' : 'application/x-www-form-urlencoded; charset=utf-8' + 'content-type' : 'application/x-www-form-urlencoded' }, body : 'formKey=formData' } From 79c6fad7c52fdd736ce53bb9f2566bc92d076d7c Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 5 May 2015 15:46:12 -0500 Subject: [PATCH 04/14] Update tested node versions to (0.10, 0.12) and add io.js --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 88913ef..73e25aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: node_js node_js: - - "0.11" + - "iojs" + - "0.12" - "0.10" - - "0.8" env: - REQUEST_VERSION=2.30.0 From 38444d51aaa3dafc66929174e1312c2dc143ebde Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 5 May 2015 15:48:28 -0500 Subject: [PATCH 05/14] Update tested request versions --- .travis.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 73e25aa..bae5bb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,13 +7,12 @@ node_js: env: - REQUEST_VERSION=2.30.0 - - REQUEST_VERSION=2.34.0 - REQUEST_VERSION=2.40.0 - - REQUEST_VERSION=2.41.0 - - REQUEST_VERSION=2.42.0 - - REQUEST_VERSION=2.43.0 - - REQUEST_VERSION=2.44.0 - - REQUEST_VERSION=2.45.0 + - REQUEST_VERSION=2.50.0 + - REQUEST_VERSION=2.52.0 + - REQUEST_VERSION=2.53.0 + - REQUEST_VERSION=2.54.0 + - REQUEST_VERSION=2.55.0 before_script: - rm -r node_modules/request/ From aa9d6b4116e1c9e46c51d55ebf16c3df61eefe9d Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 11:29:33 -0500 Subject: [PATCH 06/14] Don't check Referer headers --- test/basic.js | 18 ++++++------------ test/lib/index.js | 11 +++++++++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/basic.js b/test/basic.js index 6119292..17e4ff9 100644 --- a/test/basic.js +++ b/test/basic.js @@ -124,8 +124,7 @@ describe('request-debug', function() { uri : lib.urls.http + '/bottom', method : 'GET', headers : { - host : 'localhost:' + lib.ports.http, - referer : 'http://localhost:8480/bottom' + host : 'localhost:' + lib.ports.http } } }, { @@ -183,8 +182,7 @@ describe('request-debug', function() { uri : lib.urls.http + '/bottom', method : 'GET', headers : { - host : 'localhost:' + lib.ports.http, - referer : 'http://localhost:8480/bottom' + host : 'localhost:' + lib.ports.http } } }, { @@ -247,8 +245,7 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost', - referer : 'http://localhost:8480/auth/bottom' + host : 'localhost' } } }, { @@ -311,8 +308,7 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost', - referer : 'https://localhost:8443/auth/top/http' + host : 'localhost' } } }, { @@ -337,8 +333,7 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http, - referer : 'http://localhost:8480/middle' + host : 'localhost:' + lib.ports.http } } }, { @@ -363,8 +358,7 @@ describe('request-debug', function() { method : 'GET', headers : { authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http, - referer : 'http://localhost:8480/bottom' + host : 'localhost:' + lib.ports.http } } }, { diff --git a/test/lib/index.js b/test/lib/index.js index 78cbee4..563f55b 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -65,6 +65,9 @@ var fixHeader = { }).join(',')); } return val; + }, + referer : function(val) { + return null; } }; fixHeader['www-authenticate'] = fixHeader.authorization; @@ -74,8 +77,12 @@ exports.fixVariableHeaders = function() { for (var type in req) { for (var header in req[type].headers) { if (fixHeader[header]) { - req[type].headers[header] = - fixHeader[header](req[type].headers[header]); + var fixed = fixHeader[header](req[type].headers[header]); + if (fixed === null) { + delete req[type].headers[header]; + } else { + req[type].headers[header] = fixed; + } } } } From 3f64499891e0e2c6ca70ff9d65d042eca612cae7 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 12:56:26 -0500 Subject: [PATCH 07/14] Allow form-urlencoded 'Content-Type' with or without 'charset=utf-8' --- test/basic.js | 2 +- test/lib/index.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/basic.js b/test/basic.js index 17e4ff9..3014534 100644 --- a/test/basic.js +++ b/test/basic.js @@ -400,7 +400,7 @@ describe('request-debug', function() { headers : { host : 'localhost', 'content-length' : 16, - 'content-type' : 'application/x-www-form-urlencoded' + 'content-type' : '' }, body : 'formKey=formData' } diff --git a/test/lib/index.js b/test/lib/index.js index 563f55b..5534cf7 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -68,6 +68,9 @@ var fixHeader = { }, referer : function(val) { return null; + }, + 'content-type' : function(val) { + return val.replace(/^application\/x-www-form-urlencoded(; charset=utf-8)?$/, ''); } }; fixHeader['www-authenticate'] = fixHeader.authorization; From 3ac4b27ff8b45f53f028cf8ebc3418c2d7e95745 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 13:04:29 -0500 Subject: [PATCH 08/14] Node sends 'Transfer-Encoding: chunked' here, io.js does not --- test/basic.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/test/basic.js b/test/basic.js index 3014534..0e411c6 100644 --- a/test/basic.js +++ b/test/basic.js @@ -22,6 +22,15 @@ describe('request-debug', function() { lib.clearRequests(); }); + function maybeTransferEncodingChunked(obj) { + // io.js has major version >= 1 + var isNode = process.versions.node.test(/^0\./); + if (isNode) { + obj['transfer-encoding'] = 'chunked'; + } + return obj; + } + it('should capture a normal request', function(done) { request(lib.urls.http + '/bottom', function(err, res, body) { should.not.exist(err); @@ -228,13 +237,12 @@ describe('request-debug', function() { }, { auth : { debugId : lib.debugId, - headers : { - connection : '', - date : '', - 'transfer-encoding' : 'chunked', - 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', - 'x-powered-by' : 'Express', - }, + headers : maybeTransferEncodingChunked({ + connection : '', + date : '', + 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', + 'x-powered-by' : 'Express', + }), statusCode : 401, uri : lib.urls.http + '/auth/bottom' } @@ -291,13 +299,12 @@ describe('request-debug', function() { }, { auth : { debugId : lib.debugId, - headers : { - connection : '', - date : '', - 'transfer-encoding' : 'chunked', - 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', - 'x-powered-by' : 'Express', - }, + headers : maybeTransferEncodingChunked({ + connection : '', + date : '', + 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', + 'x-powered-by' : 'Express', + }), statusCode : 401, uri : lib.urls.https + '/auth/top/http' } From 8bb8a485a685f267d66e7a3e0a178800df8bddfe Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 13:05:21 -0500 Subject: [PATCH 09/14] io.js sends 'Content-Length' here, Node does not --- test/lib/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/lib/index.js b/test/lib/index.js index 5534cf7..93a2ec7 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -71,6 +71,14 @@ var fixHeader = { }, 'content-type' : function(val) { return val.replace(/^application\/x-www-form-urlencoded(; charset=utf-8)?$/, ''); + }, + 'content-length' : function(val, obj) { + // io.js has major version >= 1 + var isNode = process.versions.node.test(/^0\./); + if (!isNode && obj.statusCode == 401) { + // io.js sends content-length here, Node.js does not + return null; + } } }; fixHeader['www-authenticate'] = fixHeader.authorization; @@ -80,7 +88,7 @@ exports.fixVariableHeaders = function() { for (var type in req) { for (var header in req[type].headers) { if (fixHeader[header]) { - var fixed = fixHeader[header](req[type].headers[header]); + var fixed = fixHeader[header](req[type].headers[header], req[type]); if (fixed === null) { delete req[type].headers[header]; } else { From 026e9531631ed0d04235de01e4a4d0a0acc9e124 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 13:20:08 -0500 Subject: [PATCH 10/14] Second try while we're waiting on https://github.com/iojs/io.js/pull/493 --- package.json | 1 + test/basic.js | 8 ++++---- test/lib/index.js | 6 ++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 79c4ab1..e51e476 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "main" : "index.js", "devDependencies" : { + "detect-engine" : "~1.0.2", "express" : "~4.8.6", "mocha" : "~1.21.4", "passport" : "~0.2.0", diff --git a/test/basic.js b/test/basic.js index 0e411c6..9df1b95 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,4 +1,5 @@ -var lib = require('./lib'), +var engine = require('detect-engine'), + lib = require('./lib'), mocha = require('mocha'), request = require('request'), should = require('should'); @@ -23,9 +24,8 @@ describe('request-debug', function() { }); function maybeTransferEncodingChunked(obj) { - // io.js has major version >= 1 - var isNode = process.versions.node.test(/^0\./); - if (isNode) { + if (engine == 'node') { + // Node sends 'Transfer-Encoding: chunked' here, io.js does not obj['transfer-encoding'] = 'chunked'; } return obj; diff --git a/test/lib/index.js b/test/lib/index.js index 93a2ec7..342d140 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -73,10 +73,8 @@ var fixHeader = { return val.replace(/^application\/x-www-form-urlencoded(; charset=utf-8)?$/, ''); }, 'content-length' : function(val, obj) { - // io.js has major version >= 1 - var isNode = process.versions.node.test(/^0\./); - if (!isNode && obj.statusCode == 401) { - // io.js sends content-length here, Node.js does not + if (engine == 'iojs' && obj.statusCode == 401) { + // io.js sends content-length here, Node does not return null; } } From 9432e038a33d1c988ec2b3e8f83371bc6da55f1a Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 16:29:26 -0500 Subject: [PATCH 11/14] Missed a spot --- test/lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/lib/index.js b/test/lib/index.js index 342d140..d64ff1a 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -1,4 +1,5 @@ -var express = require('express'), +var engine = require('detect-engine'), + express = require('express'), fs = require('fs'), http = require('http'), https = require('https'), From e4419a1d548ec45d19b1fc1953a63aaf2274d3b3 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 13 May 2015 16:55:34 -0500 Subject: [PATCH 12/14] fffuuuuu --- test/lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/lib/index.js b/test/lib/index.js index d64ff1a..a8aaf9a 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -77,6 +77,8 @@ var fixHeader = { if (engine == 'iojs' && obj.statusCode == 401) { // io.js sends content-length here, Node does not return null; + } else { + return val; } } }; From c6178661f6c5e9fd2a385c9beb963c0a661442eb Mon Sep 17 00:00:00 2001 From: simov Date: Mon, 18 May 2015 19:43:25 +0300 Subject: [PATCH 13/14] Convert to 2 space indentation and remove ; --- index.js | 150 +++---- package.json | 70 ++-- test/basic.js | 1006 ++++++++++++++++++++++----------------------- test/lib/index.js | 234 +++++------ 4 files changed, 730 insertions(+), 730 deletions(-) diff --git a/index.js b/index.js index f6db0cd..00a3006 100644 --- a/index.js +++ b/index.js @@ -1,92 +1,92 @@ -var clone = require('clone'); +var clone = require('clone') -var debugId = 0; +var debugId = 0 module.exports = exports = function(request, log) { - log = log || exports.log; + log = log || exports.log - var proto; - if (request.Request) { - proto = request.Request.prototype; - } else if (request.get && request.post) { - // The object returned by request.defaults() doesn't include the - // Request property, so do this horrible thing to get at it. Per - // Wikipedia, port 4 is unassigned. - var req = request('http://localhost:4').on('error', function() { }); - proto = req.constructor.prototype; - } else { - throw new Error( - "Pass the object returned by require('request') to this function."); - } + var proto + if (request.Request) { + proto = request.Request.prototype + } else if (request.get && request.post) { + // The object returned by request.defaults() doesn't include the + // Request property, so do this horrible thing to get at it. Per + // Wikipedia, port 4 is unassigned. + var req = request('http://localhost:4').on('error', function() { }) + proto = req.constructor.prototype + } else { + throw new Error( + "Pass the object returned by require('request') to this function.") + } - if (!proto._initBeforeDebug) { - proto._initBeforeDebug = proto.init; + if (!proto._initBeforeDebug) { + proto._initBeforeDebug = proto.init - proto.init = function() { - if (!this._debugId) { + proto.init = function() { + if (!this._debugId) { - this.on('request', function(req) { - var data = { - debugId : this._debugId, - uri : this.uri.href, - method : this.method, - headers : clone(this.headers) - }; - if (this.body) { - data.body = this.body.toString('utf8'); - } - log('request', data, this); + this.on('request', function(req) { + var data = { + debugId : this._debugId, + uri : this.uri.href, + method : this.method, + headers : clone(this.headers) + } + if (this.body) { + data.body = this.body.toString('utf8') + } + log('request', data, this) - }).on('response', function(res) { - if (this.callback) { - // callback specified, request will buffer the body for - // us, so wait until the complete event to do anything - } else { - // cannot get body since no callback specified - log('response', { - debugId : this._debugId, - headers : clone(res.headers), - statusCode : res.statusCode - }, this); - } + }).on('response', function(res) { + if (this.callback) { + // callback specified, request will buffer the body for + // us, so wait until the complete event to do anything + } else { + // cannot get body since no callback specified + log('response', { + debugId : this._debugId, + headers : clone(res.headers), + statusCode : res.statusCode + }, this) + } - }).on('complete', function(res, body) { - if (this.callback) { - log('response', { - debugId : this._debugId, - headers : clone(res.headers), - statusCode : res.statusCode, - body : res.body - }, this); - } + }).on('complete', function(res, body) { + if (this.callback) { + log('response', { + debugId : this._debugId, + headers : clone(res.headers), + statusCode : res.statusCode, + body : res.body + }, this) + } - }).on('redirect', function() { - var type = (this.response.statusCode == 401 ? 'auth' : 'redirect'); - log(type, { - debugId : this._debugId, - statusCode : this.response.statusCode, - headers : clone(this.response.headers), - uri : this.uri.href - }, this); - }); + }).on('redirect', function() { + var type = (this.response.statusCode == 401 ? 'auth' : 'redirect') + log(type, { + debugId : this._debugId, + statusCode : this.response.statusCode, + headers : clone(this.response.headers), + uri : this.uri.href + }, this) + }) - this._debugId = ++debugId; - } + this._debugId = ++debugId + } - return proto._initBeforeDebug.apply(this, arguments); - }; + return proto._initBeforeDebug.apply(this, arguments) } + } - if (!request.stopDebugging) { - request.stopDebugging = function() { - proto.init = proto._initBeforeDebug; - delete proto._initBeforeDebug; - }; + if (!request.stopDebugging) { + request.stopDebugging = function() { + proto.init = proto._initBeforeDebug + delete proto._initBeforeDebug } -}; + } +} exports.log = function(type, data, r) { - var toLog = {}; - toLog[type] = data; - console.error(toLog); -}; + var toLog = {} + toLog[type] = data + console.error(toLog) +} diff --git a/package.json b/package.json index e51e476..a3b221c 100644 --- a/package.json +++ b/package.json @@ -1,37 +1,37 @@ { - "name" : "request-debug", - "author" : "James Nylen ", - "description" : "Library to assist with debugging HTTP(s) requests made by the request module.", - "version" : "0.1.1", - "repository" : { - "type" : "git", - "url" : "https://github.com/request/request-debug" - }, - "bugs" : { - "url" : "https://github.com/request/request-debug/issues" - }, - "license" : "MIT", - "keywords" : [ - "request", - "debug", - "http", - "https", - "headers" - ], - "scripts" : { - "test" : "node node_modules/.bin/mocha" - }, - "main" : "index.js", - "devDependencies" : { - "detect-engine" : "~1.0.2", - "express" : "~4.8.6", - "mocha" : "~1.21.4", - "passport" : "~0.2.0", - "passport-http" : "~0.2.2", - "request" : "~2.50.0", - "should" : "~4.0.4" - }, - "dependencies" : { - "clone" : "~0.1.18" - } + "name" : "request-debug", + "author" : "James Nylen ", + "description" : "Library to assist with debugging HTTP(s) requests made by the request module.", + "version" : "0.1.1", + "repository" : { + "type" : "git", + "url" : "https://github.com/request/request-debug" + }, + "bugs" : { + "url" : "https://github.com/request/request-debug/issues" + }, + "license" : "MIT", + "keywords" : [ + "request", + "debug", + "http", + "https", + "headers" + ], + "scripts" : { + "test" : "node node_modules/.bin/mocha" + }, + "main" : "index.js", + "devDependencies" : { + "detect-engine" : "~1.0.2", + "express" : "~4.8.6", + "mocha" : "~1.21.4", + "passport" : "~0.2.0", + "passport-http" : "~0.2.2", + "request" : "~2.50.0", + "should" : "~4.0.4" + }, + "dependencies" : { + "clone" : "~0.1.18" + } } diff --git a/test/basic.js b/test/basic.js index 9df1b95..428c97a 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,527 +1,527 @@ var engine = require('detect-engine'), - lib = require('./lib'), - mocha = require('mocha'), - request = require('request'), - should = require('should'); + lib = require('./lib'), + mocha = require('mocha'), + request = require('request'), + should = require('should') describe('request-debug', function() { - var proto = request.Request.prototype; + var proto = request.Request.prototype - before(function() { - lib.enableDebugging(request); - lib.startServers(); + before(function() { + lib.enableDebugging(request) + lib.startServers() - request = request.defaults({ - headers : { - host : 'localhost' - }, - rejectUnauthorized : false - }); - }); + request = request.defaults({ + headers : { + host : 'localhost' + }, + rejectUnauthorized : false + }) + }) - beforeEach(function() { - lib.clearRequests(); - }); + beforeEach(function() { + lib.clearRequests() + }) - function maybeTransferEncodingChunked(obj) { - if (engine == 'node') { - // Node sends 'Transfer-Encoding: chunked' here, io.js does not - obj['transfer-encoding'] = 'chunked'; - } - return obj; + function maybeTransferEncodingChunked(obj) { + if (engine == 'node') { + // Node sends 'Transfer-Encoding: chunked' here, io.js does not + obj['transfer-encoding'] = 'chunked' } + return obj + } - it('should capture a normal request', function(done) { - request(lib.urls.http + '/bottom', function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); - - it('should capture a request with no callback', function(done) { - var r = request(lib.urls.http + '/bottom'); - r.on('complete', function(res) { - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200 - } - } - ]); - done(); - }); - }); - - it('should capture a redirect', function(done) { - request(lib.urls.http + '/middle', function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/middle', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - redirect : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '41', - 'content-type' : 'text/plain; charset=utf-8', - date : '', - location : '/bottom', - vary : 'Accept', - 'x-powered-by' : 'Express', - }, - statusCode : 302, - uri : lib.urls.http + '/bottom' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - host : 'localhost:' + lib.ports.http - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); + it('should capture a normal request', function(done) { + request(lib.urls.http + '/bottom', function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should capture a cross-protocol redirect', function(done) { - request(lib.urls.https + '/middle/http', function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.https + '/middle/http', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - redirect : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '62', - 'content-type' : 'text/plain; charset=utf-8', - date : '', - location : lib.urls.http + '/bottom', - vary : 'Accept', - 'x-powered-by' : 'Express', - }, - statusCode : 302, - uri : lib.urls.http + '/bottom' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - host : 'localhost:' + lib.ports.http - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); + it('should capture a request with no callback', function(done) { + var r = request(lib.urls.http + '/bottom') + r.on('complete', function(res) { + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200 + } + } + ]) + done() + }) + }) - it('should capture an auth challenge', function(done) { - request(lib.urls.http + '/auth/bottom', { - auth : { - user : 'admin', - pass : 'mypass', - sendImmediately : false + it('should capture a redirect', function(done) { + request(lib.urls.http + '/middle', function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/middle', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + redirect : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '41', + 'content-type' : 'text/plain; charset=utf-8', + date : '', + location : '/bottom', + vary : 'Accept', + 'x-powered-by' : 'Express', + }, + statusCode : 302, + uri : lib.urls.http + '/bottom' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + host : 'localhost:' + lib.ports.http } - }, function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/auth/bottom', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - auth : { - debugId : lib.debugId, - headers : maybeTransferEncodingChunked({ - connection : '', - date : '', - 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', - 'x-powered-by' : 'Express', - }), - statusCode : 401, - uri : lib.urls.http + '/auth/bottom' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/auth/bottom', - method : 'GET', - headers : { - authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost' - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should capture a complicated redirect', function(done) { - request(lib.urls.https + '/auth/top/http', { - auth : { - user : 'admin', - pass : 'mypass', - sendImmediately : false + it('should capture a cross-protocol redirect', function(done) { + request(lib.urls.https + '/middle/http', function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.https + '/middle/http', + method : 'GET', + headers : { + host : 'localhost' } - }, function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.https + '/auth/top/http', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - auth : { - debugId : lib.debugId, - headers : maybeTransferEncodingChunked({ - connection : '', - date : '', - 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', - 'x-powered-by' : 'Express', - }), - statusCode : 401, - uri : lib.urls.https + '/auth/top/http' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.https + '/auth/top/http', - method : 'GET', - headers : { - authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost' - } - } - }, { - redirect : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '62', - 'content-type' : 'text/plain; charset=utf-8', - date : '', - location : lib.urls.http + '/middle', - vary : 'Accept', - 'x-powered-by' : 'Express', - }, - statusCode : 302, - uri : lib.urls.http + '/middle' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/middle', - method : 'GET', - headers : { - authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http - } - } - }, { - redirect : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '41', - 'content-type' : 'text/plain; charset=utf-8', - date : '', - location : '/bottom', - vary : 'Accept', - 'x-powered-by' : 'Express', - }, - statusCode : 302, - uri : lib.urls.http + '/bottom' - } - }, { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', - host : 'localhost:' + lib.ports.http - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); + } + }, { + redirect : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '62', + 'content-type' : 'text/plain; charset=utf-8', + date : '', + location : lib.urls.http + '/bottom', + vary : 'Accept', + 'x-powered-by' : 'Express', + }, + statusCode : 302, + uri : lib.urls.http + '/bottom' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + host : 'localhost:' + lib.ports.http + } + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should capture POST data and 404 responses', function(done) { - request({ - uri : lib.urls.http + '/bottom', - method : 'POST', - form : { - formKey : 'formData' + it('should capture an auth challenge', function(done) { + request(lib.urls.http + '/auth/bottom', { + auth : { + user : 'admin', + pass : 'mypass', + sendImmediately : false + } + }, function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/auth/bottom', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + auth : { + debugId : lib.debugId, + headers : maybeTransferEncodingChunked({ + connection : '', + date : '', + 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', + 'x-powered-by' : 'Express', + }), + statusCode : 401, + uri : lib.urls.http + '/auth/bottom' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/auth/bottom', + method : 'GET', + headers : { + authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', + host : 'localhost' } - }, function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'POST', - headers : { - host : 'localhost', - 'content-length' : 16, - 'content-type' : '' - }, - body : 'formKey=formData' - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '20', - 'content-type' : 'text/html; charset=utf-8', - date : '', - 'x-powered-by' : 'Express' - }, - statusCode : 404, - body : 'Cannot POST /bottom\n' - } - } - ]); - done(); - }); - }); + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should capture JSON responses', function(done) { - request({ - uri : lib.urls.http + '/bottom', - json : true - }, function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - accept : 'application/json', - host : 'localhost' - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '15', - 'content-type' : 'application/json; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : { - key : 'value' - } - } - } - ]); - done(); - }); - }); + it('should capture a complicated redirect', function(done) { + request(lib.urls.https + '/auth/top/http', { + auth : { + user : 'admin', + pass : 'mypass', + sendImmediately : false + } + }, function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.https + '/auth/top/http', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + auth : { + debugId : lib.debugId, + headers : maybeTransferEncodingChunked({ + connection : '', + date : '', + 'www-authenticate' : 'Digest realm="Users" <+nonce,qop>', + 'x-powered-by' : 'Express', + }), + statusCode : 401, + uri : lib.urls.https + '/auth/top/http' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.https + '/auth/top/http', + method : 'GET', + headers : { + authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', + host : 'localhost' + } + } + }, { + redirect : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '62', + 'content-type' : 'text/plain; charset=utf-8', + date : '', + location : lib.urls.http + '/middle', + vary : 'Accept', + 'x-powered-by' : 'Express', + }, + statusCode : 302, + uri : lib.urls.http + '/middle' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/middle', + method : 'GET', + headers : { + authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', + host : 'localhost:' + lib.ports.http + } + } + }, { + redirect : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '41', + 'content-type' : 'text/plain; charset=utf-8', + date : '', + location : '/bottom', + vary : 'Accept', + 'x-powered-by' : 'Express', + }, + statusCode : 302, + uri : lib.urls.http + '/bottom' + } + }, { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + authorization : 'Digest username="admin" <+realm,nonce,uri,qop,response,nc,cnonce>', + host : 'localhost:' + lib.ports.http + } + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should work with the result of request.defaults()', function(done) { - proto.should.have.property('_initBeforeDebug'); - proto.init = proto._initBeforeDebug; - delete proto._initBeforeDebug; + it('should capture POST data and 404 responses', function(done) { + request({ + uri : lib.urls.http + '/bottom', + method : 'POST', + form : { + formKey : 'formData' + } + }, function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'POST', + headers : { + host : 'localhost', + 'content-length' : 16, + 'content-type' : '' + }, + body : 'formKey=formData' + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '20', + 'content-type' : 'text/html; charset=utf-8', + date : '', + 'x-powered-by' : 'Express' + }, + statusCode : 404, + body : 'Cannot POST /bottom\n' + } + } + ]) + done() + }) + }) - request = require('request').defaults({ + it('should capture JSON responses', function(done) { + request({ + uri : lib.urls.http + '/bottom', + json : true + }, function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + accept : 'application/json', + host : 'localhost' + } + } + }, { + response : { + debugId : lib.debugId, headers : { - host : 'localhost' + connection : '', + 'content-length' : '15', + 'content-type' : 'application/json; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' }, - }); + statusCode : 200, + body : { + key : 'value' + } + } + } + ]) + done() + }) + }) + + it('should work with the result of request.defaults()', function(done) { + proto.should.have.property('_initBeforeDebug') + proto.init = proto._initBeforeDebug + delete proto._initBeforeDebug - lib.enableDebugging(request); + request = require('request').defaults({ + headers : { + host : 'localhost' + }, + }) - request(lib.urls.http + '/bottom', function(err, res, body) { - should.not.exist(err); - lib.fixVariableHeaders(); - lib.requests.should.eql([ - { - request : { - debugId : lib.debugId, - uri : lib.urls.http + '/bottom', - method : 'GET', - headers : { - host : 'localhost' - } - } - }, { - response : { - debugId : lib.debugId, - headers : { - connection : '', - 'content-length' : '10', - 'content-type' : 'text/html; charset=utf-8', - date : '', - etag : 'W/""', - 'x-powered-by' : 'Express' - }, - statusCode : 200, - body : 'Request OK' - } - } - ]); - done(); - }); - }); + lib.enableDebugging(request) + + request(lib.urls.http + '/bottom', function(err, res, body) { + should.not.exist(err) + lib.fixVariableHeaders() + lib.requests.should.eql([ + { + request : { + debugId : lib.debugId, + uri : lib.urls.http + '/bottom', + method : 'GET', + headers : { + host : 'localhost' + } + } + }, { + response : { + debugId : lib.debugId, + headers : { + connection : '', + 'content-length' : '10', + 'content-type' : 'text/html; charset=utf-8', + date : '', + etag : 'W/""', + 'x-powered-by' : 'Express' + }, + statusCode : 200, + body : 'Request OK' + } + } + ]) + done() + }) + }) - it('should not capture anything after stopDebugging()', function(done) { - request.stopDebugging(); - request(lib.urls.http + '/bottom', function(err, res, body) { - should.not.exist(err); - lib.requests.should.eql([]); - done(); - }); - }); -}); + it('should not capture anything after stopDebugging()', function(done) { + request.stopDebugging() + request(lib.urls.http + '/bottom', function(err, res, body) { + should.not.exist(err) + lib.requests.should.eql([]) + done() + }) + }) +}) diff --git a/test/lib/index.js b/test/lib/index.js index a8aaf9a..9cddf98 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -1,144 +1,144 @@ var engine = require('detect-engine'), - express = require('express'), - fs = require('fs'), - http = require('http'), - https = require('https'), - mocha = require('mocha'), - passport = require('passport'), - DigestStrategy = require('passport-http').DigestStrategy, - path = require('path'), - should = require('should'), - util = require('util'); + express = require('express'), + fs = require('fs'), + http = require('http'), + https = require('https'), + mocha = require('mocha'), + passport = require('passport'), + DigestStrategy = require('passport-http').DigestStrategy, + path = require('path'), + should = require('should'), + util = require('util') var app, - ports = { - http : 8480, - https : 8443 - }; + ports = { + http : 8480, + https : 8443 + } -exports.ports = ports; -exports.requests = []; -exports.urls = {}; -exports.debugId = 0; +exports.ports = ports +exports.requests = [] +exports.urls = {} +exports.debugId = 0 for (var proto in ports) { - exports.urls[proto] = util.format( - '%s://localhost:%d', - proto, - ports[proto]); + exports.urls[proto] = util.format( + '%s://localhost:%d', + proto, + ports[proto]) } exports.enableDebugging = function(request) { - // enable debugging - require('../..')(request, function(type, data, r) { - var obj = {}; - obj[type] = data; - exports.requests.push(obj); - if (typeof r._initBeforeDebug != 'function') { - throw new Error('Expected a Request instance here.'); - } - }); -}; + // enable debugging + require('../..')(request, function(type, data, r) { + var obj = {} + obj[type] = data + exports.requests.push(obj) + if (typeof r._initBeforeDebug != 'function') { + throw new Error('Expected a Request instance here.') + } + }) +} exports.clearRequests = function() { - exports.requests = []; - exports.debugId++; -}; + exports.requests = [] + exports.debugId++ +} var fixHeader = { - date : function(val) { - return ''; - }, - etag : function(val) { - return val.split('"')[0] + '""'; - }, - connection : function(val) { - return val.replace(/^(close|keep-alive)$/, ''); - }, - authorization : function(val) { - var arr = val.split(', '); - if (arr.length > 1) { - val = util.format( - '%s <+%s>', - arr[0], - arr.slice(1).map(function(v) { - return v.split('=')[0] - }).join(',')); - } - return val; - }, - referer : function(val) { - return null; - }, - 'content-type' : function(val) { - return val.replace(/^application\/x-www-form-urlencoded(; charset=utf-8)?$/, ''); - }, - 'content-length' : function(val, obj) { - if (engine == 'iojs' && obj.statusCode == 401) { - // io.js sends content-length here, Node does not - return null; - } else { - return val; - } + date : function(val) { + return '' + }, + etag : function(val) { + return val.split('"')[0] + '""' + }, + connection : function(val) { + return val.replace(/^(close|keep-alive)$/, '') + }, + authorization : function(val) { + var arr = val.split(', ') + if (arr.length > 1) { + val = util.format( + '%s <+%s>', + arr[0], + arr.slice(1).map(function(v) { + return v.split('=')[0] + }).join(',')) } -}; -fixHeader['www-authenticate'] = fixHeader.authorization; + return val + }, + referer : function(val) { + return null + }, + 'content-type' : function(val) { + return val.replace(/^application\/x-www-form-urlencoded( charset=utf-8)?$/, '') + }, + 'content-length' : function(val, obj) { + if (engine == 'iojs' && obj.statusCode == 401) { + // io.js sends content-length here, Node does not + return null + } else { + return val + } + } +} +fixHeader['www-authenticate'] = fixHeader.authorization exports.fixVariableHeaders = function() { - exports.requests.forEach(function(req) { - for (var type in req) { - for (var header in req[type].headers) { - if (fixHeader[header]) { - var fixed = fixHeader[header](req[type].headers[header], req[type]); - if (fixed === null) { - delete req[type].headers[header]; - } else { - req[type].headers[header] = fixed; - } - } - } + exports.requests.forEach(function(req) { + for (var type in req) { + for (var header in req[type].headers) { + if (fixHeader[header]) { + var fixed = fixHeader[header](req[type].headers[header], req[type]) + if (fixed === null) { + delete req[type].headers[header] + } else { + req[type].headers[header] = fixed + } } - }); -}; + } + } + }) +} exports.startServers = function() { - passport.use(new DigestStrategy( - { qop : 'auth' }, - function(user, done) { - return done(null, 'admin', 'mypass'); - } - )); + passport.use(new DigestStrategy( + { qop : 'auth' }, + function(user, done) { + return done(null, 'admin', 'mypass') + } + )) - app = express(); + app = express() - app.use(passport.initialize()); + app.use(passport.initialize()) - function handleRequest(req, res) { - if (req.params.level == 'bottom') { - if (req.header('accept') == 'application/json') { - res.json({ key : 'value' }); - } else { - res.send('Request OK'); - } - return; - } - var level = (req.params.level == 'top' ? 'middle' : 'bottom'); - if (req.params.proto && req.params.proto != req.protocol) { - res.redirect(exports.urls[req.params.proto] + '/' + level); - } else { - res.redirect('/' + level); - } + function handleRequest(req, res) { + if (req.params.level == 'bottom') { + if (req.header('accept') == 'application/json') { + res.json({ key : 'value' }) + } else { + res.send('Request OK') + } + return } + var level = (req.params.level == 'top' ? 'middle' : 'bottom') + if (req.params.proto && req.params.proto != req.protocol) { + res.redirect(exports.urls[req.params.proto] + '/' + level) + } else { + res.redirect('/' + level) + } + } - var auth = passport.authenticate('digest', { session : false }); - app.get('/auth/:level/:proto?', auth, handleRequest); + var auth = passport.authenticate('digest', { session : false }) + app.get('/auth/:level/:proto?', auth, handleRequest) - app.get('/:level/:proto?', handleRequest); + app.get('/:level/:proto?', handleRequest) - http.createServer(app).listen(ports.http); + http.createServer(app).listen(ports.http) - https.createServer({ - key : fs.readFileSync(path.join(__dirname, 'key.pem')), - cert : fs.readFileSync(path.join(__dirname, 'cert.pem')) - }, app).listen(ports.https); -}; + https.createServer({ + key : fs.readFileSync(path.join(__dirname, 'key.pem')), + cert : fs.readFileSync(path.join(__dirname, 'cert.pem')) + }, app).listen(ports.https) +} From 2261aaaf34532b6bc7f96090aaec011dbc6dcb41 Mon Sep 17 00:00:00 2001 From: simov Date: Mon, 18 May 2015 20:47:46 +0300 Subject: [PATCH 14/14] Fix missing ; in regex --- test/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/index.js b/test/lib/index.js index 9cddf98..5ed5ee0 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -71,7 +71,7 @@ var fixHeader = { return null }, 'content-type' : function(val) { - return val.replace(/^application\/x-www-form-urlencoded( charset=utf-8)?$/, '') + return val.replace(/^application\/x-www-form-urlencoded(; charset=utf-8)?$/, '') }, 'content-length' : function(val, obj) { if (engine == 'iojs' && obj.statusCode == 401) {