diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 5a5948158..000000000 --- a/.eslintrc +++ /dev/null @@ -1,45 +0,0 @@ -{ - "env": { - "node": true - }, - "rules": { - // 2-space indentation - "indent": [2, 2, {"SwitchCase": 1}], - // Disallow semi-colons, unless needed to disambiguate statement - "semi": [2, "never"], - // Require strings to use single quotes - "quotes": [2, "single"], - // Require curly braces for all control statements - "curly": 2, - // Disallow using variables and functions before they've been defined - "no-use-before-define": 2, - // Allow any case for variable naming - "camelcase": 0, - // Disallow unused variables, except as function arguments - "no-unused-vars": [2, {"args":"none"}], - // Allow leading underscores for method names - // REASON: we use underscores to denote private methods - "no-underscore-dangle": 0, - // Allow multi spaces around operators since they are - // used for alignment. This is not consistent in the - // code. - "no-multi-spaces": 0, - // Style rule is: most objects use { beforeColon: false, afterColon: true }, unless aligning which uses: - // - // { - // beforeColon : true, - // afterColon : true - // } - // - // eslint can't handle this, so the check is disabled. - "key-spacing": 0, - // Allow shadowing vars in outer scope (needs discussion) - "no-shadow": 0, - // Use if () { } - // ^ space - "keyword-spacing": [2, {"after": true}], - // Use if () { } - // ^ space - "space-before-blocks": [2, "always"] - } -} diff --git a/index.js b/index.js index 9ec65ea26..979260db8 100755 --- a/index.js +++ b/index.js @@ -14,15 +14,14 @@ 'use strict' -var extend = require('extend') - , cookies = require('./lib/cookies') - , helpers = require('./lib/helpers') +var extend = require('extend') +var cookies = require('./lib/cookies') +var helpers = require('./lib/helpers') var paramsHaveRequestBody = helpers.paramsHaveRequestBody - // organize params for patch, post, put, head, del -function initParams(uri, options, callback) { +function initParams (uri, options, callback) { if (typeof options === 'function') { callback = options } @@ -81,7 +80,6 @@ request.cookie = function (str) { } function wrapRequestMethod (method, options, requester, verb) { - return function (uri, opts, callback) { var params = initParams(uri, opts, callback) @@ -112,15 +110,15 @@ request.defaults = function (options, requester) { options = {} } - var defaults = wrapRequestMethod(self, options, requester) + var defaults = wrapRequestMethod(self, options, requester) var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete'] - verbs.forEach(function(verb) { - defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb) + verbs.forEach(function (verb) { + defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb) }) - defaults.cookie = wrapRequestMethod(self.cookie, options, requester) - defaults.jar = self.jar + defaults.cookie = wrapRequestMethod(self.cookie, options, requester) + defaults.jar = self.jar defaults.defaults = self.defaults return defaults } @@ -146,11 +144,11 @@ request.initParams = initParams // Backwards compatibility for request.debug Object.defineProperty(request, 'debug', { - enumerable : true, - get : function() { + enumerable: true, + get: function () { return request.Request.debug }, - set : function(debug) { + set: function (debug) { request.Request.debug = debug } }) diff --git a/lib/auth.js b/lib/auth.js index 559ca57be..42f9adaec 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -1,12 +1,11 @@ 'use strict' var caseless = require('caseless') - , uuid = require('uuid') - , helpers = require('./helpers') +var uuid = require('uuid') +var helpers = require('./helpers') var md5 = helpers.md5 - , toBase64 = helpers.toBase64 - +var toBase64 = helpers.toBase64 function Auth (request) { // define all public properties here @@ -126,7 +125,7 @@ Auth.prototype.digest = function (method, path, authHeader) { Auth.prototype.onRequest = function (user, pass, sendImmediately, bearer) { var self = this - , request = self.request + var request = self.request var authHeader if (bearer === undefined && user === undefined) { @@ -143,7 +142,7 @@ Auth.prototype.onRequest = function (user, pass, sendImmediately, bearer) { Auth.prototype.onResponse = function (response) { var self = this - , request = self.request + var request = self.request if (!self.hasAuth || self.sentAuth) { return null } diff --git a/lib/cookies.js b/lib/cookies.js index 412c07d63..bd5d46bea 100644 --- a/lib/cookies.js +++ b/lib/cookies.js @@ -3,10 +3,9 @@ var tough = require('tough-cookie') var Cookie = tough.Cookie - , CookieJar = tough.CookieJar +var CookieJar = tough.CookieJar - -exports.parse = function(str) { +exports.parse = function (str) { if (str && str.uri) { str = str.uri } @@ -17,23 +16,23 @@ exports.parse = function(str) { } // Adapt the sometimes-Async api of tough.CookieJar to our requirements -function RequestJar(store) { +function RequestJar (store) { var self = this self._jar = new CookieJar(store, {looseMode: true}) } -RequestJar.prototype.setCookie = function(cookieOrStr, uri, options) { +RequestJar.prototype.setCookie = function (cookieOrStr, uri, options) { var self = this return self._jar.setCookieSync(cookieOrStr, uri, options || {}) } -RequestJar.prototype.getCookieString = function(uri) { +RequestJar.prototype.getCookieString = function (uri) { var self = this return self._jar.getCookieStringSync(uri) } -RequestJar.prototype.getCookies = function(uri) { +RequestJar.prototype.getCookies = function (uri) { var self = this return self._jar.getCookiesSync(uri) } -exports.jar = function(store) { +exports.jar = function (store) { return new RequestJar(store) } diff --git a/lib/getProxyFromURI.js b/lib/getProxyFromURI.js index c2013a6e1..4633ba5f6 100644 --- a/lib/getProxyFromURI.js +++ b/lib/getProxyFromURI.js @@ -1,33 +1,33 @@ 'use strict' -function formatHostname(hostname) { +function formatHostname (hostname) { // canonicalize the hostname, so that 'oogle.com' won't match 'google.com' return hostname.replace(/^\.*/, '.').toLowerCase() } -function parseNoProxyZone(zone) { +function parseNoProxyZone (zone) { zone = zone.trim().toLowerCase() var zoneParts = zone.split(':', 2) - , zoneHost = formatHostname(zoneParts[0]) - , zonePort = zoneParts[1] - , hasPort = zone.indexOf(':') > -1 + var zoneHost = formatHostname(zoneParts[0]) + var zonePort = zoneParts[1] + var hasPort = zone.indexOf(':') > -1 return {hostname: zoneHost, port: zonePort, hasPort: hasPort} } -function uriInNoProxy(uri, noProxy) { +function uriInNoProxy (uri, noProxy) { var port = uri.port || (uri.protocol === 'https:' ? '443' : '80') - , hostname = formatHostname(uri.hostname) - , noProxyList = noProxy.split(',') + var hostname = formatHostname(uri.hostname) + var noProxyList = noProxy.split(',') // iterate through the noProxyList until it finds a match. - return noProxyList.map(parseNoProxyZone).some(function(noProxyZone) { + return noProxyList.map(parseNoProxyZone).some(function (noProxyZone) { var isMatchedAt = hostname.indexOf(noProxyZone.hostname) - , hostnameMatched = ( - isMatchedAt > -1 && - (isMatchedAt === hostname.length - noProxyZone.hostname.length) - ) + var hostnameMatched = ( + isMatchedAt > -1 && + (isMatchedAt === hostname.length - noProxyZone.hostname.length) + ) if (noProxyZone.hasPort) { return (port === noProxyZone.port) && hostnameMatched @@ -37,7 +37,7 @@ function uriInNoProxy(uri, noProxy) { }) } -function getProxyFromURI(uri) { +function getProxyFromURI (uri) { // Decide the proper request proxy to use based on the request URI object and the // environmental variables (NO_PROXY, HTTP_PROXY, etc.) // respect NO_PROXY environment variables (see: http://lynx.isc.org/current/breakout/lynx_help/keystrokes/environments.html) @@ -60,14 +60,14 @@ function getProxyFromURI(uri) { if (uri.protocol === 'http:') { return process.env.HTTP_PROXY || - process.env.http_proxy || null + process.env.http_proxy || null } if (uri.protocol === 'https:') { return process.env.HTTPS_PROXY || - process.env.https_proxy || - process.env.HTTP_PROXY || - process.env.http_proxy || null + process.env.https_proxy || + process.env.HTTP_PROXY || + process.env.http_proxy || null } // if none of that works, return null diff --git a/lib/har.js b/lib/har.js index 305957487..2f660309d 100644 --- a/lib/har.js +++ b/lib/har.js @@ -71,14 +71,10 @@ Har.prototype.prep = function (data) { 'multipart/related', 'multipart/form-data', 'multipart/alternative'])) { - // reset values data.postData.mimeType = 'multipart/form-data' - } - - else if (some([ + } else if (some([ 'application/x-www-form-urlencoded'])) { - if (!data.postData.params) { data.postData.text = '' } else { @@ -87,14 +83,11 @@ Har.prototype.prep = function (data) { // always overwrite data.postData.text = qs.stringify(data.postData.paramsObj) } - } - - else if (some([ + } else if (some([ 'text/json', 'text/x-json', 'application/json', 'application/x-json'])) { - data.postData.mimeType = 'application/json' if (data.postData.text) { @@ -168,14 +161,12 @@ Har.prototype.options = function (options) { } if (test('application/x-www-form-urlencoded')) { options.form = req.postData.paramsObj - } - else if (test('application/json')) { + } else if (test('application/json')) { if (req.postData.jsonObj) { options.body = req.postData.jsonObj options.json = true } - } - else if (test('multipart/form-data')) { + } else if (test('multipart/form-data')) { options.formData = {} req.postData.params.forEach(function (param) { @@ -202,8 +193,7 @@ Har.prototype.options = function (options) { options.formData[param.name] = attachment }) - } - else { + } else { if (req.postData.text) { options.body = req.postData.text } diff --git a/lib/helpers.js b/lib/helpers.js index 05c77a0bd..8b2a7e6eb 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,14 +1,14 @@ 'use strict' var jsonSafeStringify = require('json-stringify-safe') - , crypto = require('crypto') - , Buffer = require('safe-buffer').Buffer +var crypto = require('crypto') +var Buffer = require('safe-buffer').Buffer var defer = typeof setImmediate === 'undefined' ? process.nextTick : setImmediate -function paramsHaveRequestBody(params) { +function paramsHaveRequestBody (params) { return ( params.body || params.requestBodyStream || @@ -57,10 +57,10 @@ function version () { } exports.paramsHaveRequestBody = paramsHaveRequestBody -exports.safeStringify = safeStringify -exports.md5 = md5 -exports.isReadStream = isReadStream -exports.toBase64 = toBase64 -exports.copy = copy -exports.version = version -exports.defer = defer +exports.safeStringify = safeStringify +exports.md5 = md5 +exports.isReadStream = isReadStream +exports.toBase64 = toBase64 +exports.copy = copy +exports.version = version +exports.defer = defer diff --git a/lib/multipart.js b/lib/multipart.js index fc7b50276..d6b981277 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -1,10 +1,9 @@ 'use strict' var uuid = require('uuid') - , CombinedStream = require('combined-stream') - , isstream = require('isstream') - , Buffer = require('safe-buffer').Buffer - +var CombinedStream = require('combined-stream') +var isstream = require('isstream') +var Buffer = require('safe-buffer').Buffer function Multipart (request) { this.request = request @@ -15,8 +14,8 @@ function Multipart (request) { Multipart.prototype.isChunked = function (options) { var self = this - , chunked = false - , parts = options.data || options + var chunked = false + var parts = options.data || options if (!parts.forEach) { self.request.emit('error', new Error('Argument error, options.multipart.')) @@ -103,7 +102,7 @@ Multipart.prototype.onRequest = function (options) { var self = this var chunked = self.isChunked(options) - , parts = options.data || options + var parts = options.data || options self.setHeaders(chunked) self.chunked = chunked diff --git a/lib/oauth.js b/lib/oauth.js index 13b693773..0c9c57cfe 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -1,13 +1,12 @@ 'use strict' var url = require('url') - , qs = require('qs') - , caseless = require('caseless') - , uuid = require('uuid') - , oauth = require('oauth-sign') - , crypto = require('crypto') - , Buffer = require('safe-buffer').Buffer - +var qs = require('qs') +var caseless = require('caseless') +var uuid = require('uuid') +var oauth = require('oauth-sign') +var crypto = require('crypto') +var Buffer = require('safe-buffer').Buffer function OAuth (request) { this.request = request @@ -23,7 +22,7 @@ OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) oa.oauth_version = '1.0' } if (!oa.oauth_timestamp) { - oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString() + oa.oauth_timestamp = Math.floor(Date.now() / 1000).toString() } if (!oa.oauth_nonce) { oa.oauth_nonce = uuid().replace(/-/g, '') @@ -32,11 +31,11 @@ OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) oa.oauth_signature_method = 'HMAC-SHA1' } - var consumer_secret_or_private_key = oa.oauth_consumer_secret || oa.oauth_private_key + var consumer_secret_or_private_key = oa.oauth_consumer_secret || oa.oauth_private_key // eslint-disable-line camelcase delete oa.oauth_consumer_secret delete oa.oauth_private_key - var token_secret = oa.oauth_token_secret + var token_secret = oa.oauth_token_secret // eslint-disable-line camelcase delete oa.oauth_token_secret var realm = oa.oauth_realm @@ -51,8 +50,9 @@ OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) method, baseurl, params, - consumer_secret_or_private_key, - token_secret) + consumer_secret_or_private_key, // eslint-disable-line camelcase + token_secret // eslint-disable-line camelcase + ) if (realm) { oa.realm = realm @@ -61,7 +61,7 @@ OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) return oa } -OAuth.prototype.buildBodyHash = function(_oauth, body) { +OAuth.prototype.buildBodyHash = function (_oauth, body) { if (['HMAC-SHA1', 'RSA-SHA1'].indexOf(_oauth.signature_method || 'HMAC-SHA1') < 0) { this.request.emit('error', new Error('oauth: ' + _oauth.signature_method + ' signature_method not supported with body_hash signing.')) @@ -96,16 +96,16 @@ OAuth.prototype.onRequest = function (_oauth) { self.params = _oauth var uri = self.request.uri || {} - , method = self.request.method || '' - , headers = caseless(self.request.headers) - , body = self.request.body || '' - , qsLib = self.request.qsLib || qs + var method = self.request.method || '' + var headers = caseless(self.request.headers) + var body = self.request.body || '' + var qsLib = self.request.qsLib || qs var form - , query - , contentType = headers.get('content-type') || '' - , formContentType = 'application/x-www-form-urlencoded' - , transport = _oauth.transport_method || 'header' + var query + var contentType = headers.get('content-type') || '' + var formContentType = 'application/x-www-form-urlencoded' + var transport = _oauth.transport_method || 'header' if (contentType.slice(0, formContentType.length) === formContentType) { contentType = formContentType diff --git a/lib/querystring.js b/lib/querystring.js index baf5e8021..4a32cd149 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -1,8 +1,7 @@ 'use strict' var qs = require('qs') - , querystring = require('querystring') - +var querystring = require('querystring') function Querystring (request) { this.request = request @@ -13,7 +12,7 @@ function Querystring (request) { } Querystring.prototype.init = function (options) { - if (this.lib) {return} + if (this.lib) { return } this.useQuerystring = options.useQuerystring this.lib = (this.useQuerystring ? querystring : qs) diff --git a/lib/redirect.js b/lib/redirect.js index f8604491f..b9150e77c 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -9,7 +9,7 @@ function Redirect (request) { this.followRedirects = true this.followAllRedirects = false this.followOriginalHttpMethod = false - this.allowRedirect = function () {return true} + this.allowRedirect = function () { return true } this.maxRedirects = 10 this.redirects = [] this.redirectsFollowed = 0 @@ -44,7 +44,7 @@ Redirect.prototype.onRequest = function (options) { Redirect.prototype.redirectTo = function (response) { var self = this - , request = self.request + var request = self.request var redirectTo = null if (response.statusCode >= 300 && response.statusCode < 400 && response.caseless.has('location')) { @@ -78,7 +78,7 @@ Redirect.prototype.redirectTo = function (response) { Redirect.prototype.onResponse = function (response) { var self = this - , request = self.request + var request = self.request var redirectTo = self.redirectTo(response) if (!redirectTo || !self.allowRedirect.call(request, response)) { @@ -112,13 +112,10 @@ Redirect.prototype.onResponse = function (response) { delete request.agent } - self.redirects.push( - { statusCode : response.statusCode - , redirectUri: redirectTo - } - ) - if (self.followAllRedirects && request.method !== 'HEAD' - && response.statusCode !== 401 && response.statusCode !== 307) { + self.redirects.push({ statusCode: response.statusCode, redirectUri: redirectTo }) + + if (self.followAllRedirects && request.method !== 'HEAD' && + response.statusCode !== 401 && response.statusCode !== 307) { request.method = self.followOriginalHttpMethod ? request.method : 'GET' } // request.method = 'GET' // Force all redirects to use GET || commented out fixes #215 diff --git a/lib/tunnel.js b/lib/tunnel.js index bf96a8fec..4479003f6 100644 --- a/lib/tunnel.js +++ b/lib/tunnel.js @@ -1,7 +1,7 @@ 'use strict' var url = require('url') - , tunnel = require('tunnel-agent') +var tunnel = require('tunnel-agent') var defaultProxyHeaderWhiteList = [ 'accept', @@ -31,10 +31,10 @@ var defaultProxyHeaderExclusiveList = [ 'proxy-authorization' ] -function constructProxyHost(uriObject) { +function constructProxyHost (uriObject) { var port = uriObject.port - , protocol = uriObject.protocol - , proxyHost = uriObject.hostname + ':' + var protocol = uriObject.protocol + var proxyHost = uriObject.hostname + ':' if (port) { proxyHost += port @@ -47,7 +47,7 @@ function constructProxyHost(uriObject) { return proxyHost } -function constructProxyHeaderWhiteList(headers, proxyHeaderWhiteList) { +function constructProxyHeaderWhiteList (headers, proxyHeaderWhiteList) { var whiteList = proxyHeaderWhiteList .reduce(function (set, header) { set[header.toLowerCase()] = true @@ -68,41 +68,40 @@ function constructTunnelOptions (request, proxyHeaders) { var proxy = request.proxy var tunnelOptions = { - proxy : { - host : proxy.hostname, - port : +proxy.port, - proxyAuth : proxy.auth, - headers : proxyHeaders + proxy: { + host: proxy.hostname, + port: +proxy.port, + proxyAuth: proxy.auth, + headers: proxyHeaders }, - headers : request.headers, - ca : request.ca, - cert : request.cert, - key : request.key, - passphrase : request.passphrase, - pfx : request.pfx, - ciphers : request.ciphers, - rejectUnauthorized : request.rejectUnauthorized, - secureOptions : request.secureOptions, - secureProtocol : request.secureProtocol + headers: request.headers, + ca: request.ca, + cert: request.cert, + key: request.key, + passphrase: request.passphrase, + pfx: request.pfx, + ciphers: request.ciphers, + rejectUnauthorized: request.rejectUnauthorized, + secureOptions: request.secureOptions, + secureProtocol: request.secureProtocol } return tunnelOptions } -function constructTunnelFnName(uri, proxy) { +function constructTunnelFnName (uri, proxy) { var uriProtocol = (uri.protocol === 'https:' ? 'https' : 'http') var proxyProtocol = (proxy.protocol === 'https:' ? 'Https' : 'Http') return [uriProtocol, proxyProtocol].join('Over') } -function getTunnelFn(request) { +function getTunnelFn (request) { var uri = request.uri var proxy = request.proxy var tunnelFnName = constructTunnelFnName(uri, proxy) return tunnel[tunnelFnName] } - function Tunnel (request) { this.request = request this.proxyHeaderWhiteList = defaultProxyHeaderWhiteList @@ -114,8 +113,8 @@ function Tunnel (request) { Tunnel.prototype.isEnabled = function () { var self = this - , request = self.request - // Tunnel HTTPS by default. Allow the user to override this setting. + var request = self.request + // Tunnel HTTPS by default. Allow the user to override this setting. // If self.tunnelOverride is set (the user specified a value), use it. if (typeof self.tunnelOverride !== 'undefined') { @@ -133,7 +132,7 @@ Tunnel.prototype.isEnabled = function () { Tunnel.prototype.setup = function (options) { var self = this - , request = self.request + var request = self.request options = options || {} diff --git a/package.json b/package.json index a8f659073..61ef77ca6 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "test-ci": "taper tests/test-*.js", "test-cov": "istanbul cover tape tests/test-*.js", "test-browser": "node tests/browser/start.js", - "lint": "eslint lib/ *.js tests/ && echo Lint passed." + "lint": "standard" }, "devDependencies": { "bluebird": "^3.2.1", @@ -64,7 +64,6 @@ "buffer-equal": "^1.0.0", "codecov": "^2.0.2", "coveralls": "^2.11.4", - "eslint": "^2.5.3", "function-bind": "^1.0.2", "istanbul": "^0.4.0", "karma": "^1.1.1", @@ -76,12 +75,12 @@ "phantomjs-prebuilt": "^2.1.3", "rimraf": "^2.2.8", "server-destroy": "^1.0.1", + "standard": "^9.0.0", "tape": "^4.6.0", "taper": "^0.5.0" }, "greenkeeper": { "ignore": [ - "eslint", "hawk", "har-validator" ] diff --git a/request.js b/request.js index 467524ba4..ff19ca39c 100644 --- a/request.js +++ b/request.js @@ -1,48 +1,47 @@ 'use strict' var http = require('http') - , https = require('https') - , url = require('url') - , util = require('util') - , stream = require('stream') - , zlib = require('zlib') - , hawk = require('hawk') - , aws2 = require('aws-sign2') - , aws4 = require('aws4') - , httpSignature = require('http-signature') - , mime = require('mime-types') - , stringstream = require('stringstream') - , caseless = require('caseless') - , ForeverAgent = require('forever-agent') - , FormData = require('form-data') - , extend = require('extend') - , isstream = require('isstream') - , isTypedArray = require('is-typedarray').strict - , helpers = require('./lib/helpers') - , cookies = require('./lib/cookies') - , getProxyFromURI = require('./lib/getProxyFromURI') - , Querystring = require('./lib/querystring').Querystring - , Har = require('./lib/har').Har - , Auth = require('./lib/auth').Auth - , OAuth = require('./lib/oauth').OAuth - , Multipart = require('./lib/multipart').Multipart - , Redirect = require('./lib/redirect').Redirect - , Tunnel = require('./lib/tunnel').Tunnel - , now = require('performance-now') - , Buffer = require('safe-buffer').Buffer +var https = require('https') +var url = require('url') +var util = require('util') +var stream = require('stream') +var zlib = require('zlib') +var hawk = require('hawk') +var aws2 = require('aws-sign2') +var aws4 = require('aws4') +var httpSignature = require('http-signature') +var mime = require('mime-types') +var stringstream = require('stringstream') +var caseless = require('caseless') +var ForeverAgent = require('forever-agent') +var FormData = require('form-data') +var extend = require('extend') +var isstream = require('isstream') +var isTypedArray = require('is-typedarray').strict +var helpers = require('./lib/helpers') +var cookies = require('./lib/cookies') +var getProxyFromURI = require('./lib/getProxyFromURI') +var Querystring = require('./lib/querystring').Querystring +var Har = require('./lib/har').Har +var Auth = require('./lib/auth').Auth +var OAuth = require('./lib/oauth').OAuth +var Multipart = require('./lib/multipart').Multipart +var Redirect = require('./lib/redirect').Redirect +var Tunnel = require('./lib/tunnel').Tunnel +var now = require('performance-now') +var Buffer = require('safe-buffer').Buffer var safeStringify = helpers.safeStringify - , isReadStream = helpers.isReadStream - , toBase64 = helpers.toBase64 - , defer = helpers.defer - , copy = helpers.copy - , version = helpers.version - , globalCookieJar = cookies.jar() - +var isReadStream = helpers.isReadStream +var toBase64 = helpers.toBase64 +var defer = helpers.defer +var copy = helpers.copy +var version = helpers.version +var globalCookieJar = cookies.jar() var globalPool = {} -function filterForNonReserved(reserved, options) { +function filterForNonReserved (reserved, options) { // Filter out properties that are not reserved. // Reserved values are passed in at call site. @@ -56,7 +55,7 @@ function filterForNonReserved(reserved, options) { return object } -function filterOutReservedFunctions(reserved, options) { +function filterOutReservedFunctions (reserved, options) { // Filter out properties that are functions and are reserved. // Reserved values are passed in at call site. @@ -69,11 +68,10 @@ function filterOutReservedFunctions(reserved, options) { } } return object - } // Return a simpler request object to allow serialization -function requestToJSON() { +function requestToJSON () { var self = this return { uri: self.uri, @@ -83,7 +81,7 @@ function requestToJSON() { } // Return a simpler response object to allow serialization -function responseToJSON() { +function responseToJSON () { var self = this return { statusCode: self.statusCode, @@ -134,7 +132,7 @@ util.inherits(Request, stream.Stream) // Debugging Request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG) -function debug() { +function debug () { if (Request.debug) { console.error('REQUEST %s', util.format.apply(util, arguments)) } @@ -258,7 +256,7 @@ Request.prototype.init = function (options) { self.rejectUnauthorized = false } - if (!self.uri.pathname) {self.uri.pathname = '/'} + if (!self.uri.pathname) { self.uri.pathname = '/' } if (!(self.uri.host || (self.uri.hostname && self.uri.port)) && !self.uri.isUnix) { // Invalid URI: it may generate lot of bad errors, like 'TypeError: Cannot call method `indexOf` of undefined' in CookieJar @@ -300,8 +298,7 @@ Request.prototype.init = function (options) { self.jar(self._jar || options.jar) if (!self.uri.port) { - if (self.uri.protocol === 'http:') {self.uri.port = 80} - else if (self.uri.protocol === 'https:') {self.uri.port = 443} + if (self.uri.protocol === 'http:') { self.uri.port = 80 } else if (self.uri.protocol === 'https:') { self.uri.port = 443 } } if (self.proxy && !self.tunnel) { @@ -388,12 +385,12 @@ Request.prototype.init = function (options) { } if (self.uri.auth && !self.hasHeader('authorization')) { - var uriAuthPieces = self.uri.auth.split(':').map(function(item) {return self._qs.unescape(item)}) + var uriAuthPieces = self.uri.auth.split(':').map(function (item) { return self._qs.unescape(item) }) self.auth(uriAuthPieces[0], uriAuthPieces.slice(1).join(':'), true) } if (!self.tunnel && self.proxy && self.proxy.auth && !self.hasHeader('proxy-authorization')) { - var proxyAuthPieces = self.proxy.auth.split(':').map(function(item) {return self._qs.unescape(item)}) + var proxyAuthPieces = self.proxy.auth.split(':').map(function (item) { return self._qs.unescape(item) }) var authHeader = 'Basic ' + toBase64(proxyAuthPieces.join(':')) self.setHeader('proxy-authorization', authHeader) } @@ -425,11 +422,9 @@ Request.prototype.init = function (options) { var length if (typeof self.body === 'string') { length = Buffer.byteLength(self.body) - } - else if (Array.isArray(self.body)) { - length = self.body.reduce(function (a, b) {return a + b.length}, 0) - } - else { + } else if (Array.isArray(self.body)) { + length = self.body.reduce(function (a, b) { return a + b.length }, 0) + } else { length = self.body.length } @@ -451,8 +446,8 @@ Request.prototype.init = function (options) { } var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol - , defaultModules = {'http:':http, 'https:':https} - , httpModules = self.httpModules || {} + var defaultModules = {'http:': http, 'https:': https} + var httpModules = self.httpModules || {} self.httpModule = httpModules[protocol] || defaultModules[protocol] @@ -517,9 +512,9 @@ Request.prototype.init = function (options) { } } - // self.on('pipe', function () { - // console.error('You have already piped to this stream. Pipeing twice is likely to break the request.') - // }) + // self.on('pipe', function () { + // console.error('You have already piped to this stream. Pipeing twice is likely to break the request.') + // }) }) defer(function () { @@ -531,8 +526,7 @@ Request.prototype.init = function (options) { if (self._form) { if (!self._auth.hasAuth) { self._form.pipe(self) - } - else if (self._auth.hasAuth && self._auth.sentAuth) { + } else if (self._auth.hasAuth && self._auth.sentAuth) { self._form.pipe(self) } } @@ -583,7 +577,6 @@ Request.prototype.init = function (options) { self.ntick = true }) - } Request.prototype.getNewAgent = function () { @@ -778,21 +771,22 @@ Request.prototype.start = function () { self.req.on('response', self.onRequestResponse.bind(self)) self.req.on('error', self.onRequestError.bind(self)) - self.req.on('drain', function() { + self.req.on('drain', function () { self.emit('drain') }) - self.req.on('socket', function(socket) { + + self.req.on('socket', function (socket) { // `._connecting` was the old property which was made public in node v6.1.0 var isConnecting = socket._connecting || socket.connecting if (self.timing) { self.timings.socket = now() - self.startTimeNow if (isConnecting) { - var onLookupTiming = function() { + var onLookupTiming = function () { self.timings.lookup = now() - self.startTimeNow } - var onConnectTiming = function() { + var onConnectTiming = function () { self.timings.connect = now() - self.startTimeNow } @@ -800,14 +794,14 @@ Request.prototype.start = function () { socket.once('connect', onConnectTiming) // clean up timing event listeners if needed on error - self.req.once('error', function() { + self.req.once('error', function () { socket.removeListener('lookup', onLookupTiming) socket.removeListener('connect', onConnectTiming) }) } } - var setReqTimeout = function() { + var setReqTimeout = function () { // This timeout sets the amount of time to wait *between* bytes sent // from the server once connected. // @@ -829,7 +823,7 @@ Request.prototype.start = function () { // keep-alive connection) do not bother. This is important since we won't // get a 'connect' event for an already connected socket. if (isConnecting) { - var onReqSockConnect = function() { + var onReqSockConnect = function () { socket.removeListener('connect', onReqSockConnect) clearTimeout(self.timeoutTimer) self.timeoutTimer = null @@ -838,7 +832,7 @@ Request.prototype.start = function () { socket.on('connect', onReqSockConnect) - self.req.on('error', function(err) { + self.req.on('error', function (err) { // eslint-disable-line handle-callback-err socket.removeListener('connect', onReqSockConnect) }) @@ -870,8 +864,8 @@ Request.prototype.onRequestError = function (error) { if (self._aborted) { return } - if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET' - && self.agent.addRequestNoreuse) { + if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET' && + self.agent.addRequestNoreuse) { self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) } self.start() self.req.end() @@ -892,7 +886,7 @@ Request.prototype.onRequestResponse = function (response) { } debug('onRequestResponse', self.uri.href, response.statusCode, response.headers) - response.on('end', function() { + response.on('end', function () { if (self.timing) { self.timings.end = now() - self.startTimeNow response.timingStart = self.startTime @@ -948,8 +942,8 @@ Request.prototype.onRequestResponse = function (response) { // XXX This is different on 0.10, because SSL is strict by default if (self.httpModule === https && - self.strictSSL && (!response.hasOwnProperty('socket') || - !response.socket.authorized)) { + self.strictSSL && (!response.hasOwnProperty('socket') || + !response.socket.authorized)) { debug('strict ssl error', self.uri.href) var sslErr = response.hasOwnProperty('socket') ? response.socket.authorizationError : self.uri.href + ' does not support SSL' self.emit('error', new Error('SSL Error: ' + sslErr)) @@ -974,7 +968,7 @@ Request.prototype.onRequestResponse = function (response) { var targetCookieJar = (self._jar && self._jar.setCookie) ? self._jar : globalCookieJar var addCookie = function (cookie) { - //set the cookie if it's domain in the href's domain. + // set the cookie if it's domain in the href's domain. try { targetCookieJar.setCookie(cookie, self.uri.href, {ignoreError: true}) } catch (e) { @@ -1010,13 +1004,13 @@ Request.prototype.onRequestResponse = function (response) { var noBody = function (code) { return ( - self.method === 'HEAD' + self.method === 'HEAD' || // Informational - || (code >= 100 && code < 200) + (code >= 100 && code < 200) || // No Content - || code === 204 + code === 204 || // Not Modified - || code === 304 + code === 304 ) } @@ -1030,8 +1024,8 @@ Request.prototype.onRequestResponse = function (response) { // by common browsers. // Always using Z_SYNC_FLUSH is what cURL does. var zlibOptions = { - flush: zlib.Z_SYNC_FLUSH - , finishFlush: zlib.Z_SYNC_FLUSH + flush: zlib.Z_SYNC_FLUSH, + finishFlush: zlib.Z_SYNC_FLUSH } if (contentEncoding === 'gzip') { @@ -1093,13 +1087,11 @@ Request.prototype.onRequestResponse = function (response) { responseContent.on('error', function (error) { self.emit('error', error) }) - responseContent.on('close', function () {self.emit('close')}) + responseContent.on('close', function () { self.emit('close') }) if (self.callback) { self.readResponseBody(response) - } - //if no callback - else { + } else { // if no callback self.on('end', function () { if (self._aborted) { debug('aborted', self.uri.href) @@ -1114,10 +1106,10 @@ Request.prototype.onRequestResponse = function (response) { Request.prototype.readResponseBody = function (response) { var self = this - debug('reading response\'s body') + debug("reading response's body") var buffers = [] - , bufferLength = 0 - , strings = [] + var bufferLength = 0 + var strings = [] self.on('data', function (chunk) { if (!Buffer.isBuffer(chunk)) { @@ -1178,8 +1170,7 @@ Request.prototype.abort = function () { if (self.req) { self.req.abort() - } - else if (self.response) { + } else if (self.response) { self.response.destroy() } @@ -1195,8 +1186,7 @@ Request.prototype.pipeDest = function (dest) { var ctname = response.caseless.has('content-type') if (dest.setHeader) { dest.setHeader(ctname, response.headers[ctname]) - } - else { + } else { dest.headers[ctname] = response.headers[ctname] } } @@ -1267,7 +1257,7 @@ Request.prototype.form = function (form) { } // create form-data object self._form = new FormData() - self._form.on('error', function(err) { + self._form.on('error', function (err) { err.message = 'form-data: ' + err.message self.emit('error', err) self.abort() @@ -1342,8 +1332,8 @@ Request.prototype.getHeader = function (name, headers) { Request.prototype.enableUnixSocket = function () { // Get the socket & request paths from the URL var unixParts = this.uri.path.split(':') - , host = unixParts[0] - , path = unixParts[1] + var host = unixParts[0] + var path = unixParts[1] // Apply unix properties to request this.socketPath = host this.uri.pathname = path @@ -1353,7 +1343,6 @@ Request.prototype.enableUnixSocket = function () { this.uri.isUnix = true } - Request.prototype.auth = function (user, pass, sendImmediately, bearer) { var self = this @@ -1369,7 +1358,7 @@ Request.prototype.aws = function (opts, now) { return self } - if (opts.sign_version == 4 || opts.sign_version == '4') { + if (opts.sign_version === 4 || opts.sign_version === '4') { // use aws4 var options = { host: self.uri.host, @@ -1390,20 +1379,19 @@ Request.prototype.aws = function (opts, now) { if (signRes.headers['X-Amz-Security-Token']) { self.setHeader('x-amz-security-token', signRes.headers['X-Amz-Security-Token']) } - } - else { + } else { // default: use aws-sign2 var date = new Date() self.setHeader('date', date.toUTCString()) - var auth = - { key: opts.key - , secret: opts.secret - , verb: self.method.toUpperCase() - , date: date - , contentType: self.getHeader('content-type') || '' - , md5: self.getHeader('content-md5') || '' - , amazonHeaders: aws2.canonicalizeHeaders(self.headers) - } + var auth = { + key: opts.key, + secret: opts.secret, + verb: self.method.toUpperCase(), + date: date, + contentType: self.getHeader('content-type') || '', + md5: self.getHeader('content-md5') || '', + amazonHeaders: aws2.canonicalizeHeaders(self.headers) + } var path = self.uri.path if (opts.bucket && path) { auth.resource = '/' + opts.bucket + path @@ -1423,10 +1411,10 @@ Request.prototype.aws = function (opts, now) { Request.prototype.httpSignature = function (opts) { var self = this httpSignature.signRequest({ - getHeader: function(header) { + getHeader: function (header) { return self.getHeader(header, self.headers) }, - setHeader: function(header, value) { + setHeader: function (header, value) { self.setHeader(header, value) }, method: self.method, @@ -1463,13 +1451,13 @@ Request.prototype.jar = function (jar) { } else { var targetCookieJar = (jar && jar.getCookieString) ? jar : globalCookieJar var urihref = self.uri.href - //fetch cookie in the Specified host + // fetch cookie in the Specified host if (targetCookieJar) { cookies = targetCookieJar.getCookieString(urihref) } } - //if need cookie and cookie is not empty + // if need cookie and cookie is not empty if (cookies && cookies.length) { if (self.originalCookieHeader) { // Don't overwrite existing Cookie header @@ -1482,7 +1470,6 @@ Request.prototype.jar = function (jar) { return self } - // Stream API Request.prototype.pipe = function (dest, opts) { var self = this @@ -1505,7 +1492,7 @@ Request.prototype.pipe = function (dest, opts) { } Request.prototype.write = function () { var self = this - if (self._aborted) {return} + if (self._aborted) { return } if (!self._started) { self.start() @@ -1516,7 +1503,7 @@ Request.prototype.write = function () { } Request.prototype.end = function (chunk) { var self = this - if (self._aborted) {return} + if (self._aborted) { return } if (chunk) { self.write(chunk) diff --git a/tests/browser/karma.conf.js b/tests/browser/karma.conf.js index 4c9172471..5fbf31a45 100644 --- a/tests/browser/karma.conf.js +++ b/tests/browser/karma.conf.js @@ -1,7 +1,7 @@ 'use strict' var istanbul = require('browserify-istanbul') -module.exports = function(config) { +module.exports = function (config) { config.set({ client: { requestTestUrl: process.argv[4] }, basePath: '../..', diff --git a/tests/browser/start.js b/tests/browser/start.js index 8ada016f8..3ce0c6a81 100644 --- a/tests/browser/start.js +++ b/tests/browser/start.js @@ -16,7 +16,7 @@ var server = https.createServer({ res.writeHead(200) res.end('Can you hear the sound of an enormous door slamming in the depths of hell?\n') }) -server.listen(0, function() { +server.listen(0, function () { var port = this.address().port console.log('Started https server for karma tests on port ' + port) // Spawn process for karma. @@ -27,7 +27,7 @@ server.listen(0, function() { ]) c.stdout.pipe(process.stdout) c.stderr.pipe(process.stderr) - c.on('exit', function(c) { + c.on('exit', function (c) { // Exit process with karma exit code. if (c !== 0) { throw new Error('Karma exited with status code ' + c) diff --git a/tests/browser/test.js b/tests/browser/test.js index 6ee8f7815..34135a398 100644 --- a/tests/browser/test.js +++ b/tests/browser/test.js @@ -4,15 +4,14 @@ if (!Function.prototype.bind) { // This is because of the fact that phantom.js does not have Function.bind. // This is a bug in phantom.js. // More info: https://github.com/ariya/phantomjs/issues/10522 - /*eslint no-extend-native:0*/ + /* eslint no-extend-native:0 */ Function.prototype.bind = require('function-bind') } - var tape = require('tape') - , request = require('../../index') +var request = require('../../index') -tape('returns on error', function(t) { +tape('returns on error', function (t) { t.plan(1) request({ uri: 'https://stupid.nonexistent.path:port123/\\<-great-idea', @@ -23,12 +22,12 @@ tape('returns on error', function(t) { }) }) -tape('succeeds on valid URLs (with https and CORS)', function(t) { +tape('succeeds on valid URLs (with https and CORS)', function (t) { t.plan(1) request({ - uri: __karma__.config.requestTestUrl, + uri: __karma__.config.requestTestUrl, // eslint-disable-line no-undef withCredentials: false - }, function (error, response) { + }, function (_, response) { t.equal(response.statusCode, 200) t.end() }) diff --git a/tests/server.js b/tests/server.js index 10a667bc1..614f03c44 100644 --- a/tests/server.js +++ b/tests/server.js @@ -1,11 +1,11 @@ 'use strict' var fs = require('fs') - , http = require('http') - , path = require('path') - , https = require('https') - , stream = require('stream') - , assert = require('assert') +var http = require('http') +var path = require('path') +var https = require('https') +var stream = require('stream') +var assert = require('assert') exports.createServer = function () { var s = http.createServer(function (req, resp) { @@ -23,9 +23,9 @@ exports.createServer = function () { exports.createEchoServer = function () { var s = http.createServer(function (req, resp) { var b = '' - req.on('data', function (chunk) {b += chunk}) + req.on('data', function (chunk) { b += chunk }) req.on('end', function () { - resp.writeHead(200, {'content-type':'application/json'}) + resp.writeHead(200, {'content-type': 'application/json'}) resp.write(JSON.stringify({ url: req.url, method: req.method, @@ -44,11 +44,9 @@ exports.createEchoServer = function () { return s } -exports.createSSLServer = function(opts) { +exports.createSSLServer = function (opts) { var i - , options = { 'key' : path.join(__dirname, 'ssl', 'test.key') - , 'cert': path.join(__dirname, 'ssl', 'test.crt') - } + var options = { 'key': path.join(__dirname, 'ssl', 'test.key'), 'cert': path.join(__dirname, 'ssl', 'test.crt') } if (opts) { for (i in opts) { options[i] = opts[i] @@ -77,7 +75,7 @@ exports.createPostStream = function (text) { var postStream = new stream.Stream() postStream.writeable = true postStream.readable = true - setTimeout(function() { + setTimeout(function () { postStream.emit('data', new Buffer(text)) postStream.emit('end') }, 0) @@ -86,7 +84,7 @@ exports.createPostStream = function (text) { exports.createPostValidator = function (text, reqContentType) { var l = function (req, resp) { var r = '' - req.on('data', function (chunk) {r += chunk}) + req.on('data', function (chunk) { r += chunk }) req.on('end', function () { if (req.headers['content-type'] && req.headers['content-type'].indexOf('boundary=') >= 0) { var boundary = req.headers['content-type'].split('boundary=')[1] @@ -97,7 +95,7 @@ exports.createPostValidator = function (text, reqContentType) { assert.ok(req.headers['content-type']) assert.ok(~req.headers['content-type'].indexOf(reqContentType)) } - resp.writeHead(200, {'content-type':'text/plain'}) + resp.writeHead(200, {'content-type': 'text/plain'}) resp.write(r) resp.end() }) @@ -107,7 +105,7 @@ exports.createPostValidator = function (text, reqContentType) { exports.createPostJSONValidator = function (value, reqContentType) { var l = function (req, resp) { var r = '' - req.on('data', function (chunk) {r += chunk}) + req.on('data', function (chunk) { r += chunk }) req.on('end', function () { var parsedValue = JSON.parse(r) assert.deepEqual(parsedValue, value) @@ -115,7 +113,7 @@ exports.createPostJSONValidator = function (value, reqContentType) { assert.ok(req.headers['content-type']) assert.ok(~req.headers['content-type'].indexOf(reqContentType)) } - resp.writeHead(200, {'content-type':'application/json'}) + resp.writeHead(200, {'content-type': 'application/json'}) resp.write(r) resp.end() }) @@ -125,7 +123,7 @@ exports.createPostJSONValidator = function (value, reqContentType) { exports.createGetResponse = function (text, contentType) { var l = function (req, resp) { contentType = contentType || 'text/plain' - resp.writeHead(200, {'content-type':contentType}) + resp.writeHead(200, {'content-type': contentType}) resp.write(text) resp.end() } @@ -134,7 +132,7 @@ exports.createGetResponse = function (text, contentType) { exports.createChunkResponse = function (chunks, contentType) { var l = function (req, resp) { contentType = contentType || 'text/plain' - resp.writeHead(200, {'content-type':contentType}) + resp.writeHead(200, {'content-type': contentType}) chunks.forEach(function (chunk) { resp.write(chunk) }) diff --git a/tests/ssl/ca/localhost.js b/tests/ssl/ca/localhost.js index 8ba3358c6..567bdb8e6 100644 --- a/tests/ssl/ca/localhost.js +++ b/tests/ssl/ca/localhost.js @@ -2,15 +2,15 @@ var fs = require('fs') var https = require('https') -var options = { key: fs.readFileSync('./localhost.key') - , cert: fs.readFileSync('./localhost.crt') } +var options = { key: fs.readFileSync('./localhost.key'), + cert: fs.readFileSync('./localhost.crt') } var server = https.createServer(options, function (req, res) { res.writeHead(200) res.end() server.close() }) -server.listen(0, function() { +server.listen(0, function () { var ca = fs.readFileSync('./ca.crt') var agent = new https.Agent({ host: 'localhost', @@ -18,12 +18,12 @@ server.listen(0, function() { ca: ca }) - https.request({ host: 'localhost' - , method: 'HEAD' - , port: this.address().port - , agent: agent - , ca: [ ca ] - , path: '/' }, function (res) { + https.request({ host: 'localhost', + method: 'HEAD', + port: this.address().port, + agent: agent, + ca: [ ca ], + path: '/' }, function (res) { if (res.socket.authorized) { console.log('node test: OK') } else { diff --git a/tests/ssl/ca/server.js b/tests/ssl/ca/server.js index 934554540..ca2a00e77 100644 --- a/tests/ssl/ca/server.js +++ b/tests/ssl/ca/server.js @@ -2,15 +2,15 @@ var fs = require('fs') var https = require('https') -var options = { key: fs.readFileSync('./server.key') - , cert: fs.readFileSync('./server.crt') } +var options = { key: fs.readFileSync('./server.key'), + cert: fs.readFileSync('./server.crt') } var server = https.createServer(options, function (req, res) { res.writeHead(200) res.end() server.close() }) -server.listen(0, function() { +server.listen(0, function () { var ca = fs.readFileSync('./ca.crt') var agent = new https.Agent({ host: 'localhost', @@ -18,13 +18,13 @@ server.listen(0, function() { ca: ca }) - https.request({ host: 'localhost' - , method: 'HEAD' - , port: this.address().port - , headers: { host: 'testing.request.mikealrogers.com' } - , agent: agent - , ca: [ ca ] - , path: '/' }, function (res) { + https.request({ host: 'localhost', + method: 'HEAD', + port: this.address().port, + headers: { host: 'testing.request.mikealrogers.com' }, + agent: agent, + ca: [ ca ], + path: '/' }, function (res) { if (res.socket.authorized) { console.log('node test: OK') } else { diff --git a/tests/test-agent.js b/tests/test-agent.js index 3bebf446d..40cdac05f 100644 --- a/tests/test-agent.js +++ b/tests/test-agent.js @@ -1,10 +1,10 @@ 'use strict' var request = require('../index') - , version = require('../lib/helpers').version - , http = require('http') - , ForeverAgent = require('forever-agent') - , tape = require('tape') +var version = require('../lib/helpers').version +var http = require('http') +var ForeverAgent = require('forever-agent') +var tape = require('tape') var s = http.createServer(function (req, res) { res.statusCode = 200 @@ -12,7 +12,7 @@ var s = http.createServer(function (req, res) { }) tape('setup', function (t) { - s.listen(0, function() { + s.listen(0, function () { s.port = this.address().port s.url = 'http://localhost:' + s.port t.end() @@ -21,13 +21,12 @@ tape('setup', function (t) { function httpAgent (t, options, req) { var r = (req || request)(options, function (_err, res, body) { - t.ok(r.agent instanceof http.Agent, 'is http.Agent') t.equal(r.agent.options.keepAlive, true, 'is keepAlive') t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name') var name = (typeof r.agent.getName === 'function') - ? r.agent.getName({port:s.port}) + ? r.agent.getName({port: s.port}) : 'localhost:' + s.port // node 0.10- t.equal(r.agent.sockets[name].length, 1, '1 open socket') @@ -42,7 +41,6 @@ function httpAgent (t, options, req) { function foreverAgent (t, options, req) { var r = (req || request)(options, function (_err, res, body) { - t.ok(r.agent instanceof ForeverAgent, 'is ForeverAgent') t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name') @@ -84,8 +82,7 @@ tape('options.forever = true', function (t) { forever: true } - if (v.major === 0 && v.minor <= 10) {foreverAgent(t, options)} - else {httpAgent(t, options)} + if (v.major === 0 && v.minor <= 10) { foreverAgent(t, options) } else { httpAgent(t, options) } }) tape('forever() method', function (t) { @@ -95,12 +92,11 @@ tape('forever() method', function (t) { } var r = request.forever({maxSockets: 1}) - if (v.major === 0 && v.minor <= 10) {foreverAgent(t, options, r)} - else {httpAgent(t, options, r)} + if (v.major === 0 && v.minor <= 10) { foreverAgent(t, options, r) } else { httpAgent(t, options, r) } }) tape('cleanup', function (t) { - s.close(function() { + s.close(function () { t.end() }) }) diff --git a/tests/test-agentOptions.js b/tests/test-agentOptions.js index 28a997c10..4682cbbba 100644 --- a/tests/test-agentOptions.js +++ b/tests/test-agentOptions.js @@ -1,53 +1,51 @@ 'use strict' -if (process.env.running_under_istanbul) { - // test-agent.js modifies the process state - // causing these tests to fail when running under single process via tape - return -} - -var request = require('../index') - , http = require('http') - , server = require('./server') - , tape = require('tape') - -var s = server.createServer() - -s.on('/', function (req, resp) { - resp.statusCode = 200 - resp.end('') -}) +// test-agent.js modifies the process state +// causing these tests to fail when running under single process via tape +if (!process.env.running_under_istanbul) { + var request = require('../index') + var http = require('http') + var server = require('./server') + var tape = require('tape') + + var s = server.createServer() + + s.on('/', function (req, resp) { + resp.statusCode = 200 + resp.end('') + }) -tape('setup', function(t) { - s.listen(0, function() { - t.end() + tape('setup', function (t) { + s.listen(0, function () { + t.end() + }) }) -}) -tape('without agentOptions should use global agent', function(t) { - var r = request(s.url, function(err, res, body) { - t.equal(err, null) - t.equal(res.statusCode, 200) - t.deepEqual(r.agent, http.globalAgent) - t.equal(Object.keys(r.pool).length, 0) - t.end() + tape('without agentOptions should use global agent', function (t) { + var r = request(s.url, function (err, res, body) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.deepEqual(r.agent, http.globalAgent) + t.equal(Object.keys(r.pool).length, 0) + t.end() + }) }) -}) -tape('with agentOptions should apply to new agent in pool', function(t) { - var r = request(s.url, { - agentOptions: { foo: 'bar' } - }, function(err, res, body) { - t.equal(err, null) - t.equal(res.statusCode, 200) - t.equal(r.agent.options.foo, 'bar') - t.equal(Object.keys(r.pool).length, 1) - t.end() + tape('with agentOptions should apply to new agent in pool', function (t) { + var r = request(s.url, { + agentOptions: { foo: 'bar' } + }, function (err, res, body) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.equal(r.agent.options.foo, 'bar') + t.equal(Object.keys(r.pool).length, 1) + t.end() + }) }) -}) -tape('cleanup', function(t) { - s.close(function() { - t.end() + tape('cleanup', function (t) { + s.close(function () { + t.end() + }) }) -}) +} diff --git a/tests/test-api.js b/tests/test-api.js index 9471c5fc5..3aa12fdc3 100644 --- a/tests/test-api.js +++ b/tests/test-api.js @@ -1,10 +1,9 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - , server - +var request = require('../index') +var tape = require('tape') +var server tape('setup', function (t) { server = http.createServer() @@ -12,7 +11,7 @@ tape('setup', function (t) { res.writeHead(202) req.pipe(res) }) - server.listen(0, function() { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) @@ -22,12 +21,13 @@ tape('callback option', function (t) { request({ url: server.url, callback: function (err, res, body) { + t.error(err) t.equal(res.statusCode, 202) t.end() } }) }) -tape('cleanup', function(t) { +tape('cleanup', function (t) { server.close(t.end) }) diff --git a/tests/test-aws.js b/tests/test-aws.js index af854c8cb..214003974 100644 --- a/tests/test-aws.js +++ b/tests/test-aws.js @@ -1,27 +1,27 @@ 'use strict' -var request = require('../index') - , server = require('./server') - , tape = require('tape') +var request = require('../index') +var server = require('./server') +var tape = require('tape') var s = server.createServer() var path = '/aws.json' -s.on(path, function(req, res) { +s.on(path, function (req, res) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify(req.headers)) }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -tape('default behaviour: aws-sign2 without sign_version key', function(t) { +tape('default behaviour: aws-sign2 without sign_version key', function (t) { var options = { url: s.url + path, aws: { @@ -30,14 +30,15 @@ tape('default behaviour: aws-sign2 without sign_version key', function(t) { }, json: true } - request(options, function(err, res, body) { + request(options, function (err, res, body) { + t.error(err) t.ok(body.authorization) t.notOk(body['x-amz-date']) t.end() }) }) -tape('aws-sign4 options', function(t) { +tape('aws-sign4 options', function (t) { var options = { url: s.url + path, aws: { @@ -47,7 +48,8 @@ tape('aws-sign4 options', function(t) { }, json: true } - request(options, function(err, res, body) { + request(options, function (err, res, body) { + t.error(err) t.ok(body.authorization) t.ok(body['x-amz-date']) t.notok(body['x-amz-security-token']) @@ -55,7 +57,7 @@ tape('aws-sign4 options', function(t) { }) }) -tape('aws-sign4 options with session token', function(t) { +tape('aws-sign4 options with session token', function (t) { var options = { url: s.url + path, aws: { @@ -66,7 +68,8 @@ tape('aws-sign4 options with session token', function(t) { }, json: true } - request(options, function(err, res, body) { + request(options, function (err, res, body) { + t.error(err) t.ok(body.authorization) t.ok(body['x-amz-date']) t.ok(body['x-amz-security-token']) @@ -74,8 +77,8 @@ tape('aws-sign4 options with session token', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-baseUrl.js b/tests/test-baseUrl.js index 7b5d034ea..a9a5e1378 100644 --- a/tests/test-baseUrl.js +++ b/tests/test-baseUrl.js @@ -1,14 +1,14 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - , url = require('url') +var request = require('../index') +var tape = require('tape') +var url = require('url') -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { if (req.url === '/redirect/') { res.writeHead(302, { - location : '/' + location: '/' }) } else { res.statusCode = 200 @@ -17,9 +17,9 @@ var s = http.createServer(function(req, res) { res.end('ok') }) -function addTest(baseUrl, uri, expected) { - tape('test baseurl="' + baseUrl + '" uri="' + uri + '"', function(t) { - request(uri, { baseUrl: baseUrl }, function(err, resp, body) { +function addTest (baseUrl, uri, expected) { + tape('test baseurl="' + baseUrl + '" uri="' + uri + '"', function (t) { + request(uri, { baseUrl: baseUrl }, function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.equal(resp.headers['x-path'], expected) @@ -28,7 +28,7 @@ function addTest(baseUrl, uri, expected) { }) } -function addTests() { +function addTests () { addTest(s.url, '', '/') addTest(s.url + '/', '', '/') addTest(s.url, '/', '/') @@ -47,12 +47,12 @@ function addTests() { addTest(s.url + '/api/', '/resource/', '/api/resource/') } -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port addTests() - tape('cleanup', function(t) { - s.close(function() { + tape('cleanup', function (t) { + s.close(function () { t.end() }) }) @@ -60,31 +60,31 @@ tape('setup', function(t) { }) }) -tape('baseUrl', function(t) { +tape('baseUrl', function (t) { request('resource', { baseUrl: s.url - }, function(err, resp, body) { + }, function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('baseUrl defaults', function(t) { +tape('baseUrl defaults', function (t) { var withDefaults = request.defaults({ baseUrl: s.url }) - withDefaults('resource', function(err, resp, body) { + withDefaults('resource', function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('baseUrl and redirects', function(t) { +tape('baseUrl and redirects', function (t) { request('/', { baseUrl: s.url + '/redirect' - }, function(err, resp, body) { + }, function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.equal(resp.headers['x-path'], '/') @@ -92,40 +92,40 @@ tape('baseUrl and redirects', function(t) { }) }) -tape('error when baseUrl is not a String', function(t) { +tape('error when baseUrl is not a String', function (t) { request('resource', { baseUrl: url.parse(s.url + '/path') - }, function(err, resp, body) { + }, function (err, resp, body) { t.notEqual(err, null) t.equal(err.message, 'options.baseUrl must be a string') t.end() }) }) -tape('error when uri is not a String', function(t) { +tape('error when uri is not a String', function (t) { request(url.parse('resource'), { baseUrl: s.url + '/path' - }, function(err, resp, body) { + }, function (err, resp, body) { t.notEqual(err, null) t.equal(err.message, 'options.uri must be a string when using options.baseUrl') t.end() }) }) -tape('error on baseUrl and uri with scheme', function(t) { +tape('error on baseUrl and uri with scheme', function (t) { request(s.url + '/path/ignoring/baseUrl', { baseUrl: s.url + '/path/' - }, function(err, resp, body) { + }, function (err, resp, body) { t.notEqual(err, null) t.equal(err.message, 'options.uri must be a path when using options.baseUrl') t.end() }) }) -tape('error on baseUrl and uri with scheme-relative url', function(t) { +tape('error on baseUrl and uri with scheme-relative url', function (t) { request(s.url.slice('http:'.length) + '/path/ignoring/baseUrl', { baseUrl: s.url + '/path/' - }, function(err, resp, body) { + }, function (err, resp, body) { t.notEqual(err, null) t.equal(err.message, 'options.uri must be a path when using options.baseUrl') t.end() diff --git a/tests/test-basic-auth.js b/tests/test-basic-auth.js index 983e3fc54..8c1a69a1c 100644 --- a/tests/test-basic-auth.js +++ b/tests/test-basic-auth.js @@ -1,14 +1,14 @@ 'use strict' var assert = require('assert') - , http = require('http') - , request = require('../index') - , tape = require('tape') +var http = require('http') +var request = require('../index') +var tape = require('tape') var numBasicRequests = 0 - , basicServer +var basicServer -tape('setup', function(t) { +tape('setup', function (t) { basicServer = http.createServer(function (req, res) { numBasicRequests++ @@ -17,11 +17,11 @@ tape('setup', function(t) { if (req.headers.authorization) { if (req.headers.authorization === 'Basic ' + new Buffer('user:pass').toString('base64')) { ok = true - } else if ( req.headers.authorization === 'Basic ' + new Buffer('user:').toString('base64')) { + } else if (req.headers.authorization === 'Basic ' + new Buffer('user:').toString('base64')) { ok = true - } else if ( req.headers.authorization === 'Basic ' + new Buffer(':pass').toString('base64')) { + } else if (req.headers.authorization === 'Basic ' + new Buffer(':pass').toString('base64')) { ok = true - } else if ( req.headers.authorization === 'Basic ' + new Buffer('user:pâss').toString('base64')) { + } else if (req.headers.authorization === 'Basic ' + new Buffer('user:pâss').toString('base64')) { ok = true } else { // Bad auth header, don't send back WWW-Authenticate header @@ -35,7 +35,7 @@ tape('setup', function(t) { if (req.url === '/post/') { var expectedContent = 'key=value' - req.on('data', function(data) { + req.on('data', function (data) { assert.equal(data, expectedContent) }) assert.equal(req.method, 'POST') @@ -49,14 +49,14 @@ tape('setup', function(t) { res.statusCode = 401 res.end('401') } - }).listen(0, function() { + }).listen(0, function () { basicServer.port = this.address().port basicServer.url = 'http://localhost:' + basicServer.port t.end() }) }) -tape('sendImmediately - false', function(t) { +tape('sendImmediately - false', function (t) { var r = request({ 'method': 'GET', 'uri': basicServer.url + '/test/', @@ -65,7 +65,8 @@ tape('sendImmediately - false', function(t) { 'pass': 'pass', 'sendImmediately': false } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 2) @@ -73,7 +74,7 @@ tape('sendImmediately - false', function(t) { }) }) -tape('sendImmediately - true', function(t) { +tape('sendImmediately - true', function (t) { // If we don't set sendImmediately = false, request will send basic auth var r = request({ 'method': 'GET', @@ -82,7 +83,8 @@ tape('sendImmediately - true', function(t) { 'user': 'user', 'pass': 'pass' } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 3) @@ -90,11 +92,12 @@ tape('sendImmediately - true', function(t) { }) }) -tape('credentials in url', function(t) { +tape('credentials in url', function (t) { var r = request({ 'method': 'GET', 'uri': basicServer.url.replace(/:\/\//, '$&user:pass@') + '/test2/' - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 4) @@ -102,7 +105,7 @@ tape('credentials in url', function(t) { }) }) -tape('POST request', function(t) { +tape('POST request', function (t) { var r = request({ 'method': 'POST', 'form': { 'key': 'value' }, @@ -112,7 +115,8 @@ tape('POST request', function(t) { 'pass': 'pass', 'sendImmediately': false } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 6) @@ -120,8 +124,8 @@ tape('POST request', function(t) { }) }) -tape('user - empty string', function(t) { - t.doesNotThrow( function() { +tape('user - empty string', function (t) { + t.doesNotThrow(function () { var r = request({ 'method': 'GET', 'uri': basicServer.url + '/allow_empty_user/', @@ -130,7 +134,8 @@ tape('user - empty string', function(t) { 'pass': 'pass', 'sendImmediately': false } - }, function(error, res, body ) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, '') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 8) @@ -139,8 +144,8 @@ tape('user - empty string', function(t) { }) }) -tape('pass - undefined', function(t) { - t.doesNotThrow( function() { +tape('pass - undefined', function (t) { + t.doesNotThrow(function () { var r = request({ 'method': 'GET', 'uri': basicServer.url + '/allow_undefined_password/', @@ -149,7 +154,8 @@ tape('pass - undefined', function(t) { 'pass': undefined, 'sendImmediately': false } - }, function(error, res, body ) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(res.statusCode, 200) t.equal(numBasicRequests, 10) @@ -158,9 +164,8 @@ tape('pass - undefined', function(t) { }) }) - -tape('pass - utf8', function(t) { - t.doesNotThrow( function() { +tape('pass - utf8', function (t) { + t.doesNotThrow(function () { var r = request({ 'method': 'GET', 'uri': basicServer.url + '/allow_undefined_password/', @@ -169,7 +174,8 @@ tape('pass - utf8', function(t) { 'pass': 'pâss', 'sendImmediately': false } - }, function(error, res, body ) { + }, function (error, res, body) { + t.error(error) t.equal(r._auth.user, 'user') t.equal(r._auth.pass, 'pâss') t.equal(res.statusCode, 200) @@ -179,7 +185,7 @@ tape('pass - utf8', function(t) { }) }) -tape('auth method', function(t) { +tape('auth method', function (t) { var r = request .get(basicServer.url + '/test/') .auth('user', '', false) @@ -191,7 +197,7 @@ tape('auth method', function(t) { }) }) -tape('get method', function(t) { +tape('get method', function (t) { var r = request.get(basicServer.url + '/test/', { auth: { @@ -208,8 +214,8 @@ tape('get method', function(t) { }) }) -tape('cleanup', function(t) { - basicServer.close(function() { +tape('cleanup', function (t) { + basicServer.close(function () { t.end() }) }) diff --git a/tests/test-bearer-auth.js b/tests/test-bearer-auth.js index 8519c6131..032ccc7ee 100644 --- a/tests/test-bearer-auth.js +++ b/tests/test-bearer-auth.js @@ -1,14 +1,14 @@ 'use strict' var assert = require('assert') - , http = require('http') - , request = require('../index') - , tape = require('tape') +var http = require('http') +var request = require('../index') +var tape = require('tape') var numBearerRequests = 0 - , bearerServer +var bearerServer -tape('setup', function(t) { +tape('setup', function (t) { bearerServer = http.createServer(function (req, res) { numBearerRequests++ @@ -29,7 +29,7 @@ tape('setup', function(t) { if (req.url === '/post/') { var expectedContent = 'data_key=data_value' - req.on('data', function(data) { + req.on('data', function (data) { assert.equal(data, expectedContent) }) assert.equal(req.method, 'POST') @@ -43,13 +43,13 @@ tape('setup', function(t) { res.statusCode = 401 res.end('401') } - }).listen(0, function() { + }).listen(0, function () { bearerServer.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('bearer auth', function(t) { +tape('bearer auth', function (t) { request({ 'method': 'GET', 'uri': bearerServer.url + '/test/', @@ -57,14 +57,15 @@ tape('bearer auth', function(t) { 'bearer': 'theToken', 'sendImmediately': false } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 200) t.equal(numBearerRequests, 2) t.end() }) }) -tape('bearer auth with default sendImmediately', function(t) { +tape('bearer auth with default sendImmediately', function (t) { // If we don't set sendImmediately = false, request will send bearer auth request({ 'method': 'GET', @@ -72,14 +73,15 @@ tape('bearer auth with default sendImmediately', function(t) { 'auth': { 'bearer': 'theToken' } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 200) t.equal(numBearerRequests, 3) t.end() }) }) -tape('', function(t) { +tape('', function (t) { request({ 'method': 'POST', 'form': { 'data_key': 'data_value' }, @@ -88,14 +90,15 @@ tape('', function(t) { 'bearer': 'theToken', 'sendImmediately': false } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 200) t.equal(numBearerRequests, 5) t.end() }) }) -tape('using .auth, sendImmediately = false', function(t) { +tape('using .auth, sendImmediately = false', function (t) { request .get(bearerServer.url + '/test/') .auth(null, null, false, 'theToken') @@ -106,7 +109,7 @@ tape('using .auth, sendImmediately = false', function(t) { }) }) -tape('using .auth, sendImmediately = true', function(t) { +tape('using .auth, sendImmediately = true', function (t) { request .get(bearerServer.url + '/test/') .auth(null, null, true, 'theToken') @@ -117,65 +120,68 @@ tape('using .auth, sendImmediately = true', function(t) { }) }) -tape('bearer is a function', function(t) { +tape('bearer is a function', function (t) { request({ 'method': 'GET', 'uri': bearerServer.url + '/test/', 'auth': { - 'bearer': function() { return 'theToken' }, + 'bearer': function () { return 'theToken' }, 'sendImmediately': false } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 200) t.equal(numBearerRequests, 10) t.end() }) }) -tape('bearer is a function, path = test2', function(t) { +tape('bearer is a function, path = test2', function (t) { // If we don't set sendImmediately = false, request will send bearer auth request({ 'method': 'GET', 'uri': bearerServer.url + '/test2/', 'auth': { - 'bearer': function() { return 'theToken' } + 'bearer': function () { return 'theToken' } } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 200) t.equal(numBearerRequests, 11) t.end() }) }) -tape('no auth method', function(t) { +tape('no auth method', function (t) { request({ 'method': 'GET', 'uri': bearerServer.url + '/test2/', 'auth': { 'bearer': undefined } - }, function(error, res, body) { + }, function (error, res, body) { t.equal(error.message, 'no auth mechanism defined') t.end() }) }) -tape('null bearer', function(t) { +tape('null bearer', function (t) { request({ 'method': 'GET', 'uri': bearerServer.url + '/test2/', 'auth': { 'bearer': null } - }, function(error, res, body) { + }, function (error, res, body) { + t.error(error) t.equal(res.statusCode, 401) t.equal(numBearerRequests, 13) t.end() }) }) -tape('cleanup', function(t) { - bearerServer.close(function() { +tape('cleanup', function (t) { + bearerServer.close(function () { t.end() }) }) diff --git a/tests/test-body.js b/tests/test-body.js index be2a67b59..2cbb494cf 100644 --- a/tests/test-body.js +++ b/tests/test-body.js @@ -1,20 +1,20 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') - , http = require('http') +var request = require('../index') +var tape = require('tape') +var http = require('http') var s = server.createServer() -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -function addTest(name, data) { - tape('test ' + name, function(t) { +function addTest (name, data) { + tape('test ' + name, function (t) { s.on('/' + name, data.resp) data.uri = s.url + '/' + name request(data, function (err, resp, body) { @@ -30,64 +30,49 @@ function addTest(name, data) { } addTest('testGet', { - resp : server.createGetResponse('TESTING!') - , expectBody: 'TESTING!' + resp: server.createGetResponse('TESTING!'), expectBody: 'TESTING!' }) addTest('testGetChunkBreak', { - resp : server.createChunkResponse( - [ new Buffer([239]) - , new Buffer([163]) - , new Buffer([191]) - , new Buffer([206]) - , new Buffer([169]) - , new Buffer([226]) - , new Buffer([152]) - , new Buffer([131]) - ]) - , expectBody: '\uF8FF\u03A9\u2603' + resp: server.createChunkResponse( + [ new Buffer([239]), + new Buffer([163]), + new Buffer([191]), + new Buffer([206]), + new Buffer([169]), + new Buffer([226]), + new Buffer([152]), + new Buffer([131]) + ]), + expectBody: '\uF8FF\u03A9\u2603' }) addTest('testGetBuffer', { - resp : server.createGetResponse(new Buffer('TESTING!')) - , encoding: null - , expectBody: new Buffer('TESTING!') + resp: server.createGetResponse(new Buffer('TESTING!')), encoding: null, expectBody: new Buffer('TESTING!') }) addTest('testGetEncoding', { - resp : server.createGetResponse(new Buffer('efa3bfcea9e29883', 'hex')) - , encoding: 'hex' - , expectBody: 'efa3bfcea9e29883' + resp: server.createGetResponse(new Buffer('efa3bfcea9e29883', 'hex')), encoding: 'hex', expectBody: 'efa3bfcea9e29883' }) addTest('testGetUTF', { - resp: server.createGetResponse(new Buffer([0xEF, 0xBB, 0xBF, 226, 152, 131])) - , encoding: 'utf8' - , expectBody: '\u2603' + resp: server.createGetResponse(new Buffer([0xEF, 0xBB, 0xBF, 226, 152, 131])), encoding: 'utf8', expectBody: '\u2603' }) addTest('testGetJSON', { - resp : server.createGetResponse('{"test":true}', 'application/json') - , json : true - , expectBody: {'test':true} + resp: server.createGetResponse('{"test":true}', 'application/json'), json: true, expectBody: {'test': true} }) addTest('testPutString', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : 'PUTTINGDATA' + resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: 'PUTTINGDATA' }) addTest('testPutBuffer', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : new Buffer('PUTTINGDATA') + resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: new Buffer('PUTTINGDATA') }) addTest('testPutJSON', { - resp : server.createPostValidator(JSON.stringify({foo: 'bar'})) - , method: 'PUT' - , json: {foo: 'bar'} + resp: server.createPostValidator(JSON.stringify({foo: 'bar'})), method: 'PUT', json: {foo: 'bar'} }) addTest('testPutMultipart', { @@ -99,12 +84,11 @@ addTest('testPutMultipart', { '\r\n--__BOUNDARY__\r\n\r\n' + 'Oh hi.' + '\r\n--__BOUNDARY__--' - ) - , method: 'PUT' - , multipart: - [ {'content-type': 'text/html', 'body': 'Oh hi.'} - , {'body': 'Oh hi.'} - ] + ), + method: 'PUT', + multipart: [ {'content-type': 'text/html', 'body': 'Oh hi.'}, + {'body': 'Oh hi.'} + ] }) addTest('testPutMultipartPreambleCRLF', { @@ -116,13 +100,12 @@ addTest('testPutMultipartPreambleCRLF', { '\r\n--__BOUNDARY__\r\n\r\n' + 'Oh hi.' + '\r\n--__BOUNDARY__--' - ) - , method: 'PUT' - , preambleCRLF: true - , multipart: - [ {'content-type': 'text/html', 'body': 'Oh hi.'} - , {'body': 'Oh hi.'} - ] + ), + method: 'PUT', + preambleCRLF: true, + multipart: [ {'content-type': 'text/html', 'body': 'Oh hi.'}, + {'body': 'Oh hi.'} + ] }) addTest('testPutMultipartPostambleCRLF', { @@ -135,14 +118,13 @@ addTest('testPutMultipartPostambleCRLF', { 'Oh hi.' + '\r\n--__BOUNDARY__--' + '\r\n' - ) - , method: 'PUT' - , preambleCRLF: true - , postambleCRLF: true - , multipart: - [ {'content-type': 'text/html', 'body': 'Oh hi.'} - , {'body': 'Oh hi.'} - ] + ), + method: 'PUT', + preambleCRLF: true, + postambleCRLF: true, + multipart: [ {'content-type': 'text/html', 'body': 'Oh hi.'}, + {'body': 'Oh hi.'} + ] }) tape('typed array', function (t) { @@ -158,14 +140,15 @@ tape('typed array', function (t) { body: data, encoding: null }, function (err, res, body) { + t.error(err) t.deepEqual(new Buffer(data), body) server.close(t.end) }) }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-cookies.js b/tests/test-cookies.js index 8a6065927..6bebcaf12 100644 --- a/tests/test-cookies.js +++ b/tests/test-cookies.js @@ -1,13 +1,12 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - +var request = require('../index') +var tape = require('tape') var validUrl - , malformedUrl - , invalidUrl +var malformedUrl +var invalidUrl var server = http.createServer(function (req, res) { if (req.url === '/valid') { @@ -20,8 +19,8 @@ var server = http.createServer(function (req, res) { res.end('okay') }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port validUrl = server.url + '/valid' malformedUrl = server.url + '/malformed' @@ -30,79 +29,79 @@ tape('setup', function(t) { }) }) -tape('simple cookie creation', function(t) { +tape('simple cookie creation', function (t) { var cookie = request.cookie('foo=bar') t.equals(cookie.key, 'foo') t.equals(cookie.value, 'bar') t.end() }) -tape('simple malformed cookie creation', function(t) { +tape('simple malformed cookie creation', function (t) { var cookie = request.cookie('foo') t.equals(cookie.key, '') t.equals(cookie.value, 'foo') t.end() }) -tape('after server sends a cookie', function(t) { +tape('after server sends a cookie', function (t) { var jar1 = request.jar() request({ method: 'GET', url: validUrl, jar: jar1 }, - function (error, response, body) { - t.equal(error, null) - t.equal(jar1.getCookieString(validUrl), 'foo=bar') - t.equal(body, 'okay') + function (error, response, body) { + t.equal(error, null) + t.equal(jar1.getCookieString(validUrl), 'foo=bar') + t.equal(body, 'okay') - var cookies = jar1.getCookies(validUrl) - t.equal(cookies.length, 1) - t.equal(cookies[0].key, 'foo') - t.equal(cookies[0].value, 'bar') - t.end() - }) + var cookies = jar1.getCookies(validUrl) + t.equal(cookies.length, 1) + t.equal(cookies[0].key, 'foo') + t.equal(cookies[0].value, 'bar') + t.end() + }) }) -tape('after server sends a malformed cookie', function(t) { +tape('after server sends a malformed cookie', function (t) { var jar = request.jar() request({ method: 'GET', url: malformedUrl, jar: jar }, - function (error, response, body) { - t.equal(error, null) - t.equal(jar.getCookieString(malformedUrl), 'foo') - t.equal(body, 'okay') + function (error, response, body) { + t.equal(error, null) + t.equal(jar.getCookieString(malformedUrl), 'foo') + t.equal(body, 'okay') - var cookies = jar.getCookies(malformedUrl) - t.equal(cookies.length, 1) - t.equal(cookies[0].key, '') - t.equal(cookies[0].value, 'foo') - t.end() - }) + var cookies = jar.getCookies(malformedUrl) + t.equal(cookies.length, 1) + t.equal(cookies[0].key, '') + t.equal(cookies[0].value, 'foo') + t.end() + }) }) -tape('after server sends a cookie for a different domain', function(t) { +tape('after server sends a cookie for a different domain', function (t) { var jar2 = request.jar() request({ method: 'GET', url: invalidUrl, jar: jar2 }, - function (error, response, body) { - t.equal(error, null) - t.equal(jar2.getCookieString(validUrl), '') - t.deepEqual(jar2.getCookies(validUrl), []) - t.equal(body, 'okay') - t.end() - }) + function (error, response, body) { + t.equal(error, null) + t.equal(jar2.getCookieString(validUrl), '') + t.deepEqual(jar2.getCookies(validUrl), []) + t.equal(body, 'okay') + t.end() + }) }) -tape('make sure setCookie works', function(t) { +tape('make sure setCookie works', function (t) { var jar3 = request.jar() - , err = null + var err = null try { jar3.setCookie(request.cookie('foo=bar'), validUrl) } catch (e) { @@ -116,16 +115,16 @@ tape('make sure setCookie works', function(t) { t.end() }) -tape('custom store', function(t) { - var Store = function() {} +tape('custom store', function (t) { + var Store = function () {} var store = new Store() var jar = request.jar(store) t.equals(store, jar._jar.store) t.end() }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-defaults.js b/tests/test-defaults.js index 0fc578e7b..f75f5d7bc 100644 --- a/tests/test-defaults.js +++ b/tests/test-defaults.js @@ -1,37 +1,38 @@ 'use strict' var server = require('./server') - , request = require('../index') - , qs = require('qs') - , tape = require('tape') +var request = require('../index') +var qs = require('qs') +var tape = require('tape') var s = server.createServer() -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.on('/', function (req, res) { res.writeHead(200, {'content-type': 'application/json'}) res.end(JSON.stringify({ - method: req.method, headers: req.headers, + method: req.method, + headers: req.headers, qs: qs.parse(req.url.replace(/.*\?(.*)/, '$1')) })) }) s.on('/head', function (req, res) { - res.writeHead(200, {'x-data': - JSON.stringify({method: req.method, headers: req.headers})}) + res.writeHead(200, {'x-data': JSON.stringify({method: req.method, headers: req.headers})}) res.end() }) s.on('/set-undefined', function (req, res) { var data = '' - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { res.writeHead(200, {'Content-Type': 'application/json'}) res.end(JSON.stringify({ - method: req.method, headers: req.headers, data: JSON.parse(data)})) + method: req.method, headers: req.headers, data: JSON.parse(data) + })) }) }) @@ -39,7 +40,7 @@ tape('setup', function(t) { }) }) -tape('get(string, function)', function(t) { +tape('get(string, function)', function (t) { request.defaults({ headers: { foo: 'bar' } })(s.url + '/', function (e, r, b) { @@ -50,7 +51,7 @@ tape('get(string, function)', function(t) { }) }) -tape('merge headers', function(t) { +tape('merge headers', function (t) { request.defaults({ headers: { foo: 'bar', merged: 'no' } })(s.url + '/', { @@ -62,9 +63,9 @@ tape('merge headers', function(t) { }) }) -tape('deep extend', function(t) { +tape('deep extend', function (t) { request.defaults({ - headers: {a: 1, b: 2 }, + headers: { a: 1, b: 2 }, qs: { a: 1, b: 2 } })(s.url + '/', { headers: { b: 3, c: 4 }, @@ -80,10 +81,10 @@ tape('deep extend', function(t) { }) }) -tape('default undefined header', function(t) { +tape('default undefined header', function (t) { request.defaults({ headers: { foo: 'bar', test: undefined }, json: true - })(s.url + '/', function(e, r, b) { + })(s.url + '/', function (e, r, b) { t.equal(b.method, 'GET') t.equal(b.headers.foo, 'bar') t.equal(b.headers.test, undefined) @@ -91,7 +92,7 @@ tape('default undefined header', function(t) { }) }) -tape('post(string, object, function)', function(t) { +tape('post(string, object, function)', function (t) { request.defaults({ headers: { foo: 'bar' } }).post(s.url + '/', { json: true }, function (e, r, b) { @@ -102,7 +103,7 @@ tape('post(string, object, function)', function(t) { }) }) -tape('patch(string, object, function)', function(t) { +tape('patch(string, object, function)', function (t) { request.defaults({ headers: { foo: 'bar' } }).patch(s.url + '/', { json: true }, function (e, r, b) { @@ -113,7 +114,7 @@ tape('patch(string, object, function)', function(t) { }) }) -tape('post(string, object, function) with body', function(t) { +tape('post(string, object, function) with body', function (t) { request.defaults({ headers: { foo: 'bar' } }).post(s.url + '/', { @@ -127,7 +128,7 @@ tape('post(string, object, function) with body', function(t) { }) }) -tape('del(string, function)', function(t) { +tape('del(string, function)', function (t) { request.defaults({ headers: {foo: 'bar'}, json: true @@ -138,7 +139,7 @@ tape('del(string, function)', function(t) { }) }) -tape('delete(string, function)', function(t) { +tape('delete(string, function)', function (t) { request.defaults({ headers: {foo: 'bar'}, json: true @@ -149,7 +150,7 @@ tape('delete(string, function)', function(t) { }) }) -tape('head(object, function)', function(t) { +tape('head(object, function)', function (t) { request.defaults({ headers: { foo: 'bar' } }).head({ uri: s.url + '/head' }, function (e, r, b) { @@ -160,17 +161,17 @@ tape('head(object, function)', function(t) { }) }) -tape('recursive defaults', function(t) { +tape('recursive defaults', function (t) { t.plan(11) var defaultsOne = request.defaults({ headers: { foo: 'bar1' } }) - , defaultsTwo = defaultsOne.defaults({ headers: { baz: 'bar2' } }) - , defaultsThree = defaultsTwo.defaults({}, function(options, callback) { - options.headers = { - foo: 'bar3' - } - defaultsTwo(options, callback) - }) + var defaultsTwo = defaultsOne.defaults({ headers: { baz: 'bar2' } }) + var defaultsThree = defaultsTwo.defaults({}, function (options, callback) { + options.headers = { + foo: 'bar3' + } + defaultsTwo(options, callback) + }) defaultsOne(s.url + '/', {json: true}, function (e, r, b) { t.equal(b.method, 'GET') @@ -197,23 +198,24 @@ tape('recursive defaults', function(t) { }) }) -tape('recursive defaults requester', function(t) { +tape('recursive defaults requester', function (t) { t.plan(5) - var defaultsOne = request.defaults({}, function(options, callback) { - var headers = options.headers || {} - headers.foo = 'bar1' - options.headers = headers + var defaultsOne = request.defaults({}, function (options, callback) { + var headers = options.headers || {} + headers.foo = 'bar1' + options.headers = headers - request(options, callback) - }) - , defaultsTwo = defaultsOne.defaults({}, function(options, callback) { - var headers = options.headers || {} - headers.baz = 'bar2' - options.headers = headers + request(options, callback) + }) - defaultsOne(options, callback) - }) + var defaultsTwo = defaultsOne.defaults({}, function (options, callback) { + var headers = options.headers || {} + headers.baz = 'bar2' + options.headers = headers + + defaultsOne(options, callback) + }) defaultsOne.get(s.url + '/', {json: true}, function (e, r, b) { t.equal(b.method, 'GET') @@ -227,35 +229,35 @@ tape('recursive defaults requester', function(t) { }) }) -tape('test custom request handler function', function(t) { +tape('test custom request handler function', function (t) { t.plan(3) var requestWithCustomHandler = request.defaults({ headers: { foo: 'bar' }, body: 'TESTING!' - }, function(uri, options, callback) { + }, function (uri, options, callback) { var params = request.initParams(uri, options, callback) params.headers.x = 'y' return request(params.uri, params, params.callback) }) - t.throws(function() { - requestWithCustomHandler.head(s.url + '/', function(e, r, b) { + t.throws(function () { + requestWithCustomHandler.head(s.url + '/', function (e, r, b) { throw new Error('We should never get here') }) }, /HTTP HEAD requests MUST NOT include a request body/) - requestWithCustomHandler.get(s.url + '/', function(e, r, b) { + requestWithCustomHandler.get(s.url + '/', function (e, r, b) { b = JSON.parse(b) t.equal(b.headers.foo, 'bar') t.equal(b.headers.x, 'y') }) }) -tape('test custom request handler function without options', function(t) { +tape('test custom request handler function without options', function (t) { t.plan(2) - var customHandlerWithoutOptions = request.defaults(function(uri, options, callback) { + var customHandlerWithoutOptions = request.defaults(function (uri, options, callback) { var params = request.initParams(uri, options, callback) var headers = params.headers || {} headers.x = 'y' @@ -264,14 +266,14 @@ tape('test custom request handler function without options', function(t) { return request(params.uri, params, params.callback) }) - customHandlerWithoutOptions.get(s.url + '/', function(e, r, b) { + customHandlerWithoutOptions.get(s.url + '/', function (e, r, b) { b = JSON.parse(b) t.equal(b.headers.foo, 'bar') t.equal(b.headers.x, 'y') }) }) -tape('test only setting undefined properties', function(t) { +tape('test only setting undefined properties', function (t) { request.defaults({ method: 'post', json: true, @@ -289,7 +291,7 @@ tape('test only setting undefined properties', function(t) { }) }) -tape('test only function', function(t) { +tape('test only function', function (t) { var post = request.post t.doesNotThrow(function () { post(s.url + '/', function (e, r, b) { @@ -299,7 +301,7 @@ tape('test only function', function(t) { }) }) -tape('invoke defaults', function(t) { +tape('invoke defaults', function (t) { var d = request.defaults({ uri: s.url + '/', headers: { foo: 'bar' } @@ -311,7 +313,7 @@ tape('invoke defaults', function(t) { }) }) -tape('invoke convenience method from defaults', function(t) { +tape('invoke convenience method from defaults', function (t) { var d = request.defaults({ uri: s.url + '/', headers: { foo: 'bar' } @@ -323,7 +325,7 @@ tape('invoke convenience method from defaults', function(t) { }) }) -tape('defaults without options', function(t) { +tape('defaults without options', function (t) { var d = request.defaults() d.get(s.url + '/', {json: true}, function (e, r, b) { t.equal(r.statusCode, 200) @@ -331,8 +333,8 @@ tape('defaults without options', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-digest-auth.js b/tests/test-digest-auth.js index b5801ca61..d5c3c0ee5 100644 --- a/tests/test-digest-auth.js +++ b/tests/test-digest-auth.js @@ -1,15 +1,15 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - , crypto = require('crypto') +var request = require('../index') +var tape = require('tape') +var crypto = require('crypto') -function makeHeader() { +function makeHeader () { return [].join.call(arguments, ', ') } -function makeHeaderRegex() { +function makeHeaderRegex () { return new RegExp('^' + makeHeader.apply(null, arguments) + '$') } @@ -17,23 +17,23 @@ function md5 (str) { return crypto.createHash('md5').update(str).digest('hex') } -var digestServer = http.createServer(function(req, res) { - var ok - , testHeader +var digestServer = http.createServer(function (req, res) { + var ok, + testHeader if (req.url === '/test/') { if (req.headers.authorization) { testHeader = makeHeaderRegex( - 'Digest username="test"', - 'realm="Private"', - 'nonce="WpcHS2/TBAA=dffcc0dbd5f96d49a5477166649b7c0ae3866a93"', - 'uri="/test/"', - 'qop=auth', - 'response="[a-f0-9]{32}"', - 'nc=00000001', - 'cnonce="[a-f0-9]{32}"', - 'algorithm=MD5', - 'opaque="5ccc069c403ebaf9f0171e9517f40e41"' + 'Digest username="test"', + 'realm="Private"', + 'nonce="WpcHS2/TBAA=dffcc0dbd5f96d49a5477166649b7c0ae3866a93"', + 'uri="/test/"', + 'qop=auth', + 'response="[a-f0-9]{32}"', + 'nc=00000001', + 'cnonce="[a-f0-9]{32}"', + 'algorithm=MD5', + 'opaque="5ccc069c403ebaf9f0171e9517f40e41"' ) if (testHeader.test(req.headers.authorization)) { ok = true @@ -45,11 +45,11 @@ var digestServer = http.createServer(function(req, res) { // No auth header, send back WWW-Authenticate header ok = false res.setHeader('www-authenticate', makeHeader( - 'Digest realm="Private"', - 'nonce="WpcHS2/TBAA=dffcc0dbd5f96d49a5477166649b7c0ae3866a93"', - 'algorithm=MD5', - 'qop="auth"', - 'opaque="5ccc069c403ebaf9f0171e9517f40e41"' + 'Digest realm="Private"', + 'nonce="WpcHS2/TBAA=dffcc0dbd5f96d49a5477166649b7c0ae3866a93"', + 'algorithm=MD5', + 'qop="auth"', + 'opaque="5ccc069c403ebaf9f0171e9517f40e41"' )) } } else if (req.url === '/test/md5-sess') { // RFC 2716 MD5-sess w/ qop=auth @@ -61,10 +61,9 @@ var digestServer = http.createServer(function(req, res) { var qop = 'auth' var algorithm = 'MD5-sess' if (req.headers.authorization) { - - //HA1=MD5(MD5(username:realm:password):nonce:cnonce) - //HA2=MD5(method:digestURI) - //response=MD5(HA1:nonce:nonceCount:clientNonce:qop:HA2) + // HA1=MD5(MD5(username:realm:password):nonce:cnonce) + // HA2=MD5(method:digestURI) + // response=MD5(HA1:nonce:nonceCount:clientNonce:qop:HA2) var cnonce = /cnonce="(.*)"/.exec(req.headers.authorization)[1] var ha1 = md5(md5(user + ':' + realm + ':' + pass) + ':' + nonce + ':' + cnonce) @@ -72,15 +71,15 @@ var digestServer = http.createServer(function(req, res) { var response = md5(ha1 + ':' + nonce + ':' + nonceCount + ':' + cnonce + ':' + qop + ':' + ha2) testHeader = makeHeaderRegex( - 'Digest username="' + user + '"', - 'realm="' + realm + '"', - 'nonce="' + nonce + '"', - 'uri="/test/md5-sess"', - 'qop=' + qop, - 'response="' + response + '"', - 'nc=' + nonceCount, - 'cnonce="' + cnonce + '"', - 'algorithm=' + algorithm + 'Digest username="' + user + '"', + 'realm="' + realm + '"', + 'nonce="' + nonce + '"', + 'uri="/test/md5-sess"', + 'qop=' + qop, + 'response="' + response + '"', + 'nc=' + nonceCount, + 'cnonce="' + cnonce + '"', + 'algorithm=' + algorithm ) ok = testHeader.test(req.headers.authorization) @@ -88,10 +87,10 @@ var digestServer = http.createServer(function(req, res) { // No auth header, send back WWW-Authenticate header ok = false res.setHeader('www-authenticate', makeHeader( - 'Digest realm="' + realm + '"', - 'nonce="' + nonce + '"', - 'algorithm=' + algorithm, - 'qop="' + qop + '"' + 'Digest realm="' + realm + '"', + 'nonce="' + nonce + '"', + 'algorithm=' + algorithm, + 'qop="' + qop + '"' )) } } else if (req.url === '/dir/index.html') { @@ -131,14 +130,14 @@ var digestServer = http.createServer(function(req, res) { } }) -tape('setup', function(t) { - digestServer.listen(0, function() { +tape('setup', function (t) { + digestServer.listen(0, function () { digestServer.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('with sendImmediately = false', function(t) { +tape('with sendImmediately = false', function (t) { var numRedirects = 0 request({ @@ -149,18 +148,18 @@ tape('with sendImmediately = false', function(t) { pass: 'testing', sendImmediately: false } - }, function(error, response, body) { + }, function (error, response, body) { t.equal(error, null) t.equal(response.statusCode, 200) t.equal(numRedirects, 1) t.end() - }).on('redirect', function() { + }).on('redirect', function () { t.equal(this.response.statusCode, 401) numRedirects++ }) }) -tape('with MD5-sess algorithm', function(t) { +tape('with MD5-sess algorithm', function (t) { var numRedirects = 0 request({ @@ -171,18 +170,18 @@ tape('with MD5-sess algorithm', function(t) { pass: 'testing', sendImmediately: false } - }, function(error, response, body) { + }, function (error, response, body) { t.equal(error, null) t.equal(response.statusCode, 200) t.equal(numRedirects, 1) t.end() - }).on('redirect', function() { + }).on('redirect', function () { t.equal(this.response.statusCode, 401) numRedirects++ }) }) -tape('without sendImmediately = false', function(t) { +tape('without sendImmediately = false', function (t) { var numRedirects = 0 // If we don't set sendImmediately = false, request will send basic auth @@ -193,18 +192,18 @@ tape('without sendImmediately = false', function(t) { user: 'test', pass: 'testing' } - }, function(error, response, body) { + }, function (error, response, body) { t.equal(error, null) t.equal(response.statusCode, 401) t.equal(numRedirects, 0) t.end() - }).on('redirect', function() { + }).on('redirect', function () { t.equal(this.response.statusCode, 401) numRedirects++ }) }) -tape('with different credentials', function(t) { +tape('with different credentials', function (t) { var numRedirects = 0 request({ @@ -215,19 +214,19 @@ tape('with different credentials', function(t) { pass: 'CircleOfLife', sendImmediately: false } - }, function(error, response, body) { + }, function (error, response, body) { t.equal(error, null) t.equal(response.statusCode, 200) t.equal(numRedirects, 1) t.end() - }).on('redirect', function() { + }).on('redirect', function () { t.equal(this.response.statusCode, 401) numRedirects++ }) }) -tape('cleanup', function(t) { - digestServer.close(function() { +tape('cleanup', function (t) { + digestServer.close(function () { t.end() }) }) diff --git a/tests/test-emptyBody.js b/tests/test-emptyBody.js index 412e93229..250bfe179 100644 --- a/tests/test-emptyBody.js +++ b/tests/test-emptyBody.js @@ -1,23 +1,23 @@ 'use strict' var request = require('../index') - , http = require('http') - , tape = require('tape') +var http = require('http') +var tape = require('tape') var s = http.createServer(function (req, resp) { resp.statusCode = 200 resp.end('') }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('empty body with encoding', function(t) { - request(s.url, function(err, res, body) { +tape('empty body with encoding', function (t) { + request(s.url, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, '') @@ -25,11 +25,11 @@ tape('empty body with encoding', function(t) { }) }) -tape('empty body without encoding', function(t) { +tape('empty body without encoding', function (t) { request({ url: s.url, encoding: null - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.same(body, new Buffer(0)) @@ -37,11 +37,11 @@ tape('empty body without encoding', function(t) { }) }) -tape('empty JSON body', function(t) { +tape('empty JSON body', function (t) { request({ url: s.url, json: {} - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, undefined) @@ -49,8 +49,8 @@ tape('empty JSON body', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-errors.js b/tests/test-errors.js index 9adc0a0ee..7060e9fca 100644 --- a/tests/test-errors.js +++ b/tests/test-errors.js @@ -1,19 +1,19 @@ 'use strict' var request = require('../index') - , tape = require('tape') +var tape = require('tape') var local = 'http://localhost:0/asdf' -tape('without uri', function(t) { - t.throws(function() { +tape('without uri', function (t) { + t.throws(function () { request({}) }, /^Error: options\.uri is a required argument$/) t.end() }) -tape('invalid uri 1', function(t) { - t.throws(function() { +tape('invalid uri 1', function (t) { + t.throws(function () { request({ uri: 'this-is-not-a-valid-uri' }) @@ -21,8 +21,8 @@ tape('invalid uri 1', function(t) { t.end() }) -tape('invalid uri 2', function(t) { - t.throws(function() { +tape('invalid uri 2', function (t) { + t.throws(function () { request({ uri: 'github.com/uri-is-not-valid-without-protocol' }) @@ -30,9 +30,9 @@ tape('invalid uri 2', function(t) { t.end() }) -tape('invalid uri + NO_PROXY', function(t) { +tape('invalid uri + NO_PROXY', function (t) { process.env.NO_PROXY = 'google.com' - t.throws(function() { + t.throws(function () { request({ uri: 'invalid' }) @@ -41,8 +41,8 @@ tape('invalid uri + NO_PROXY', function(t) { t.end() }) -tape('deprecated unix URL', function(t) { - t.throws(function() { +tape('deprecated unix URL', function (t) { + t.throws(function () { request({ uri: 'unix://path/to/socket/and/then/request/path' }) @@ -50,8 +50,8 @@ tape('deprecated unix URL', function(t) { t.end() }) -tape('invalid body', function(t) { - t.throws(function() { +tape('invalid body', function (t) { + t.throws(function () { request({ uri: local, body: {} }) @@ -59,8 +59,8 @@ tape('invalid body', function(t) { t.end() }) -tape('invalid multipart', function(t) { - t.throws(function() { +tape('invalid multipart', function (t) { + t.throws(function () { request({ uri: local, multipart: 'foo' @@ -69,8 +69,8 @@ tape('invalid multipart', function(t) { t.end() }) -tape('multipart without body 1', function(t) { - t.throws(function() { +tape('multipart without body 1', function (t) { + t.throws(function () { request({ uri: local, multipart: [ {} ] @@ -79,8 +79,8 @@ tape('multipart without body 1', function(t) { t.end() }) -tape('multipart without body 2', function(t) { - t.throws(function() { +tape('multipart without body 2', function (t) { + t.throws(function () { request(local, { multipart: [ {} ] }) @@ -88,8 +88,8 @@ tape('multipart without body 2', function(t) { t.end() }) -tape('head method with a body', function(t) { - t.throws(function() { +tape('head method with a body', function (t) { + t.throws(function () { request(local, { method: 'HEAD', body: 'foo' @@ -98,8 +98,8 @@ tape('head method with a body', function(t) { t.end() }) -tape('head method with a body 2', function(t) { - t.throws(function() { +tape('head method with a body 2', function (t) { + t.throws(function () { request.head(local, { body: 'foo' }) diff --git a/tests/test-event-forwarding.js b/tests/test-event-forwarding.js index 3c2086eb3..c057a0bb9 100644 --- a/tests/test-event-forwarding.js +++ b/tests/test-event-forwarding.js @@ -1,14 +1,14 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var s = server.createServer() -tape('setup', function(t) { - s.listen(0, function() { - s.on('/', function(req, res) { +tape('setup', function (t) { + s.listen(0, function () { + s.on('/', function (req, res) { res.writeHead(200, { 'content-type': 'text/plain' }) res.write('waited') res.end() @@ -17,23 +17,23 @@ tape('setup', function(t) { }) }) -tape('should emit socket event', function(t) { +tape('should emit socket event', function (t) { t.plan(4) - var req = request(s.url, function(err, res, body) { + var req = request(s.url, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'waited') }) - req.on('socket', function(socket) { + req.on('socket', function (socket) { var requestSocket = req.req.socket t.equal(requestSocket, socket) }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-follow-all-303.js b/tests/test-follow-all-303.js index 2110146fc..b40adf84b 100644 --- a/tests/test-follow-all-303.js +++ b/tests/test-follow-all-303.js @@ -1,8 +1,8 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var server = http.createServer(function (req, res) { if (req.method === 'POST') { @@ -14,14 +14,14 @@ var server = http.createServer(function (req, res) { } }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('followAllRedirects with 303', function(t) { +tape('followAllRedirects with 303', function (t) { var redirects = 0 request.post({ @@ -33,13 +33,13 @@ tape('followAllRedirects with 303', function(t) { t.equal(body, 'ok') t.equal(redirects, 1) t.end() - }).on('redirect', function() { + }).on('redirect', function () { redirects++ }) }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-follow-all.js b/tests/test-follow-all.js index e8054cafb..c35d74b3e 100644 --- a/tests/test-follow-all.js +++ b/tests/test-follow-all.js @@ -1,8 +1,8 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var server = http.createServer(function (req, res) { // redirect everything 3 times, no matter what. @@ -25,14 +25,14 @@ var server = http.createServer(function (req, res) { res.end('try again') }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('followAllRedirects', function(t) { +tape('followAllRedirects', function (t) { var redirects = 0 request.post({ @@ -45,13 +45,13 @@ tape('followAllRedirects', function(t) { t.equal(body, 'ok') t.equal(redirects, 4) t.end() - }).on('redirect', function() { + }).on('redirect', function () { redirects++ }) }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-form-data-error.js b/tests/test-form-data-error.js index d5d6f2a5d..d6ee25d1b 100644 --- a/tests/test-form-data-error.js +++ b/tests/test-form-data-error.js @@ -1,19 +1,19 @@ 'use strict' var request = require('../index') - , server = require('./server') - , tape = require('tape') +var server = require('./server') +var tape = require('tape') var s = server.createServer() -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -tape('re-emit formData errors', function(t) { - s.on('/', function(req, res) { +tape('re-emit formData errors', function (t) { + s.on('/', function (req, res) { res.writeHead(400) res.end() t.fail('The form-data error did not abort the request.') @@ -21,14 +21,13 @@ tape('re-emit formData errors', function(t) { request.post(s.url, function (err, res, body) { t.equal(err.message, 'form-data: Arrays are not supported.') - setTimeout(function() { + setTimeout(function () { t.end() }, 10) }).form().append('field', ['value1', 'value2']) }) -tape('omit content-length header if the value is set to NaN', function(t) { - +tape('omit content-length header if the value is set to NaN', function (t) { // returns chunked HTTP response which is streamed to the 2nd HTTP request in the form data s.on('/chunky', server.createChunkResponse( ['some string', @@ -36,31 +35,31 @@ tape('omit content-length header if the value is set to NaN', function(t) { ])) // accepts form data request - s.on('/stream', function(req, resp) { - req.on('data', function(chunk) { + s.on('/stream', function (req, resp) { + req.on('data', function (chunk) { // consume the request body }) - req.on('end', function() { + req.on('end', function () { resp.writeHead(200) resp.end() }) }) - var sendStreamRequest = function(stream) { + var sendStreamRequest = function (stream) { request.post({ uri: s.url + '/stream', formData: { param: stream } - }, function(err, res) { + }, function (err, res) { t.error(err, 'request failed') t.end() }) } request.get({ - uri: s.url + '/chunky', - }).on('response', function(res) { + uri: s.url + '/chunky' + }).on('response', function (res) { sendStreamRequest(res) }) }) @@ -79,8 +78,8 @@ tape('form-data should throw on null value', function (t) { t.end() }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-form-data.js b/tests/test-form-data.js index fbfc8c5e2..d43a88d7c 100644 --- a/tests/test-form-data.js +++ b/tests/test-form-data.js @@ -1,20 +1,20 @@ 'use strict' var http = require('http') - , path = require('path') - , mime = require('mime-types') - , request = require('../index') - , fs = require('fs') - , tape = require('tape') +var path = require('path') +var mime = require('mime-types') +var request = require('../index') +var fs = require('fs') +var tape = require('tape') -function runTest(t, options) { +function runTest (t, options) { var remoteFile = path.join(__dirname, 'googledoodle.jpg') - , localFile = path.join(__dirname, 'unicycle.jpg') - , multipartFormData = {} + var localFile = path.join(__dirname, 'unicycle.jpg') + var multipartFormData = {} - var server = http.createServer(function(req, res) { + var server = http.createServer(function (req, res) { if (req.url === '/file') { - res.writeHead(200, {'content-type': 'image/jpg', 'content-length':7187}) + res.writeHead(200, {'content-type': 'image/jpg', 'content-length': 7187}) res.end(fs.readFileSync(remoteFile), 'binary') return } @@ -36,51 +36,51 @@ function runTest(t, options) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { // check for the fields' traces // 1st field : my_field - t.ok( data.indexOf('form-data; name="my_field"') !== -1 ) - t.ok( data.indexOf(multipartFormData.my_field) !== -1 ) + t.ok(data.indexOf('form-data; name="my_field"') !== -1) + t.ok(data.indexOf(multipartFormData.my_field) !== -1) // 2nd field : my_buffer - t.ok( data.indexOf('form-data; name="my_buffer"') !== -1 ) - t.ok( data.indexOf(multipartFormData.my_buffer) !== -1 ) + t.ok(data.indexOf('form-data; name="my_buffer"') !== -1) + t.ok(data.indexOf(multipartFormData.my_buffer) !== -1) // 3rd field : my_file - t.ok( data.indexOf('form-data; name="my_file"') !== -1 ) - t.ok( data.indexOf('; filename="' + path.basename(multipartFormData.my_file.path) + '"') !== -1 ) + t.ok(data.indexOf('form-data; name="my_file"') !== -1) + t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.my_file.path) + '"') !== -1) // check for unicycle.jpg traces - t.ok( data.indexOf('2005:06:21 01:44:12') !== -1 ) - t.ok( data.indexOf('Content-Type: ' + mime.lookup(multipartFormData.my_file.path) ) !== -1 ) + t.ok(data.indexOf('2005:06:21 01:44:12') !== -1) + t.ok(data.indexOf('Content-Type: ' + mime.lookup(multipartFormData.my_file.path)) !== -1) // 4th field : remote_file - t.ok( data.indexOf('form-data; name="remote_file"') !== -1 ) - t.ok( data.indexOf('; filename="' + path.basename(multipartFormData.remote_file.path) + '"') !== -1 ) + t.ok(data.indexOf('form-data; name="remote_file"') !== -1) + t.ok(data.indexOf('; filename="' + path.basename(multipartFormData.remote_file.path) + '"') !== -1) // 5th field : file with metadata - t.ok( data.indexOf('form-data; name="secret_file"') !== -1 ) - t.ok( data.indexOf('Content-Disposition: form-data; name="secret_file"; filename="topsecret.jpg"') !== -1 ) - t.ok( data.indexOf('Content-Type: image/custom') !== -1 ) + t.ok(data.indexOf('form-data; name="secret_file"') !== -1) + t.ok(data.indexOf('Content-Disposition: form-data; name="secret_file"; filename="topsecret.jpg"') !== -1) + t.ok(data.indexOf('Content-Type: image/custom') !== -1) // 6th field : batch of files - t.ok( data.indexOf('form-data; name="batch"') !== -1 ) - t.ok( data.match(/form-data; name="batch"/g).length === 2 ) + t.ok(data.indexOf('form-data; name="batch"') !== -1) + t.ok(data.match(/form-data; name="batch"/g).length === 2) // check for http://localhost:nnnn/file traces - t.ok( data.indexOf('Photoshop ICC') !== -1 ) - t.ok( data.indexOf('Content-Type: ' + mime.lookup(remoteFile) ) !== -1 ) + t.ok(data.indexOf('Photoshop ICC') !== -1) + t.ok(data.indexOf('Content-Type: ' + mime.lookup(remoteFile)) !== -1) res.writeHead(200) res.end(options.json ? JSON.stringify({status: 'done'}) : 'done') }) }) - server.listen(0, function() { + server.listen(0, function () { var url = 'http://localhost:' + this.address().port // @NOTE: multipartFormData properties must be set here so that my_file read stream does not leak in node v0.8 multipartFormData.my_field = 'my_value' @@ -113,22 +113,21 @@ function runTest(t, options) { t.equal(err, null) t.equal(res.statusCode, 200) t.deepEqual(body, options.json ? {status: 'done'} : 'done') - server.close(function() { + server.close(function () { t.end() }) }) - }) } -tape('multipart formData', function(t) { +tape('multipart formData', function (t) { runTest(t, {json: false}) }) -tape('multipart formData + JSON', function(t) { +tape('multipart formData + JSON', function (t) { runTest(t, {json: true}) }) -tape('multipart formData + basic auth', function(t) { +tape('multipart formData + basic auth', function (t) { runTest(t, {json: false, auth: true}) }) diff --git a/tests/test-form-urlencoded.js b/tests/test-form-urlencoded.js index f080a27aa..5e46917bb 100644 --- a/tests/test-form-urlencoded.js +++ b/tests/test-form-urlencoded.js @@ -1,14 +1,11 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - +var request = require('../index') +var tape = require('tape') function runTest (t, options, index) { - - var server = http.createServer(function(req, res) { - + var server = http.createServer(function (req, res) { if (index === 0 || index === 3) { t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') } else { @@ -20,11 +17,11 @@ function runTest (t, options, index) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { t.equal(data, 'some=url&encoded=data') res.writeHead(200) @@ -32,13 +29,13 @@ function runTest (t, options, index) { }) }) - server.listen(0, function() { + server.listen(0, function () { var url = 'http://localhost:' + this.address().port - var r = request.post(url, options, function(err, res, body) { + var r = request.post(url, options, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'done') - server.close(function() { + server.close(function () { t.end() }) }) @@ -70,7 +67,7 @@ var cases = [ ] cases.forEach(function (options, index) { - tape('application/x-www-form-urlencoded ' + index, function(t) { + tape('application/x-www-form-urlencoded ' + index, function (t) { runTest(t, options, index) }) }) diff --git a/tests/test-form.js b/tests/test-form.js index 836ec1dfe..1c0a4d25d 100644 --- a/tests/test-form.js +++ b/tests/test-form.js @@ -1,22 +1,21 @@ 'use strict' var http = require('http') - , path = require('path') - , mime = require('mime-types') - , request = require('../index') - , fs = require('fs') - , tape = require('tape') - -tape('multipart form append', function(t) { +var path = require('path') +var mime = require('mime-types') +var request = require('../index') +var fs = require('fs') +var tape = require('tape') +tape('multipart form append', function (t) { var remoteFile = path.join(__dirname, 'googledoodle.jpg') - , localFile = path.join(__dirname, 'unicycle.jpg') - , totalLength = null - , FIELDS = [] + var localFile = path.join(__dirname, 'unicycle.jpg') + var totalLength = null + var FIELDS = [] - var server = http.createServer(function(req, res) { + var server = http.createServer(function (req, res) { if (req.url === '/file') { - res.writeHead(200, {'content-type': 'image/jpg', 'content-length':7187}) + res.writeHead(200, {'content-type': 'image/jpg', 'content-length': 7187}) res.end(fs.readFileSync(remoteFile), 'binary') return } @@ -28,41 +27,41 @@ tape('multipart form append', function(t) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { var field // check for the fields' traces // 1st field : my_field field = FIELDS.shift() - t.ok( data.indexOf('form-data; name="' + field.name + '"') !== -1 ) - t.ok( data.indexOf(field.value) !== -1 ) + t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1) + t.ok(data.indexOf(field.value) !== -1) // 2nd field : my_buffer field = FIELDS.shift() - t.ok( data.indexOf('form-data; name="' + field.name + '"') !== -1 ) - t.ok( data.indexOf(field.value) !== -1 ) + t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1) + t.ok(data.indexOf(field.value) !== -1) // 3rd field : my_file field = FIELDS.shift() - t.ok( data.indexOf('form-data; name="' + field.name + '"') !== -1 ) - t.ok( data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1 ) + t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1) + t.ok(data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1) // check for unicycle.jpg traces - t.ok( data.indexOf('2005:06:21 01:44:12') !== -1 ) - t.ok( data.indexOf('Content-Type: ' + mime.lookup(field.value.path) ) !== -1 ) + t.ok(data.indexOf('2005:06:21 01:44:12') !== -1) + t.ok(data.indexOf('Content-Type: ' + mime.lookup(field.value.path)) !== -1) // 4th field : remote_file field = FIELDS.shift() - t.ok( data.indexOf('form-data; name="' + field.name + '"') !== -1 ) - t.ok( data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1 ) + t.ok(data.indexOf('form-data; name="' + field.name + '"') !== -1) + t.ok(data.indexOf('; filename="' + path.basename(field.value.path) + '"') !== -1) // check for http://localhost:nnnn/file traces - t.ok( data.indexOf('Photoshop ICC') !== -1 ) - t.ok( data.indexOf('Content-Type: ' + mime.lookup(remoteFile) ) !== -1 ) + t.ok(data.indexOf('Photoshop ICC') !== -1) + t.ok(data.indexOf('Content-Type: ' + mime.lookup(remoteFile)) !== -1) - t.ok( +req.headers['content-length'] === totalLength ) + t.ok(+req.headers['content-length'] === totalLength) res.writeHead(200) res.end('done') @@ -71,7 +70,7 @@ tape('multipart form append', function(t) { }) }) - server.listen(0, function() { + server.listen(0, function () { var url = 'http://localhost:' + this.address().port FIELDS = [ { name: 'my_field', value: 'my_value' }, @@ -80,21 +79,21 @@ tape('multipart form append', function(t) { { name: 'remote_file', value: request(url + '/file') } ] - var req = request.post(url + '/upload', function(err, res, body) { + var req = request.post(url + '/upload', function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'done') - server.close(function() { + server.close(function () { t.end() }) }) var form = req.form() - FIELDS.forEach(function(field) { + FIELDS.forEach(function (field) { form.append(field.name, field.value) }) - form.getLength(function(err, length) { + form.getLength(function (err, length) { t.equal(err, null) totalLength = length }) diff --git a/tests/test-gzip.js b/tests/test-gzip.js index ac523a8d7..b69f3cdc4 100644 --- a/tests/test-gzip.js +++ b/tests/test-gzip.js @@ -1,18 +1,18 @@ 'use strict' var request = require('../index') - , http = require('http') - , zlib = require('zlib') - , assert = require('assert') - , bufferEqual = require('buffer-equal') - , tape = require('tape') +var http = require('http') +var zlib = require('zlib') +var assert = require('assert') +var bufferEqual = require('buffer-equal') +var tape = require('tape') var testContent = 'Compressible response content.\n' - , testContentBig - , testContentBigGzip - , testContentGzip +var testContentBig +var testContentBigGzip +var testContentGzip -var server = http.createServer(function(req, res) { +var server = http.createServer(function (req, res) { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') @@ -38,15 +38,15 @@ var server = http.createServer(function(req, res) { } else if (req.url === '/chunks') { res.writeHead(200) res.write(testContentBigGzip.slice(0, 4096)) - setTimeout(function() { res.end(testContentBigGzip.slice(4096)) }, 10) + setTimeout(function () { res.end(testContentBigGzip.slice(4096)) }, 10) } else if (req.url === '/just-slightly-truncated') { - zlib.gzip(testContent, function(err, data) { + zlib.gzip(testContent, function (err, data) { assert.equal(err, null) // truncate the CRC checksum and size check at the end of the stream - res.end(data.slice(0, data.length-8)) + res.end(data.slice(0, data.length - 8)) }) } else { - zlib.gzip(testContent, function(err, data) { + zlib.gzip(testContent, function (err, data) { assert.equal(err, null) res.end(data) }) @@ -62,13 +62,13 @@ var server = http.createServer(function(req, res) { } }) -tape('setup', function(t) { +tape('setup', function (t) { // Need big compressed content to be large enough to chunk into gzip blocks. // Want it to be deterministic to ensure test is reliable. // Generate pseudo-random printable ASCII characters using MINSTD var a = 48271 - , m = 0x7FFFFFFF - , x = 1 + var m = 0x7FFFFFFF + var x = 1 testContentBig = new Buffer(10240) for (var i = 0; i < testContentBig.length; ++i) { x = (a * x) & m @@ -76,15 +76,15 @@ tape('setup', function(t) { testContentBig[i] = (x % 95) + 32 } - zlib.gzip(testContent, function(err, data) { + zlib.gzip(testContent, function (err, data) { t.equal(err, null) testContentGzip = data - zlib.gzip(testContentBig, function(err, data2) { + zlib.gzip(testContentBig, function (err, data2) { t.equal(err, null) testContentBigGzip = data2 - server.listen(0, function() { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) @@ -92,9 +92,9 @@ tape('setup', function(t) { }) }) -tape('transparently supports gzip decoding to callbacks', function(t) { +tape('transparently supports gzip decoding to callbacks', function (t) { var options = { url: server.url + '/foo', gzip: true } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-encoding'], 'gzip') t.equal(body, testContent) @@ -102,9 +102,9 @@ tape('transparently supports gzip decoding to callbacks', function(t) { }) }) -tape('supports slightly invalid gzip content', function(t) { +tape('supports slightly invalid gzip content', function (t) { var options = { url: server.url + '/just-slightly-truncated', gzip: true } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-encoding'], 'gzip') t.equal(body, testContent) @@ -112,30 +112,30 @@ tape('supports slightly invalid gzip content', function(t) { }) }) -tape('transparently supports gzip decoding to pipes', function(t) { +tape('transparently supports gzip decoding to pipes', function (t) { var options = { url: server.url + '/foo', gzip: true } var chunks = [] request.get(options) - .on('data', function(chunk) { + .on('data', function (chunk) { chunks.push(chunk) }) - .on('end', function() { + .on('end', function () { t.equal(Buffer.concat(chunks).toString(), testContent) t.end() }) - .on('error', function(err) { + .on('error', function (err) { t.fail(err) }) }) -tape('does not request gzip if user specifies Accepted-Encodings', function(t) { +tape('does not request gzip if user specifies Accepted-Encodings', function (t) { var headers = { 'Accept-Encoding': null } var options = { url: server.url + '/foo', headers: headers, gzip: true } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-encoding'], undefined) t.equal(body, testContent) @@ -143,10 +143,10 @@ tape('does not request gzip if user specifies Accepted-Encodings', function(t) { }) }) -tape('does not decode user-requested encoding by default', function(t) { +tape('does not decode user-requested encoding by default', function (t) { var headers = { 'Accept-Encoding': 'gzip' } var options = { url: server.url + '/foo', headers: headers } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-encoding'], 'gzip') t.equal(body, testContentGzip.toString()) @@ -154,7 +154,7 @@ tape('does not decode user-requested encoding by default', function(t) { }) }) -tape('supports character encoding with gzip encoding', function(t) { +tape('supports character encoding with gzip encoding', function (t) { var headers = { 'Accept-Encoding': 'gzip' } var options = { url: server.url + '/foo', @@ -164,22 +164,22 @@ tape('supports character encoding with gzip encoding', function(t) { } var strings = [] request.get(options) - .on('data', function(string) { + .on('data', function (string) { t.equal(typeof string, 'string') strings.push(string) }) - .on('end', function() { + .on('end', function () { t.equal(strings.join(''), testContent) t.end() }) - .on('error', function(err) { + .on('error', function (err) { t.fail(err) }) }) -tape('transparently supports gzip error to callbacks', function(t) { +tape('transparently supports gzip error to callbacks', function (t) { var options = { url: server.url + '/error', gzip: true } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err.code, 'Z_DATA_ERROR') t.equal(res, undefined) t.equal(body, undefined) @@ -187,10 +187,10 @@ tape('transparently supports gzip error to callbacks', function(t) { }) }) -tape('transparently supports gzip error to pipes', function(t) { +tape('transparently supports gzip error to pipes', function (t) { var options = { url: server.url + '/error', gzip: true } request.get(options) - .on('data', function (/*chunk*/) { + .on('data', function (chunk) { t.fail('Should not receive data event') }) .on('end', function () { @@ -202,12 +202,12 @@ tape('transparently supports gzip error to pipes', function(t) { }) }) -tape('pause when streaming from a gzip request object', function(t) { +tape('pause when streaming from a gzip request object', function (t) { var chunks = [] var paused = false var options = { url: server.url + '/chunks', gzip: true } request.get(options) - .on('data', function(chunk) { + .on('data', function (chunk) { var self = this t.notOk(paused, 'Only receive data when not paused') @@ -216,40 +216,40 @@ tape('pause when streaming from a gzip request object', function(t) { if (chunks.length === 1) { self.pause() paused = true - setTimeout(function() { + setTimeout(function () { paused = false self.resume() }, 100) } }) - .on('end', function() { + .on('end', function () { t.ok(chunks.length > 1, 'Received multiple chunks') t.ok(bufferEqual(Buffer.concat(chunks), testContentBig), 'Expected content') t.end() }) }) -tape('pause before streaming from a gzip request object', function(t) { +tape('pause before streaming from a gzip request object', function (t) { var paused = true var options = { url: server.url + '/foo', gzip: true } var r = request.get(options) r.pause() - r.on('data', function(data) { + r.on('data', function (data) { t.notOk(paused, 'Only receive data when not paused') t.equal(data.toString(), testContent) }) r.on('end', t.end.bind(t)) - setTimeout(function() { + setTimeout(function () { paused = false r.resume() }, 100) }) -tape('transparently supports deflate decoding to callbacks', function(t) { +tape('transparently supports deflate decoding to callbacks', function (t) { var options = { url: server.url + '/foo', gzip: true, headers: { 'Accept-Encoding': 'deflate' } } - request.get(options, function(err, res, body) { + request.get(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-encoding'], 'deflate') t.equal(body, testContent) @@ -257,33 +257,33 @@ tape('transparently supports deflate decoding to callbacks', function(t) { }) }) -tape('do not try to pipe HEAD request responses', function(t) { +tape('do not try to pipe HEAD request responses', function (t) { var options = { method: 'HEAD', url: server.url + '/foo', gzip: true } - request(options, function(err, res, body) { + request(options, function (err, res, body) { t.equal(err, null) t.equal(body, '') t.end() }) }) -tape('do not try to pipe responses with no body', function(t) { +tape('do not try to pipe responses with no body', function (t) { var options = { url: server.url + '/foo', gzip: true } options.headers = {code: 105} - request.post(options, function(err, res, body) { + request.post(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers.code, '105') t.equal(body, '') - + options.headers = {code: 204} - request.post(options, function(err, res, body) { + request.post(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers.code, '204') t.equal(body, '') - + options.headers = {code: 304} - request.post(options, function(err, res, body) { + request.post(options, function (err, res, body) { t.equal(err, null) t.equal(res.headers.code, '304') t.equal(body, '') @@ -294,9 +294,8 @@ tape('do not try to pipe responses with no body', function(t) { }) }) - -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-hawk.js b/tests/test-hawk.js index f0aa1d56b..34db8da25 100644 --- a/tests/test-hawk.js +++ b/tests/test-hawk.js @@ -1,13 +1,13 @@ 'use strict' var http = require('http') - , request = require('../index') - , hawk = require('hawk') - , tape = require('tape') - , assert = require('assert') +var request = require('../index') +var hawk = require('hawk') +var tape = require('tape') +var assert = require('assert') -var server = http.createServer(function(req, res) { - var getCred = function(id, callback) { +var server = http.createServer(function (req, res) { + var getCred = function (id, callback) { assert.equal(id, 'dh37fgj492je') var credentials = { key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', @@ -17,7 +17,7 @@ var server = http.createServer(function(req, res) { return callback(null, credentials) } - hawk.server.authenticate(req, getCred, {}, function(err, credentials, attributes) { + hawk.server.authenticate(req, getCred, {}, function (err, credentials, attributes) { res.writeHead(err ? 401 : 200, { 'Content-Type': 'text/plain' }) @@ -25,14 +25,14 @@ var server = http.createServer(function(req, res) { }) }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('hawk', function(t) { +tape('hawk', function (t) { var creds = { key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', algorithm: 'sha256', @@ -40,7 +40,7 @@ tape('hawk', function(t) { } request(server.url, { hawk: { credentials: creds } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'Hello Steve') @@ -48,8 +48,8 @@ tape('hawk', function(t) { }) }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-headers.js b/tests/test-headers.js index 91abd25ef..c1c29622a 100644 --- a/tests/test-headers.js +++ b/tests/test-headers.js @@ -1,25 +1,25 @@ 'use strict' var server = require('./server') - , request = require('../index') - , util = require('util') - , tape = require('tape') - , url = require('url') +var request = require('../index') +var util = require('util') +var tape = require('tape') +var url = require('url') var s = server.createServer() -s.on('/redirect/from', function(req, res) { +s.on('/redirect/from', function (req, res) { res.writeHead(301, { - location : '/redirect/to' + location: '/redirect/to' }) res.end() }) -s.on('/redirect/to', function(req, res) { +s.on('/redirect/to', function (req, res) { res.end('ok') }) -s.on('/headers.json', function(req, res) { +s.on('/headers.json', function (req, res) { res.writeHead(200, { 'Content-Type': 'application/json' }) @@ -27,15 +27,15 @@ s.on('/headers.json', function(req, res) { res.end(JSON.stringify(req.headers)) }) -function runTest(name, path, requestObj, serverAssertFn) { - tape(name, function(t) { - s.on('/' + path, function(req, res) { +function runTest (name, path, requestObj, serverAssertFn) { + tape(name, function (t) { + s.on('/' + path, function (req, res) { serverAssertFn(t, req, res) res.writeHead(200) res.end() }) requestObj.url = s.url + '/' + path - request(requestObj, function(err, res, body) { + request(requestObj, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.end() @@ -43,12 +43,12 @@ function runTest(name, path, requestObj, serverAssertFn) { }) } -function addTests() { +function addTests () { runTest( '#125: headers.cookie with no cookie jar', 'no-jar', {headers: {cookie: 'foo=bar'}}, - function(t, req, res) { + function (t, req, res) { t.equal(req.headers.cookie, 'foo=bar') }) @@ -58,7 +58,7 @@ function addTests() { '#125: headers.cookie + cookie jar', 'header-and-jar', {jar: jar, headers: {cookie: 'foo=bar'}}, - function(t, req, res) { + function (t, req, res) { t.equal(req.headers.cookie, 'foo=bar; quux=baz') }) @@ -68,7 +68,7 @@ function addTests() { '#794: ignore cookie parsing and domain errors', 'ignore-errors', {jar: jar2, headers: {cookie: 'foo=bar'}}, - function(t, req, res) { + function (t, req, res) { t.equal(req.headers.cookie, 'foo=bar') }) @@ -80,7 +80,7 @@ function addTests() { method: 'POST', headers: { 'content-type': 'application/json; charset=UTF-8' }, body: { hello: 'my friend' }}, - function(t, req, res) { + function (t, req, res) { t.equal(req.headers['content-type'], 'application/json; charset=UTF-8') } ) @@ -89,16 +89,16 @@ function addTests() { 'neither headers.cookie nor a cookie jar is specified', 'no-cookie', {}, - function(t, req, res) { + function (t, req, res) { t.equal(req.headers.cookie, undefined) }) } -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { addTests() - tape('cleanup', function(t) { - s.close(function() { + tape('cleanup', function (t) { + s.close(function () { t.end() }) }) @@ -106,12 +106,12 @@ tape('setup', function(t) { }) }) -tape('upper-case Host header and redirect', function(t) { +tape('upper-case Host header and redirect', function (t) { // Horrible hack to observe the raw data coming to the server (before Node // core lower-cases the headers) var rawData = '' - s.on('connection', function(socket) { + s.on('connection', function (socket) { if (socket.ondata) { var ondata = socket.ondata } @@ -127,7 +127,7 @@ tape('upper-case Host header and redirect', function(t) { socket.ondata = handledata }) - function checkHostHeader(host) { + function checkHostHeader (host) { t.ok( new RegExp('^Host: ' + host + '$', 'm').test(rawData), util.format( @@ -138,9 +138,9 @@ tape('upper-case Host header and redirect', function(t) { var redirects = 0 request({ - url : s.url + '/redirect/from', - headers : { Host : '127.0.0.1' } - }, function(err, res, body) { + url: s.url + '/redirect/from', + headers: { Host: '127.0.0.1' } + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'ok') @@ -148,14 +148,14 @@ tape('upper-case Host header and redirect', function(t) { // XXX should the host header change like this after a redirect? checkHostHeader('localhost:' + s.port) t.end() - }).on('redirect', function() { + }).on('redirect', function () { redirects++ t.equal(this.uri.href, s.url + '/redirect/to') checkHostHeader('127.0.0.1') }) }) -tape('undefined headers', function(t) { +tape('undefined headers', function (t) { request({ url: s.url + '/headers.json', headers: { @@ -163,7 +163,7 @@ tape('undefined headers', function(t) { 'X-TEST-2': undefined }, json: true - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(body['x-test-1'], 'test1') t.equal(typeof body['x-test-2'], 'undefined') @@ -171,13 +171,13 @@ tape('undefined headers', function(t) { }) }) -tape('catch invalid characters error - GET', function(t) { +tape('catch invalid characters error - GET', function (t) { request({ url: s.url + '/headers.json', headers: { 'test': 'אבגד' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err.message, 'The header content contains invalid characters') }) .on('error', function (err) { @@ -186,7 +186,7 @@ tape('catch invalid characters error - GET', function(t) { }) }) -tape('catch invalid characters error - POST', function(t) { +tape('catch invalid characters error - POST', function (t) { request({ method: 'POST', url: s.url + '/headers.json', @@ -194,7 +194,7 @@ tape('catch invalid characters error - POST', function(t) { 'test': 'אבגד' }, body: 'beep' - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err.message, 'The header content contains invalid characters') }) .on('error', function (err) { @@ -203,11 +203,11 @@ tape('catch invalid characters error - POST', function(t) { }) }) -tape('IPv6 Host header', function(t) { +tape('IPv6 Host header', function (t) { // Horrible hack to observe the raw data coming to the server var rawData = '' - s.on('connection', function(socket) { + s.on('connection', function (socket) { if (socket.ondata) { var ondata = socket.ondata } @@ -223,7 +223,7 @@ tape('IPv6 Host header', function(t) { socket.ondata = handledata }) - function checkHostHeader(host) { + function checkHostHeader (host) { t.ok( new RegExp('^Host: ' + host + '$', 'im').test(rawData), util.format( @@ -233,8 +233,8 @@ tape('IPv6 Host header', function(t) { } request({ - url : s.url.replace(url.parse(s.url).hostname, '[::1]') + '/headers.json' - }, function(err, res, body) { + url: s.url.replace(url.parse(s.url).hostname, '[::1]') + '/headers.json' + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) checkHostHeader('\\[::1\\]:' + s.port) diff --git a/tests/test-http-signature.js b/tests/test-http-signature.js index b5679beba..5ce015cba 100644 --- a/tests/test-http-signature.js +++ b/tests/test-http-signature.js @@ -1,9 +1,9 @@ 'use strict' var http = require('http') - , request = require('../index') - , httpSignature = require('http-signature') - , tape = require('tape') +var request = require('../index') +var httpSignature = require('http-signature') +var tape = require('tape') var privateKeyPEMs = {} @@ -68,43 +68,43 @@ var server = http.createServer(function (req, res) { res.end() }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('correct key', function(t) { +tape('correct key', function (t) { var options = { httpSignature: { keyId: 'key-1', key: privateKeyPEMs['key-1'] } } - request(server.url, options, function(err, res, body) { + request(server.url, options, function (err, res, body) { t.equal(err, null) t.equal(200, res.statusCode) t.end() }) }) -tape('incorrect key', function(t) { +tape('incorrect key', function (t) { var options = { httpSignature: { keyId: 'key-2', key: privateKeyPEMs['key-1'] } } - request(server.url, options, function(err, res, body) { + request(server.url, options, function (err, res, body) { t.equal(err, null) t.equal(400, res.statusCode) t.end() }) }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-httpModule.js b/tests/test-httpModule.js index 9acf80c0e..4d4e236bf 100644 --- a/tests/test-httpModule.js +++ b/tests/test-httpModule.js @@ -1,27 +1,27 @@ 'use strict' var http = require('http') - , https = require('https') - , destroyable = require('server-destroy') - , server = require('./server') - , request = require('../index') - , tape = require('tape') +var https = require('https') +var destroyable = require('server-destroy') +var server = require('./server') +var request = require('../index') +var tape = require('tape') -var faux_requests_made +var fauxRequestsMade -function clear_faux_requests() { - faux_requests_made = { http: 0, https: 0 } +function clearFauxRequests () { + fauxRequestsMade = { http: 0, https: 0 } } -function wrap_request(name, module) { +function wrapRequest (name, module) { // Just like the http or https module, but note when a request is made. var wrapped = {} - Object.keys(module).forEach(function(key) { + Object.keys(module).forEach(function (key) { var value = module[key] if (key === 'request') { - wrapped[key] = function(/*options, callback*/) { - faux_requests_made[name] += 1 + wrapped[key] = function (/* options, callback */) { + fauxRequestsMade[name] += 1 return value.apply(this, arguments) } } else { @@ -32,32 +32,32 @@ function wrap_request(name, module) { return wrapped } -var faux_http = wrap_request('http', http) - , faux_https = wrap_request('https', https) - , plain_server = server.createServer() - , https_server = server.createSSLServer() +var fauxHTTP = wrapRequest('http', http) +var fauxHTTPS = wrapRequest('https', https) +var plainServer = server.createServer() +var httpsServer = server.createSSLServer() -destroyable(plain_server) -destroyable(https_server) +destroyable(plainServer) +destroyable(httpsServer) -tape('setup', function(t) { - plain_server.listen(0, function() { - plain_server.on('/plain', function (req, res) { +tape('setup', function (t) { + plainServer.listen(0, function () { + plainServer.on('/plain', function (req, res) { res.writeHead(200) res.end('plain') }) - plain_server.on('/to_https', function (req, res) { - res.writeHead(301, { 'location': 'https://localhost:' + https_server.port + '/https' }) + plainServer.on('/to_https', function (req, res) { + res.writeHead(301, { 'location': 'https://localhost:' + httpsServer.port + '/https' }) res.end() }) - https_server.listen(0, function() { - https_server.on('/https', function (req, res) { + httpsServer.listen(0, function () { + httpsServer.on('/https', function (req, res) { res.writeHead(200) res.end('https') }) - https_server.on('/to_plain', function (req, res) { - res.writeHead(302, { 'location': 'http://localhost:' + plain_server.port + '/plain' }) + httpsServer.on('/to_plain', function (req, res) { + res.writeHead(302, { 'location': 'http://localhost:' + plainServer.port + '/plain' }) res.end() }) @@ -66,30 +66,30 @@ tape('setup', function(t) { }) }) -function run_tests(name, httpModules) { - tape(name, function(t) { - var to_https = 'http://localhost:' + plain_server.port + '/to_https' - , to_plain = 'https://localhost:' + https_server.port + '/to_plain' - , options = { httpModules: httpModules, strictSSL: false } - , modulesTest = httpModules || {} +function runTests (name, httpModules) { + tape(name, function (t) { + var toHttps = 'http://localhost:' + plainServer.port + '/to_https' + var toPlain = 'https://localhost:' + httpsServer.port + '/to_plain' + var options = { httpModules: httpModules, strictSSL: false } + var modulesTest = httpModules || {} - clear_faux_requests() + clearFauxRequests() - request(to_https, options, function (err, res, body) { + request(toHttps, options, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'https', 'Received HTTPS server body') - t.equal(faux_requests_made.http, modulesTest['http:' ] ? 1 : 0) - t.equal(faux_requests_made.https, modulesTest['https:'] ? 1 : 0) + t.equal(fauxRequestsMade.http, modulesTest['http:'] ? 1 : 0) + t.equal(fauxRequestsMade.https, modulesTest['https:'] ? 1 : 0) - request(to_plain, options, function (err, res, body) { + request(toPlain, options, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'plain', 'Received HTTPS server body') - t.equal(faux_requests_made.http, modulesTest['http:' ] ? 2 : 0) - t.equal(faux_requests_made.https, modulesTest['https:'] ? 2 : 0) + t.equal(fauxRequestsMade.http, modulesTest['http:'] ? 2 : 0) + t.equal(fauxRequestsMade.https, modulesTest['https:'] ? 2 : 0) t.end() }) @@ -97,15 +97,15 @@ function run_tests(name, httpModules) { }) } -run_tests('undefined') -run_tests('empty', {}) -run_tests('http only', { 'http:': faux_http }) -run_tests('https only', { 'https:': faux_https }) -run_tests('http and https', { 'http:': faux_http, 'https:': faux_https }) +runTests('undefined') +runTests('empty', {}) +runTests('http only', { 'http:': fauxHTTP }) +runTests('https only', { 'https:': fauxHTTPS }) +runTests('http and https', { 'http:': fauxHTTP, 'https:': fauxHTTPS }) -tape('cleanup', function(t) { - plain_server.destroy(function() { - https_server.destroy(function() { +tape('cleanup', function (t) { + plainServer.destroy(function () { + httpsServer.destroy(function () { t.end() }) }) diff --git a/tests/test-https.js b/tests/test-https.js index c298f7d54..3138f3d1e 100644 --- a/tests/test-https.js +++ b/tests/test-https.js @@ -4,32 +4,32 @@ // otherwise exactly the same as the ssl test var server = require('./server') - , request = require('../index') - , fs = require('fs') - , path = require('path') - , tape = require('tape') +var request = require('../index') +var fs = require('fs') +var path = require('path') +var tape = require('tape') var s = server.createSSLServer() - , caFile = path.resolve(__dirname, 'ssl/ca/ca.crt') - , ca = fs.readFileSync(caFile) - , opts = { - ciphers: 'AES256-SHA', - key: path.resolve(__dirname, 'ssl/ca/server.key'), - cert: path.resolve(__dirname, 'ssl/ca/server.crt') - } - , sStrict = server.createSSLServer(opts) +var caFile = path.resolve(__dirname, 'ssl/ca/ca.crt') +var ca = fs.readFileSync(caFile) +var opts = { + ciphers: 'AES256-SHA', + key: path.resolve(__dirname, 'ssl/ca/server.key'), + cert: path.resolve(__dirname, 'ssl/ca/server.crt') +} +var sStrict = server.createSSLServer(opts) -function runAllTests(strict, s) { +function runAllTests (strict, s) { var strictMsg = (strict ? 'strict ' : 'relaxed ') - tape(strictMsg + 'setup', function(t) { - s.listen(0, function() { + tape(strictMsg + 'setup', function (t) { + s.listen(0, function () { t.end() }) }) - function runTest(name, test) { - tape(strictMsg + name, function(t) { + function runTest (name, test) { + tape(strictMsg + name, function (t) { s.on('/' + name, test.resp) test.uri = s.url + '/' + name if (strict) { @@ -39,7 +39,7 @@ function runAllTests(strict, s) { } else { test.rejectUnauthorized = false } - request(test, function(err, resp, body) { + request(test, function (err, resp, body) { t.equal(err, null) if (test.expectBody) { t.deepEqual(test.expectBody, body) @@ -50,46 +50,37 @@ function runAllTests(strict, s) { } runTest('testGet', { - resp : server.createGetResponse('TESTING!') - , expectBody: 'TESTING!' + resp: server.createGetResponse('TESTING!'), expectBody: 'TESTING!' }) runTest('testGetChunkBreak', { - resp : server.createChunkResponse( - [ new Buffer([239]) - , new Buffer([163]) - , new Buffer([191]) - , new Buffer([206]) - , new Buffer([169]) - , new Buffer([226]) - , new Buffer([152]) - , new Buffer([131]) - ]) - , expectBody: '\uf8ff\u03a9\u2603' + resp: server.createChunkResponse( + [ new Buffer([239]), + new Buffer([163]), + new Buffer([191]), + new Buffer([206]), + new Buffer([169]), + new Buffer([226]), + new Buffer([152]), + new Buffer([131]) + ]), + expectBody: '\uf8ff\u03a9\u2603' }) runTest('testGetJSON', { - resp : server.createGetResponse('{"test":true}', 'application/json') - , json : true - , expectBody: {'test':true} + resp: server.createGetResponse('{"test":true}', 'application/json'), json: true, expectBody: {'test': true} }) runTest('testPutString', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : 'PUTTINGDATA' + resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: 'PUTTINGDATA' }) runTest('testPutBuffer', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : new Buffer('PUTTINGDATA') + resp: server.createPostValidator('PUTTINGDATA'), method: 'PUT', body: new Buffer('PUTTINGDATA') }) runTest('testPutJSON', { - resp : server.createPostValidator(JSON.stringify({foo: 'bar'})) - , method: 'PUT' - , json: {foo: 'bar'} + resp: server.createPostValidator(JSON.stringify({foo: 'bar'})), method: 'PUT', json: {foo: 'bar'} }) runTest('testPutMultipart', { @@ -101,16 +92,15 @@ function runAllTests(strict, s) { '\r\n--__BOUNDARY__\r\n\r\n' + 'Oh hi.' + '\r\n--__BOUNDARY__--' - ) - , method: 'PUT' - , multipart: - [ {'content-type': 'text/html', 'body': 'Oh hi.'} - , {'body': 'Oh hi.'} - ] + ), + method: 'PUT', + multipart: [ {'content-type': 'text/html', 'body': 'Oh hi.'}, + {'body': 'Oh hi.'} + ] }) - tape(strictMsg + 'cleanup', function(t) { - s.close(function() { + tape(strictMsg + 'cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-isUrl.js b/tests/test-isUrl.js index cbdd246df..ae7f3ba11 100644 --- a/tests/test-isUrl.js +++ b/tests/test-isUrl.js @@ -1,94 +1,94 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { res.statusCode = 200 res.end('ok') }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.port = this.address().port s.url = 'http://localhost:' + s.port t.end() }) }) -tape('lowercase', function(t) { - request(s.url, function(err, resp, body) { +tape('lowercase', function (t) { + request(s.url, function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('uppercase', function(t) { - request(s.url.replace('http', 'HTTP'), function(err, resp, body) { +tape('uppercase', function (t) { + request(s.url.replace('http', 'HTTP'), function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('mixedcase', function(t) { - request(s.url.replace('http', 'HtTp'), function(err, resp, body) { +tape('mixedcase', function (t) { + request(s.url.replace('http', 'HtTp'), function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('hostname and port', function(t) { +tape('hostname and port', function (t) { request({ uri: { protocol: 'http:', hostname: 'localhost', port: s.port } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('hostname and port 1', function(t) { +tape('hostname and port 1', function (t) { request({ uri: { protocol: 'http:', hostname: 'localhost', port: s.port } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('hostname and port 2', function(t) { +tape('hostname and port 2', function (t) { request({ protocol: 'http:', hostname: 'localhost', port: s.port }, { // need this empty options object, otherwise request thinks no uri was set - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('hostname and port 3', function(t) { +tape('hostname and port 3', function (t) { request({ protocol: 'http:', hostname: 'localhost', port: s.port - }, function(err, res, body) { + }, function (err, res, body) { t.notEqual(err, null) t.equal(err.message, 'options.uri is a required argument') t.equal(body, undefined) @@ -96,7 +96,7 @@ tape('hostname and port 3', function(t) { }) }) -tape('hostname and query string', function(t) { +tape('hostname and query string', function (t) { request({ uri: { protocol: 'http:', @@ -106,15 +106,15 @@ tape('hostname and query string', function(t) { qs: { test: 'test' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(body, 'ok') t.end() }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-json-request.js b/tests/test-json-request.js index b89e254e2..af82f15b5 100644 --- a/tests/test-json-request.js +++ b/tests/test-json-request.js @@ -1,19 +1,19 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var s = server.createServer() -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -function testJSONValue(testId, value) { - tape('test ' + testId, function(t) { +function testJSONValue (testId, value) { + tape('test ' + testId, function (t) { var testUrl = '/' + testId s.on(testUrl, server.createPostJSONValidator(value, 'application/json')) var opts = { @@ -31,8 +31,8 @@ function testJSONValue(testId, value) { }) } -function testJSONValueReviver(testId, value, reviver, revivedValue) { - tape('test ' + testId, function(t) { +function testJSONValueReviver (testId, value, reviver, revivedValue) { + tape('test ' + testId, function (t) { var testUrl = '/' + testId s.on(testUrl, server.createPostJSONValidator(value, 'application/json')) var opts = { @@ -51,8 +51,8 @@ function testJSONValueReviver(testId, value, reviver, revivedValue) { }) } -function testJSONValueReplacer(testId, value, replacer, replacedValue) { - tape('test ' + testId, function(t) { +function testJSONValueReplacer (testId, value, replacer, replacedValue) { + tape('test ' + testId, function (t) { var testUrl = '/' + testId s.on(testUrl, server.createPostJSONValidator(replacedValue, 'application/json')) var opts = { @@ -95,24 +95,23 @@ testJSONValueReviver('jsonReviverInvalid', -48269.592, 'invalid reviver', -48269 testJSONValueReplacer('jsonReplacer', -48269.592, function (k, v) { return v * -1 }, 48269.592) -testJSONValueReplacer('jsonReplacerInvalid', -48269.592,'invalid replacer', -48269.592) +testJSONValueReplacer('jsonReplacerInvalid', -48269.592, 'invalid replacer', -48269.592) testJSONValueReplacer('jsonReplacerObject', {foo: 'bar'}, function (k, v) { return v.toUpperCase ? v.toUpperCase() : v }, {foo: 'BAR'}) - tape('missing body', function (t) { s.on('/missing-body', function (req, res) { t.equal(req.headers['content-type'], undefined) res.end() }) - request({url:s.url + '/missing-body', json:true}, function () { + request({url: s.url + '/missing-body', json: true}, function () { t.end() }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-localAddress.js b/tests/test-localAddress.js index ea3a4ce7c..88a335326 100644 --- a/tests/test-localAddress.js +++ b/tests/test-localAddress.js @@ -1,6 +1,6 @@ 'use strict' var request = require('../index') - , tape = require('tape') +var tape = require('tape') tape('bind to invalid address', function (t) { request.get({ @@ -39,7 +39,7 @@ tape('bind to local address on redirect', function (t) { }) }) request.get({ - uri: 'http://google.com', //redirects to 'http://google.com' + uri: 'http://google.com', // redirects to 'http://google.com' localAddress: localIPS[0] }, function (err, res) { t.equal(err, null) diff --git a/tests/test-multipart-encoding.js b/tests/test-multipart-encoding.js index 8691f1f76..c88a6f143 100644 --- a/tests/test-multipart-encoding.js +++ b/tests/test-multipart-encoding.js @@ -1,11 +1,10 @@ 'use strict' var http = require('http') - , path = require('path') - , request = require('../index') - , fs = require('fs') - , tape = require('tape') - +var path = require('path') +var request = require('../index') +var fs = require('fs') +var tape = require('tape') var localFile = path.join(__dirname, 'unicycle.jpg') var cases = { @@ -83,10 +82,8 @@ var cases = { } } -function runTest(t, test) { - - var server = http.createServer(function(req, res) { - +function runTest (t, test) { + var server = http.createServer(function (req, res) { t.ok(req.headers['content-type'].match(/^multipart\/related; boundary=[^\s;]+$/)) if (test.expected.chunked) { @@ -101,11 +98,11 @@ function runTest(t, test) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { // check for the fields traces if (test.expected.chunked && data.indexOf('name: file') !== -1) { // file @@ -124,7 +121,7 @@ function runTest(t, test) { }) }) - server.listen(0, function() { + server.listen(0, function () { var url = 'http://localhost:' + this.address().port // @NOTE: multipartData properties must be set here // so that file read stream does not leak in node v0.8 @@ -144,7 +141,7 @@ function runTest(t, test) { } Object.keys(cases).forEach(function (name) { - tape('multipart-encoding ' + name, function(t) { + tape('multipart-encoding ' + name, function (t) { runTest(t, cases[name]) }) }) diff --git a/tests/test-multipart.js b/tests/test-multipart.js index c1bf26d23..e52cd0490 100644 --- a/tests/test-multipart.js +++ b/tests/test-multipart.js @@ -1,18 +1,17 @@ 'use strict' var http = require('http') - , path = require('path') - , request = require('../index') - , fs = require('fs') - , tape = require('tape') +var path = require('path') +var request = require('../index') +var fs = require('fs') +var tape = require('tape') - -function runTest(t, a) { +function runTest (t, a) { var remoteFile = path.join(__dirname, 'googledoodle.jpg') - , localFile = path.join(__dirname, 'unicycle.jpg') - , multipartData = [] + var localFile = path.join(__dirname, 'unicycle.jpg') + var multipartData = [] - var server = http.createServer(function(req, res) { + var server = http.createServer(function (req, res) { if (req.url === '/file') { res.writeHead(200, {'content-type': 'image/jpg'}) res.end(fs.readFileSync(remoteFile), 'binary') @@ -34,17 +33,17 @@ function runTest(t, a) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { // check for the fields traces // my_field t.ok(data.indexOf('name: my_field') !== -1) t.ok(data.indexOf(multipartData[0].body) !== -1) - + // my_number t.ok(data.indexOf('name: my_number') !== -1) t.ok(data.indexOf(multipartData[1].body) !== -1) @@ -72,7 +71,7 @@ function runTest(t, a) { }) }) - server.listen(0, function() { + server.listen(0, function () { var url = 'http://localhost:' + this.address().port // @NOTE: multipartData properties must be set here so that my_file read stream does not leak in node v0.8 multipartData = [ @@ -99,7 +98,7 @@ function runTest(t, a) { t.equal(err, null) t.equal(res.statusCode, 200) t.deepEqual(body, a.json ? {status: 'done'} : 'done') - server.close(function() { + server.close(function () { t.end() }) }) @@ -113,16 +112,16 @@ var testHeaders = [ ] var methods = ['post', 'get'] -methods.forEach(function(method) { - testHeaders.forEach(function(header) { - [true, false].forEach(function(json) { +methods.forEach(function (method) { + testHeaders.forEach(function (header) { + [true, false].forEach(function (json) { var name = [ 'multipart-related', method.toUpperCase(), (header || 'default'), (json ? '+' : '-') + 'json' ].join(' ') - tape(name, function(t) { + tape(name, function (t) { runTest(t, {method: method, header: header, json: json}) }) }) diff --git a/tests/test-node-debug.js b/tests/test-node-debug.js index 2291bf9c1..bcc6a401d 100644 --- a/tests/test-node-debug.js +++ b/tests/test-node-debug.js @@ -1,36 +1,36 @@ 'use strict' var request = require('../index') - , http = require('http') - , tape = require('tape') +var http = require('http') +var tape = require('tape') -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { res.statusCode = 200 res.end('') }) var stderr = [] - , prevStderrLen = 0 +var prevStderrLen = 0 -tape('setup', function(t) { +tape('setup', function (t) { process.stderr._oldWrite = process.stderr.write - process.stderr.write = function(string, encoding, fd) { + process.stderr.write = function (string, encoding, fd) { stderr.push(string) } - s.listen(0, function() { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('a simple request should not fail with debugging enabled', function(t) { +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(s.url, function(err, res, body) { + request(s.url, function (err, res, body) { t.ifError(err, 'the request did not fail') t.ok(res, 'the request did not fail') @@ -38,16 +38,16 @@ tape('a simple request should not fail with debugging enabled', function(t) { var url = s.url.replace(/\//g, '\\/') var patterns = [ /^REQUEST { uri: /, - new RegExp('^REQUEST make request ' + url + '\/\n$'), + new RegExp('^REQUEST make request ' + url + '/\n$'), /^REQUEST onRequestResponse /, /^REQUEST finish init /, /^REQUEST response end /, /^REQUEST end event /, /^REQUEST emitting complete / ] - patterns.forEach(function(pattern) { + patterns.forEach(function (pattern) { var found = false - stderr.forEach(function(msg) { + stderr.forEach(function (msg) { if (pattern.test(msg)) { found = true } @@ -59,11 +59,11 @@ tape('a simple request should not fail with debugging enabled', function(t) { }) }) -tape('there should be no further lookups on process.env', function(t) { +tape('there should be no further lookups on process.env', function (t) { process.env.NODE_DEBUG = '' stderr = [] - request(s.url, function(err, res, body) { + request(s.url, function (err, res, body) { t.ifError(err, 'the request did not fail') t.ok(res, 'the request did not fail') t.equal(stderr.length, prevStderrLen, 'env.NODE_DEBUG is not retested') @@ -71,13 +71,13 @@ tape('there should be no further lookups on process.env', function(t) { }) }) -tape('it should be possible to disable debugging at runtime', 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(s.url, function(err, res, body) { + request(s.url, function (err, res, body) { t.ifError(err, 'the request did not fail') t.ok(res, 'the request did not fail') t.equal(stderr.length, 0, 'debugging can be disabled') @@ -85,11 +85,11 @@ tape('it should be possible to disable debugging at runtime', function(t) { }) }) -tape('cleanup', function(t) { +tape('cleanup', function (t) { process.stderr.write = process.stderr._oldWrite delete process.stderr._oldWrite - s.close(function() { + s.close(function () { t.end() }) }) diff --git a/tests/test-oauth.js b/tests/test-oauth.js index dd994f07d..bfb03b971 100644 --- a/tests/test-oauth.js +++ b/tests/test-oauth.js @@ -1,17 +1,17 @@ 'use strict' var oauth = require('oauth-sign') - , qs = require('querystring') - , fs = require('fs') - , path = require('path') - , request = require('../index') - , tape = require('tape') - , crypto = require('crypto') - , http = require('http') - -function getSignature(r) { +var qs = require('querystring') +var fs = require('fs') +var path = require('path') +var request = require('../index') +var tape = require('tape') +var crypto = require('crypto') +var http = require('http') + +function getSignature (r) { var sign - r.headers.Authorization.slice('OAuth '.length).replace(/,\ /g, ',').split(',').forEach(function(v) { + r.headers.Authorization.slice('OAuth '.length).replace(/, /g, ',').split(',').forEach(function (v) { if (v.slice(0, 'oauth_signature="'.length) === 'oauth_signature="') { sign = v.slice('oauth_signature="'.length, -1) } @@ -22,254 +22,247 @@ function getSignature(r) { // Tests from Twitter documentation https://dev.twitter.com/docs/auth/oauth var hmacsign = oauth.hmacsign - , rsasign = oauth.rsasign - , rsa_private_pem = fs.readFileSync(path.join(__dirname, 'ssl', 'test.key')) - , reqsign - , reqsign_rsa - , accsign - , accsign_rsa - , upsign - , upsign_rsa - -tape('reqsign', function(t) { +var rsasign = oauth.rsasign +var rsaPrivatePEM = fs.readFileSync(path.join(__dirname, 'ssl', 'test.key')) +var reqsign +var reqsignRSA +var accsign +var accsignRSA +var upsign +var upsignRSA + +tape('reqsign', function (t) { reqsign = hmacsign('POST', 'https://api.twitter.com/oauth/request_token', - { oauth_callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11' - , oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk' - , oauth_signature_method: 'HMAC-SHA1' - , oauth_timestamp: '1272323042' - , oauth_version: '1.0' + { oauth_callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11', + oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', + oauth_signature_method: 'HMAC-SHA1', + oauth_timestamp: '1272323042', + oauth_version: '1.0' }, 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98') t.equal(reqsign, '8wUi7m5HFQy76nowoCThusfgB+Q=') t.end() }) -tape('reqsign_rsa', function(t) { - reqsign_rsa = rsasign('POST', 'https://api.twitter.com/oauth/request_token', - { oauth_callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11' - , oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk' - , oauth_signature_method: 'RSA-SHA1' - , oauth_timestamp: '1272323042' - , oauth_version: '1.0' - }, rsa_private_pem, 'this parameter is not used for RSA signing') - - t.equal(reqsign_rsa, 'MXdzEnIrQco3ACPoVWxCwv5pxYrm5MFRXbsP3LfRV+zfcRr+WMp/dOPS/3r+Wcb+17Z2IK3uJ8dMHfzb5LiDNCTUIj7SWBrbxOpy3Y6SA6z3vcrtjSekkTHLek1j+mzxOi3r3fkbYaNwjHx3PyoFSazbEstnkQQotbITeFt5FBE=') +tape('reqsignRSA', function (t) { + reqsignRSA = rsasign('POST', 'https://api.twitter.com/oauth/request_token', + { oauth_callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11', + oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', + oauth_signature_method: 'RSA-SHA1', + oauth_timestamp: '1272323042', + oauth_version: '1.0' + }, rsaPrivatePEM, 'this parameter is not used for RSA signing') + + t.equal(reqsignRSA, 'MXdzEnIrQco3ACPoVWxCwv5pxYrm5MFRXbsP3LfRV+zfcRr+WMp/dOPS/3r+Wcb+17Z2IK3uJ8dMHfzb5LiDNCTUIj7SWBrbxOpy3Y6SA6z3vcrtjSekkTHLek1j+mzxOi3r3fkbYaNwjHx3PyoFSazbEstnkQQotbITeFt5FBE=') t.end() }) -tape('accsign', function(t) { +tape('accsign', function (t) { accsign = hmacsign('POST', 'https://api.twitter.com/oauth/access_token', - { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , oauth_signature_method: 'HMAC-SHA1' - , oauth_token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , oauth_timestamp: '1272323047' - , oauth_verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , oauth_version: '1.0' + { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + oauth_signature_method: 'HMAC-SHA1', + oauth_token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + oauth_timestamp: '1272323047', + oauth_verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + oauth_version: '1.0' }, 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA') t.equal(accsign, 'PUw/dHA4fnlJYM6RhXk5IU/0fCc=') t.end() }) -tape('accsign_rsa', function(t) { - accsign_rsa = rsasign('POST', 'https://api.twitter.com/oauth/access_token', - { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , oauth_signature_method: 'RSA-SHA1' - , oauth_token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , oauth_timestamp: '1272323047' - , oauth_verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , oauth_version: '1.0' - }, rsa_private_pem) - - t.equal(accsign_rsa, 'gZrMPexdgGMVUl9H6RxK0MbR6Db8tzf2kIIj52kOrDFcMgh4BunMBgUZAO1msUwz6oqZIvkVqyfyDAOP2wIrpYem0mBg3vqwPIroSE1AlUWo+TtQxOTuqrU+3kDcXpdvJe7CAX5hUx9Np/iGRqaCcgByqb9DaCcQ9ViQ+0wJiXI=') +tape('accsignRSA', function (t) { + accsignRSA = rsasign('POST', 'https://api.twitter.com/oauth/access_token', + { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + oauth_signature_method: 'RSA-SHA1', + oauth_token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + oauth_timestamp: '1272323047', + oauth_verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + oauth_version: '1.0' + }, rsaPrivatePEM) + + t.equal(accsignRSA, 'gZrMPexdgGMVUl9H6RxK0MbR6Db8tzf2kIIj52kOrDFcMgh4BunMBgUZAO1msUwz6oqZIvkVqyfyDAOP2wIrpYem0mBg3vqwPIroSE1AlUWo+TtQxOTuqrU+3kDcXpdvJe7CAX5hUx9Np/iGRqaCcgByqb9DaCcQ9ViQ+0wJiXI=') t.end() }) -tape('upsign', function(t) { +tape('upsign', function (t) { upsign = hmacsign('POST', 'http://api.twitter.com/1/statuses/update.json', - { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y' - , oauth_signature_method: 'HMAC-SHA1' - , oauth_token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw' - , oauth_timestamp: '1272325550' - , oauth_version: '1.0' - , status: 'setting up my twitter 私のさえずりを設定する' + { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y', + oauth_signature_method: 'HMAC-SHA1', + oauth_token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw', + oauth_timestamp: '1272325550', + oauth_version: '1.0', + status: 'setting up my twitter 私のさえずりを設定する' }, 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', 'J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA') t.equal(upsign, 'yOahq5m0YjDDjfjxHaXEsW9D+X0=') t.end() }) -tape('upsign_rsa', function(t) { - upsign_rsa = rsasign('POST', 'http://api.twitter.com/1/statuses/update.json', - { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g' - , oauth_nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y' - , oauth_signature_method: 'RSA-SHA1' - , oauth_token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw' - , oauth_timestamp: '1272325550' - , oauth_version: '1.0' - , status: 'setting up my twitter 私のさえずりを設定する' - }, rsa_private_pem) - - t.equal(upsign_rsa, 'fF4G9BNzDxPu/htctzh9CWzGhtXo9DYYl+ZyRO1/QNOhOZPqnWVUOT+CGUKxmAeJSzLKMAH8y/MFSHI0lzihqwgfZr7nUhTx6kH7lUChcVasr+TZ4qPqvGGEhfJ8Av8D5dF5fytfCSzct62uONU0iHYVqainP+zefk1K7Ptb6hI=') +tape('upsignRSA', function (t) { + upsignRSA = rsasign('POST', 'http://api.twitter.com/1/statuses/update.json', + { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', + oauth_nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y', + oauth_signature_method: 'RSA-SHA1', + oauth_token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw', + oauth_timestamp: '1272325550', + oauth_version: '1.0', + status: 'setting up my twitter 私のさえずりを設定する' + }, rsaPrivatePEM) + + t.equal(upsignRSA, 'fF4G9BNzDxPu/htctzh9CWzGhtXo9DYYl+ZyRO1/QNOhOZPqnWVUOT+CGUKxmAeJSzLKMAH8y/MFSHI0lzihqwgfZr7nUhTx6kH7lUChcVasr+TZ4qPqvGGEhfJ8Av8D5dF5fytfCSzct62uONU0iHYVqainP+zefk1K7Ptb6hI=') t.end() }) -tape('rsign', function(t) { +tape('rsign', function (t) { var rsign = request.post( - { url: 'https://api.twitter.com/oauth/request_token' - , oauth: - { callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11' - , consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk' - , timestamp: '1272323042' - , version: '1.0' - , consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' + { url: 'https://api.twitter.com/oauth/request_token', + oauth: { callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11', + consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', + timestamp: '1272323042', + version: '1.0', + consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' } }) - process.nextTick(function() { + process.nextTick(function () { t.equal(reqsign, getSignature(rsign)) rsign.abort() t.end() }) }) -tape('rsign_rsa', function(t) { - var rsign_rsa = request.post( - { url: 'https://api.twitter.com/oauth/request_token' - , oauth: - { callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11' - , consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk' - , timestamp: '1272323042' - , version: '1.0' - , private_key: rsa_private_pem - , signature_method: 'RSA-SHA1' +tape('rsign_rsa', function (t) { + var rsignRSA = request.post( + { url: 'https://api.twitter.com/oauth/request_token', + oauth: { callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11', + consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', + timestamp: '1272323042', + version: '1.0', + private_key: rsaPrivatePEM, + signature_method: 'RSA-SHA1' } }) - process.nextTick(function() { - t.equal(reqsign_rsa, getSignature(rsign_rsa)) - rsign_rsa.abort() + process.nextTick(function () { + t.equal(reqsignRSA, getSignature(rsignRSA)) + rsignRSA.abort() t.end() }) }) -tape('raccsign', function(t) { +tape('raccsign', function (t) { var raccsign = request.post( - { url: 'https://api.twitter.com/oauth/access_token' - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , signature_method: 'HMAC-SHA1' - , token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , timestamp: '1272323047' - , verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , version: '1.0' - , consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' - , token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' + { url: 'https://api.twitter.com/oauth/access_token', + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + signature_method: 'HMAC-SHA1', + token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + timestamp: '1272323047', + verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + version: '1.0', + consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', + token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' } }) - process.nextTick(function() { + process.nextTick(function () { t.equal(accsign, getSignature(raccsign)) raccsign.abort() t.end() }) }) -tape('raccsign_rsa', function(t) { - var raccsign_rsa = request.post( - { url: 'https://api.twitter.com/oauth/access_token' - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , signature_method: 'RSA-SHA1' - , token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , timestamp: '1272323047' - , verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , version: '1.0' - , private_key: rsa_private_pem - , token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' +tape('raccsignRSA', function (t) { + var raccsignRSA = request.post( + { url: 'https://api.twitter.com/oauth/access_token', + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + signature_method: 'RSA-SHA1', + token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + timestamp: '1272323047', + verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + version: '1.0', + private_key: rsaPrivatePEM, + token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' } }) - process.nextTick(function() { - t.equal(accsign_rsa, getSignature(raccsign_rsa)) - raccsign_rsa.abort() + process.nextTick(function () { + t.equal(accsignRSA, getSignature(raccsignRSA)) + raccsignRSA.abort() t.end() }) }) -tape('rupsign', function(t) { +tape('rupsign', function (t) { var rupsign = request.post( - { url: 'http://api.twitter.com/1/statuses/update.json' - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y' - , signature_method: 'HMAC-SHA1' - , token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw' - , timestamp: '1272325550' - , version: '1.0' - , consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' - , token_secret: 'J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA' - } - , form: {status: 'setting up my twitter 私のさえずりを設定する'} + { url: 'http://api.twitter.com/1/statuses/update.json', + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y', + signature_method: 'HMAC-SHA1', + token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw', + timestamp: '1272325550', + version: '1.0', + consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', + token_secret: 'J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA' + }, + form: {status: 'setting up my twitter 私のさえずりを設定する'} }) - process.nextTick(function() { + process.nextTick(function () { t.equal(upsign, getSignature(rupsign)) rupsign.abort() t.end() }) }) -tape('rupsign_rsa', function(t) { - var rupsign_rsa = request.post( - { url: 'http://api.twitter.com/1/statuses/update.json' - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y' - , signature_method: 'RSA-SHA1' - , token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw' - , timestamp: '1272325550' - , version: '1.0' - , private_key: rsa_private_pem - , token_secret: 'J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA' - } - , form: {status: 'setting up my twitter 私のさえずりを設定する'} +tape('rupsignRSA', function (t) { + var rupsignRSA = request.post( + { url: 'http://api.twitter.com/1/statuses/update.json', + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: 'oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y', + signature_method: 'RSA-SHA1', + token: '819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw', + timestamp: '1272325550', + version: '1.0', + private_key: rsaPrivatePEM, + token_secret: 'J6zix3FfA9LofH0awS24M3HcBYXO5nI1iYe8EfBA' + }, + form: {status: 'setting up my twitter 私のさえずりを設定する'} }) - process.nextTick(function() { - t.equal(upsign_rsa, getSignature(rupsign_rsa)) - rupsign_rsa.abort() + process.nextTick(function () { + t.equal(upsignRSA, getSignature(rupsignRSA)) + rupsignRSA.abort() t.end() }) }) -tape('rfc5849 example', function(t) { +tape('rfc5849 example', function (t) { var rfc5849 = request.post( - { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b' - , oauth: - { consumer_key: '9djdj82h48djs9d2' - , nonce: '7d8f3e4a' - , signature_method: 'HMAC-SHA1' - , token: 'kkk9d7dh3k39sjv7' - , timestamp: '137131201' - , consumer_secret: 'j49sk3j29djd' - , token_secret: 'dh893hdasih9' - , realm: 'Example' - } - , form: { - c2: '', - a3: '2 q' - } + { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b', + oauth: { consumer_key: '9djdj82h48djs9d2', + nonce: '7d8f3e4a', + signature_method: 'HMAC-SHA1', + token: 'kkk9d7dh3k39sjv7', + timestamp: '137131201', + consumer_secret: 'j49sk3j29djd', + token_secret: 'dh893hdasih9', + realm: 'Example' + }, + form: { + c2: '', + a3: '2 q' + } }) - process.nextTick(function() { + process.nextTick(function () { // different signature in rfc5849 because request sets oauth_version by default t.equal('OB33pYjWAnf+xtOHN4Gmbdil168=', getSignature(rfc5849)) rfc5849.abort() @@ -277,110 +270,104 @@ tape('rfc5849 example', function(t) { }) }) -tape('rfc5849 RSA example', function(t) { - var rfc5849_rsa = request.post( - { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b' - , oauth: - { consumer_key: '9djdj82h48djs9d2' - , nonce: '7d8f3e4a' - , signature_method: 'RSA-SHA1' - , token: 'kkk9d7dh3k39sjv7' - , timestamp: '137131201' - , private_key: rsa_private_pem - , token_secret: 'dh893hdasih9' - , realm: 'Example' +tape('rfc5849 RSA example', function (t) { + var rfc5849RSA = request.post( + { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b', + oauth: { consumer_key: '9djdj82h48djs9d2', + nonce: '7d8f3e4a', + signature_method: 'RSA-SHA1', + token: 'kkk9d7dh3k39sjv7', + timestamp: '137131201', + private_key: rsaPrivatePEM, + token_secret: 'dh893hdasih9', + realm: 'Example' + }, + form: { + c2: '', + a3: '2 q' } - , form: { - c2: '', - a3: '2 q' - } }) - process.nextTick(function() { + process.nextTick(function () { // different signature in rfc5849 because request sets oauth_version by default - t.equal('ThNYfZhYogcAU6rWgI3ZFlPEhoIXHMZcuMzl+jykJZW/ab+AxyefS03dyd64CclIZ0u8JEW64TQ5SHthoQS8aM8qir4t+t88lRF3LDkD2KtS1krgCZTUQxkDL5BO5pxsqAQ2Zfdcrzaxb6VMGD1Hf+Pno+fsHQo/UUKjq4V3RMo=', getSignature(rfc5849_rsa)) - rfc5849_rsa.abort() + t.equal('ThNYfZhYogcAU6rWgI3ZFlPEhoIXHMZcuMzl+jykJZW/ab+AxyefS03dyd64CclIZ0u8JEW64TQ5SHthoQS8aM8qir4t+t88lRF3LDkD2KtS1krgCZTUQxkDL5BO5pxsqAQ2Zfdcrzaxb6VMGD1Hf+Pno+fsHQo/UUKjq4V3RMo=', getSignature(rfc5849RSA)) + rfc5849RSA.abort() t.end() }) }) -tape('plaintext signature method', function(t) { +tape('plaintext signature method', function (t) { var plaintext = request.post( - { url: 'https://dummy.com' - , oauth: - { consumer_secret: 'consumer_secret' - , token_secret: 'token_secret' - , signature_method: 'PLAINTEXT' + { url: 'https://dummy.com', + oauth: { consumer_secret: 'consumer_secret', + token_secret: 'token_secret', + signature_method: 'PLAINTEXT' } }) - process.nextTick(function() { + process.nextTick(function () { t.equal('consumer_secret&token_secret', getSignature(plaintext)) plaintext.abort() t.end() }) }) -tape('invalid transport_method', function(t) { +tape('invalid transport_method', function (t) { t.throws( function () { request.post( - { url: 'http://example.com/' - , oauth: - { transport_method: 'headerquery' - } - }) + { url: 'http://example.com/', + oauth: { transport_method: 'headerquery' + } + }) }, /transport_method invalid/) t.end() }) -tape('invalid method while using transport_method \'body\'', function(t) { +tape("invalid method while using transport_method 'body'", function (t) { t.throws( function () { request.get( - { url: 'http://example.com/' - , headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } - , oauth: - { transport_method: 'body' - } - }) + { url: 'http://example.com/', + headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' }, + oauth: { transport_method: 'body' + } + }) }, /requires POST/) t.end() }) -tape('invalid content-type while using transport_method \'body\'', function(t) { +tape("invalid content-type while using transport_method 'body'", function (t) { t.throws( function () { request.post( - { url: 'http://example.com/' - , headers: { 'content-type': 'application/json; charset=UTF-8' } - , oauth: - { transport_method: 'body' - } - }) + { url: 'http://example.com/', + headers: { 'content-type': 'application/json; charset=UTF-8' }, + oauth: { transport_method: 'body' + } + }) }, /requires POST/) t.end() }) -tape('query transport_method', function(t) { +tape('query transport_method', function (t) { var r = request.post( - { url: 'https://api.twitter.com/oauth/access_token' - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , signature_method: 'HMAC-SHA1' - , token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , timestamp: '1272323047' - , verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , version: '1.0' - , consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' - , token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' - , transport_method: 'query' + { url: 'https://api.twitter.com/oauth/access_token', + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + signature_method: 'HMAC-SHA1', + token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + timestamp: '1272323047', + verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + version: '1.0', + consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', + token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA', + transport_method: 'query' } }) - process.nextTick(function() { - t.notOk(r.headers.Authorization, 'oauth Authorization header should not be present with transport_method \'query\'') + process.nextTick(function () { + t.notOk(r.headers.Authorization, "oauth Authorization header should not be present with transport_method 'query'") t.equal(r.uri.path, r.path, 'r.uri.path should equal r.path') t.ok(r.path.match(/^\/oauth\/access_token\?/), 'path should contain path + query') t.deepEqual(qs.parse(r.uri.query), @@ -397,28 +384,27 @@ tape('query transport_method', function(t) { }) }) -tape('query transport_method + form option + url params', function(t) { +tape('query transport_method + form option + url params', function (t) { var r = request.post( - { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b' - , oauth: - { consumer_key: '9djdj82h48djs9d2' - , nonce: '7d8f3e4a' - , signature_method: 'HMAC-SHA1' - , token: 'kkk9d7dh3k39sjv7' - , timestamp: '137131201' - , consumer_secret: 'j49sk3j29djd' - , token_secret: 'dh893hdasih9' - , realm: 'Example' - , transport_method: 'query' + { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b', + oauth: { consumer_key: '9djdj82h48djs9d2', + nonce: '7d8f3e4a', + signature_method: 'HMAC-SHA1', + token: 'kkk9d7dh3k39sjv7', + timestamp: '137131201', + consumer_secret: 'j49sk3j29djd', + token_secret: 'dh893hdasih9', + realm: 'Example', + transport_method: 'query' + }, + form: { + c2: '', + a3: '2 q' } - , form: { - c2: '', - a3: '2 q' - } }) - process.nextTick(function() { - t.notOk(r.headers.Authorization, 'oauth Authorization header should not be present with transport_method \'query\'') + process.nextTick(function () { + t.notOk(r.headers.Authorization, "oauth Authorization header should not be present with transport_method 'query'") t.equal(r.uri.path, r.path, 'r.uri.path should equal r.path') t.ok(r.path.match(/^\/request\?/), 'path should contain path + query') t.deepEqual(qs.parse(r.uri.query), @@ -439,30 +425,29 @@ tape('query transport_method + form option + url params', function(t) { }) }) -tape('query transport_method + qs option + url params', function(t) { +tape('query transport_method + qs option + url params', function (t) { var r = request.post( - { url: 'http://example.com/request?a2=r%20b' - , oauth: - { consumer_key: '9djdj82h48djs9d2' - , nonce: '7d8f3e4a' - , signature_method: 'HMAC-SHA1' - , token: 'kkk9d7dh3k39sjv7' - , timestamp: '137131201' - , consumer_secret: 'j49sk3j29djd' - , token_secret: 'dh893hdasih9' - , realm: 'Example' - , transport_method: 'query' + { url: 'http://example.com/request?a2=r%20b', + oauth: { consumer_key: '9djdj82h48djs9d2', + nonce: '7d8f3e4a', + signature_method: 'HMAC-SHA1', + token: 'kkk9d7dh3k39sjv7', + timestamp: '137131201', + consumer_secret: 'j49sk3j29djd', + token_secret: 'dh893hdasih9', + realm: 'Example', + transport_method: 'query' + }, + qs: { + b5: '=%3D', + a3: ['a', '2 q'], + 'c@': '', + c2: '' } - , qs: { - b5: '=%3D', - a3: ['a', '2 q'], - 'c@': '', - c2: '' - } - }) + }) - process.nextTick(function() { - t.notOk(r.headers.Authorization, 'oauth Authorization header should not be present with transport_method \'query\'') + process.nextTick(function () { + t.notOk(r.headers.Authorization, "oauth Authorization header should not be present with transport_method 'query'") t.equal(r.uri.path, r.path, 'r.uri.path should equal r.path') t.ok(r.path.match(/^\/request\?/), 'path should contain path + query') t.deepEqual(qs.parse(r.uri.query), @@ -485,26 +470,25 @@ tape('query transport_method + qs option + url params', function(t) { }) }) -tape('body transport_method', function(t) { +tape('body transport_method', function (t) { var r = request.post( - { url: 'https://api.twitter.com/oauth/access_token' - , headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' } - , oauth: - { consumer_key: 'GDdmIQH6jhtmLUypg82g' - , nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8' - , signature_method: 'HMAC-SHA1' - , token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc' - , timestamp: '1272323047' - , verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY' - , version: '1.0' - , consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' - , token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA' - , transport_method: 'body' + { url: 'https://api.twitter.com/oauth/access_token', + headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' }, + oauth: { consumer_key: 'GDdmIQH6jhtmLUypg82g', + nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', + signature_method: 'HMAC-SHA1', + token: '8ldIZyxQeVrFZXFOZH5tAwj6vzJYuLQpl0WUEYtWc', + timestamp: '1272323047', + verifier: 'pDNg57prOHapMbhv25RNf75lVRd6JDsni1AJJIDYoTY', + version: '1.0', + consumer_secret: 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98', + token_secret: 'x6qpRnlEmW9JbQn4PQVVeVG8ZLPEx6A0TOebgwcuA', + transport_method: 'body' } }) - process.nextTick(function() { - t.notOk(r.headers.Authorization, 'oauth Authorization header should not be present with transport_method \'body\'') + process.nextTick(function () { + t.notOk(r.headers.Authorization, "oauth Authorization header should not be present with transport_method 'body'") t.deepEqual(qs.parse(r.body), { oauth_consumer_key: 'GDdmIQH6jhtmLUypg82g', oauth_nonce: '9zWH6qe0qG7Lc1telCn7FhUbLyVdjEaL3MO5uHxn8', @@ -519,28 +503,27 @@ tape('body transport_method', function(t) { }) }) -tape('body transport_method + form option + url params', function(t) { +tape('body transport_method + form option + url params', function (t) { var r = request.post( - { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b' - , oauth: - { consumer_key: '9djdj82h48djs9d2' - , nonce: '7d8f3e4a' - , signature_method: 'HMAC-SHA1' - , token: 'kkk9d7dh3k39sjv7' - , timestamp: '137131201' - , consumer_secret: 'j49sk3j29djd' - , token_secret: 'dh893hdasih9' - , realm: 'Example' - , transport_method: 'body' + { url: 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b', + oauth: { consumer_key: '9djdj82h48djs9d2', + nonce: '7d8f3e4a', + signature_method: 'HMAC-SHA1', + token: 'kkk9d7dh3k39sjv7', + timestamp: '137131201', + consumer_secret: 'j49sk3j29djd', + token_secret: 'dh893hdasih9', + realm: 'Example', + transport_method: 'body' + }, + form: { + c2: '', + a3: '2 q' } - , form: { - c2: '', - a3: '2 q' - } }) - process.nextTick(function() { - t.notOk(r.headers.Authorization, 'oauth Authorization header should not be present with transport_method \'body\'') + process.nextTick(function () { + t.notOk(r.headers.Authorization, "oauth Authorization header should not be present with transport_method 'body'") t.deepEqual(qs.parse(r.body), { c2: '', a3: '2 q', @@ -557,7 +540,7 @@ tape('body transport_method + form option + url params', function(t) { }) }) -tape('body_hash manual built', function(t) { +tape('body_hash manual built', function (t) { function buildBodyHash (body) { var shasum = crypto.createHash('sha1') shasum.update(body || '') @@ -567,81 +550,79 @@ tape('body_hash manual built', function(t) { var json = {foo: 'bar'} var r = request.post( - { url: 'http://example.com' - , oauth: - { consumer_secret: 'consumer_secret' - , body_hash: buildBodyHash(JSON.stringify(json)) - } - , json: json + { url: 'http://example.com', + oauth: { consumer_secret: 'consumer_secret', + body_hash: buildBodyHash(JSON.stringify(json)) + }, + json: json }) - process.nextTick(function() { - var body_hash = r.headers.Authorization.replace(/.*oauth_body_hash="([^"]+)".*/, '$1') - t.equal('YTVlNzQ0ZDAxNjQ1NDBkMzNiMWQ3ZWE2MTZjMjhmMmZhOTdlNzU0YQ%3D%3D', body_hash) + process.nextTick(function () { + var hash = r.headers.Authorization.replace(/.*oauth_body_hash="([^"]+)".*/, '$1') + t.equal('YTVlNzQ0ZDAxNjQ1NDBkMzNiMWQ3ZWE2MTZjMjhmMmZhOTdlNzU0YQ%3D%3D', hash) r.abort() t.end() }) }) -tape('body_hash automatic built', function(t) { +tape('body_hash automatic built', function (t) { var r = request.post( - { url: 'http://example.com' - , oauth: - { consumer_secret: 'consumer_secret' - , body_hash: true - } - , json: {foo: 'bar'} + { url: 'http://example.com', + oauth: { consumer_secret: 'consumer_secret', + body_hash: true + }, + json: {foo: 'bar'} }) - process.nextTick(function() { - var body_hash = r.headers.Authorization.replace(/.*oauth_body_hash="([^"]+)".*/, '$1') - t.equal('YTVlNzQ0ZDAxNjQ1NDBkMzNiMWQ3ZWE2MTZjMjhmMmZhOTdlNzU0YQ%3D%3D', body_hash) + process.nextTick(function () { + var hash = r.headers.Authorization.replace(/.*oauth_body_hash="([^"]+)".*/, '$1') + t.equal('YTVlNzQ0ZDAxNjQ1NDBkMzNiMWQ3ZWE2MTZjMjhmMmZhOTdlNzU0YQ%3D%3D', hash) r.abort() t.end() }) }) -tape('body_hash PLAINTEXT signature_method', function(t) { - t.throws(function() { +tape('body_hash PLAINTEXT signature_method', function (t) { + t.throws(function () { request.post( - { url: 'http://example.com' - , oauth: - { consumer_secret: 'consumer_secret' - , body_hash: true - , signature_method: 'PLAINTEXT' - } - , json: {foo: 'bar'} - }) + { url: 'http://example.com', + oauth: { consumer_secret: 'consumer_secret', + body_hash: true, + signature_method: 'PLAINTEXT' + }, + json: {foo: 'bar'} + }) }, /oauth: PLAINTEXT signature_method not supported with body_hash signing/) t.end() }) -tape('refresh oauth_nonce on redirect', function(t) { - var oauth_nonce1, oauth_nonce2, url +tape('refresh oauth_nonce on redirect', function (t) { + var oauthNonce1 + var oauthNonce2 + var url var s = http.createServer(function (req, res) { if (req.url === '/redirect') { - oauth_nonce1 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1') - res.writeHead(302, {location:url + '/response'}) + oauthNonce1 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1') + res.writeHead(302, {location: url + '/response'}) res.end() } else if (req.url === '/response') { - oauth_nonce2 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1') - res.writeHead(200, {'content-type':'text/plain'}) + oauthNonce2 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1') + res.writeHead(200, {'content-type': 'text/plain'}) res.end() } }) s.listen(0, function () { url = 'http://localhost:' + this.address().port request.get( - { url: url + '/redirect' - , oauth: - { consumer_key: 'consumer_key' - , consumer_secret: 'consumer_secret' - , token: 'token' - , token_secret: 'token_secret' + { url: url + '/redirect', + oauth: { consumer_key: 'consumer_key', + consumer_secret: 'consumer_secret', + token: 'token', + token_secret: 'token_secret' } }, function (err, res, body) { t.equal(err, null) - t.notEqual(oauth_nonce1, oauth_nonce2) + t.notEqual(oauthNonce1, oauthNonce2) s.close(function () { t.end() }) @@ -649,13 +630,13 @@ tape('refresh oauth_nonce on redirect', function(t) { }) }) -tape('no credentials on external redirect', function(t) { +tape('no credentials on external redirect', function (t) { var s2 = http.createServer(function (req, res) { - res.writeHead(200, {'content-type':'text/plain'}) + res.writeHead(200, {'content-type': 'text/plain'}) res.end() }) var s1 = http.createServer(function (req, res) { - res.writeHead(302, {location:s2.url}) + res.writeHead(302, {location: s2.url}) res.end() }) s1.listen(0, function () { @@ -663,12 +644,11 @@ tape('no credentials on external redirect', function(t) { s2.listen(0, function () { s2.url = 'http://127.0.0.1:' + this.address().port request.get( - { url: s1.url - , oauth: - { consumer_key: 'consumer_key' - , consumer_secret: 'consumer_secret' - , token: 'token' - , token_secret: 'token_secret' + { url: s1.url, + oauth: { consumer_key: 'consumer_key', + consumer_secret: 'consumer_secret', + token: 'token', + token_secret: 'token_secret' } }, function (err, res, body) { t.equal(err, null) diff --git a/tests/test-onelineproxy.js b/tests/test-onelineproxy.js index 5732f0512..b2219f246 100644 --- a/tests/test-onelineproxy.js +++ b/tests/test-onelineproxy.js @@ -1,11 +1,11 @@ 'use strict' var http = require('http') - , assert = require('assert') - , request = require('../index') - , tape = require('tape') +var assert = require('assert') +var request = require('../index') +var tape = require('tape') -var server = http.createServer(function(req, resp) { +var server = http.createServer(function (req, resp) { resp.statusCode = 200 if (req.url === '/get') { assert.equal(req.method, 'GET') @@ -16,10 +16,10 @@ var server = http.createServer(function(req, resp) { if (req.url === '/put') { var x = '' assert.equal(req.method, 'PUT') - req.on('data', function(chunk) { + req.on('data', function (chunk) { x += chunk }) - req.on('end', function() { + req.on('end', function () { assert.equal(x, 'content') resp.write('success') resp.end() @@ -38,15 +38,15 @@ var server = http.createServer(function(req, resp) { throw new Error('Unknown url', req.url) }) -tape('setup', function(t) { - server.listen(0, function() { +tape('setup', function (t) { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('chained one-line proxying', function(t) { - request(server.url + '/test', function(err, res, body) { +tape('chained one-line proxying', function (t) { + request(server.url + '/test', function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'success') @@ -54,8 +54,8 @@ tape('chained one-line proxying', function(t) { }) }) -tape('cleanup', function(t) { - server.close(function() { +tape('cleanup', function (t) { + server.close(function () { t.end() }) }) diff --git a/tests/test-option-reuse.js b/tests/test-option-reuse.js index 706b121a9..1c9b09d64 100644 --- a/tests/test-option-reuse.js +++ b/tests/test-option-reuse.js @@ -1,39 +1,39 @@ 'use strict' var request = require('../index') - , http = require('http') - , tape = require('tape') +var http = require('http') +var tape = require('tape') var methodsSeen = { head: 0, get: 0 } -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { res.statusCode = 200 res.end('ok') methodsSeen[req.method.toLowerCase()]++ }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('options object is not mutated', function(t) { +tape('options object is not mutated', function (t) { var url = s.url var options = { url: url } - request.head(options, function(err, resp, body) { + request.head(options, function (err, resp, body) { t.equal(err, null) t.equal(body, '') t.equal(Object.keys(options).length, 1) t.equal(options.url, url) - request.get(options, function(err, resp, body) { + request.get(options, function (err, resp, body) { t.equal(err, null) t.equal(body, 'ok') t.equal(Object.keys(options).length, 1) @@ -47,8 +47,8 @@ tape('options object is not mutated', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-params.js b/tests/test-params.js index 6cd0a99f2..5b7880dd4 100644 --- a/tests/test-params.js +++ b/tests/test-params.js @@ -1,15 +1,15 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var s = server.createServer() -function runTest(name, test) { - tape(name, function(t) { +function runTest (name, test) { + tape(name, function (t) { s.on('/' + name, test.resp) - request(s.url + '/' + name, test, function(err, resp, body) { + request(s.url + '/' + name, test, function (err, resp, body) { t.equal(err, null) if (test.expectBody) { if (Buffer.isBuffer(test.expectBody)) { @@ -23,59 +23,59 @@ function runTest(name, test) { }) } -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) runTest('testGet', { - resp : server.createGetResponse('TESTING!') - , expectBody: 'TESTING!' + resp: server.createGetResponse('TESTING!'), + expectBody: 'TESTING!' }) runTest('testGetChunkBreak', { - resp : server.createChunkResponse( - [ new Buffer([239]) - , new Buffer([163]) - , new Buffer([191]) - , new Buffer([206]) - , new Buffer([169]) - , new Buffer([226]) - , new Buffer([152]) - , new Buffer([131]) - ]) - , expectBody: '\uf8ff\u03a9\u2603' + resp: server.createChunkResponse( + [ new Buffer([239]), + new Buffer([163]), + new Buffer([191]), + new Buffer([206]), + new Buffer([169]), + new Buffer([226]), + new Buffer([152]), + new Buffer([131]) + ]), + expectBody: '\uf8ff\u03a9\u2603' }) runTest('testGetBuffer', { - resp : server.createGetResponse(new Buffer('TESTING!')) - , encoding: null - , expectBody: new Buffer('TESTING!') + resp: server.createGetResponse(new Buffer('TESTING!')), + encoding: null, + expectBody: new Buffer('TESTING!') }) runTest('testGetJSON', { - resp : server.createGetResponse('{"test":true}', 'application/json') - , json : true - , expectBody: {'test':true} + resp: server.createGetResponse('{"test":true}', 'application/json'), + json: true, + expectBody: {'test': true} }) runTest('testPutString', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : 'PUTTINGDATA' + resp: server.createPostValidator('PUTTINGDATA'), + method: 'PUT', + body: 'PUTTINGDATA' }) runTest('testPutBuffer', { - resp : server.createPostValidator('PUTTINGDATA') - , method : 'PUT' - , body : new Buffer('PUTTINGDATA') + resp: server.createPostValidator('PUTTINGDATA'), + method: 'PUT', + body: new Buffer('PUTTINGDATA') }) runTest('testPutJSON', { - resp : server.createPostValidator(JSON.stringify({foo: 'bar'})) - , method: 'PUT' - , json: {foo: 'bar'} + resp: server.createPostValidator(JSON.stringify({foo: 'bar'})), + method: 'PUT', + json: {foo: 'bar'} }) runTest('testPutMultipart', { @@ -87,16 +87,15 @@ runTest('testPutMultipart', { '\r\n--__BOUNDARY__\r\n\r\n' + 'Oh hi.' + '\r\n--__BOUNDARY__--' - ) - , method: 'PUT' - , multipart: - [ {'content-type': 'text/html', 'body': 'Oh hi.'} - , {'body': 'Oh hi.'} - ] + ), + method: 'PUT', + multipart: [ {'content-type': 'text/html', 'body': 'Oh hi.'}, + {'body': 'Oh hi.'} + ] }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-piped-redirect.js b/tests/test-piped-redirect.js index d669c0923..77135d4d1 100644 --- a/tests/test-piped-redirect.js +++ b/tests/test-piped-redirect.js @@ -1,13 +1,13 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var port1 - , port2 +var port2 -var s1 = http.createServer(function(req, resp) { +var s1 = http.createServer(function (req, resp) { if (req.url === '/original') { resp.writeHeader(302, { 'location': '/redirected' @@ -22,33 +22,33 @@ var s1 = http.createServer(function(req, resp) { } }) -var s2 = http.createServer(function(req, resp) { +var s2 = http.createServer(function (req, resp) { var x = request('http://localhost:' + port1 + '/original') req.pipe(x) x.pipe(resp) }) -tape('setup', function(t) { - s1.listen(0, function() { +tape('setup', function (t) { + s1.listen(0, function () { port1 = this.address().port - s2.listen(0, function() { + s2.listen(0, function () { port2 = this.address().port t.end() }) }) }) -tape('piped redirect', function(t) { - request('http://localhost:' + port2 + '/original', function(err, res, body) { +tape('piped redirect', function (t) { + request('http://localhost:' + port2 + '/original', function (err, res, body) { t.equal(err, null) t.equal(body, 'OK') t.end() }) }) -tape('cleanup', function(t) { - s1.close(function() { - s2.close(function() { +tape('cleanup', function (t) { + s1.close(function () { + s2.close(function () { t.end() }) }) diff --git a/tests/test-pipes.js b/tests/test-pipes.js index f1498e17d..0ee3dd6c8 100644 --- a/tests/test-pipes.js +++ b/tests/test-pipes.js @@ -1,16 +1,16 @@ 'use strict' var server = require('./server') - , stream = require('stream') - , fs = require('fs') - , request = require('../index') - , path = require('path') - , util = require('util') - , tape = require('tape') +var stream = require('stream') +var fs = require('fs') +var request = require('../index') +var path = require('path') +var util = require('util') +var tape = require('tape') var s = server.createServer() -s.on('/cat', function(req, res) { +s.on('/cat', function (req, res) { if (req.method === 'GET') { res.writeHead(200, { 'content-type': 'text/plain-test', @@ -19,9 +19,9 @@ s.on('/cat', function(req, res) { res.end('asdf') } else if (req.method === 'PUT') { var body = '' - req.on('data', function(chunk) { + req.on('data', function (chunk) { body += chunk - }).on('end', function() { + }).on('end', function () { res.writeHead(201) res.end() s.emit('catDone', req, res, body) @@ -29,7 +29,7 @@ s.on('/cat', function(req, res) { } }) -s.on('/doodle', function(req, res) { +s.on('/doodle', function (req, res) { if (req.headers['x-oneline-proxy']) { res.setHeader('x-oneline-proxy', 'yup') } @@ -37,13 +37,13 @@ s.on('/doodle', function(req, res) { fs.createReadStream(path.join(__dirname, 'googledoodle.jpg')).pipe(res) }) -function ValidationStream(t, str) { +function ValidationStream (t, str) { this.str = str this.buf = '' - this.on('data', function(data) { + this.on('data', function (data) { this.buf += data }) - this.on('end', function() { + this.on('end', function () { t.equal(this.str, this.buf) }) this.writable = true @@ -51,25 +51,24 @@ function ValidationStream(t, str) { util.inherits(ValidationStream, stream.Stream) -ValidationStream.prototype.write = function(chunk) { +ValidationStream.prototype.write = function (chunk) { this.emit('data', chunk) } -ValidationStream.prototype.end = function(chunk) { +ValidationStream.prototype.end = function (chunk) { if (chunk) { this.emit('data', chunk) } this.emit('end') } - -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -tape('piping to a request object', function(t) { +tape('piping to a request object', function (t) { s.once('/push', server.createPostValidator('mydata')) var mydata = new stream.Stream() @@ -77,7 +76,7 @@ tape('piping to a request object', function(t) { var r1 = request.put({ url: s.url + '/push' - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'mydata') @@ -89,14 +88,14 @@ tape('piping to a request object', function(t) { mydata.emit('end') }) -tape('piping to a request object with invalid uri', function(t) { +tape('piping to a request object with invalid uri', function (t) { var mybodydata = new stream.Stream() mybodydata.readable = true var r2 = request.put({ url: '/bad-uri', json: true - }, function(err, res, body) { + }, function (err, res, body) { t.ok(err instanceof Error) t.equal(err.message, 'Invalid URI "/bad-uri"') t.end() @@ -107,7 +106,7 @@ tape('piping to a request object with invalid uri', function(t) { mybodydata.emit('end') }) -tape('piping to a request object with a json body', function(t) { +tape('piping to a request object with a json body', function (t) { var obj = {foo: 'bar'} var json = JSON.stringify(obj) s.once('/push-json', server.createPostValidator(json, 'application/json')) @@ -117,7 +116,7 @@ tape('piping to a request object with a json body', function(t) { var r2 = request.put({ url: s.url + '/push-json', json: true - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.deepEqual(body, obj) @@ -129,7 +128,7 @@ tape('piping to a request object with a json body', function(t) { mybodydata.emit('end') }) -tape('piping from a request object', function(t) { +tape('piping from a request object', function (t) { s.once('/pull', server.createGetResponse('mypulldata')) var mypulldata = new stream.Stream() @@ -141,22 +140,22 @@ tape('piping from a request object', function(t) { var d = '' - mypulldata.write = function(chunk) { + mypulldata.write = function (chunk) { d += chunk } - mypulldata.end = function() { + mypulldata.end = function () { t.equal(d, 'mypulldata') t.end() } }) -tape('pause when piping from a request object', function(t) { - s.once('/chunks', function(req, res) { +tape('pause when piping from a request object', function (t) { + s.once('/chunks', function (req, res) { res.writeHead(200, { 'content-type': 'text/plain' }) res.write('Chunk 1') - setTimeout(function() { res.end('Chunk 2') }, 10) + setTimeout(function () { res.end('Chunk 2') }, 10) }) var chunkNum = 0 @@ -164,7 +163,7 @@ tape('pause when piping from a request object', function(t) { request({ url: s.url + '/chunks' }) - .on('data', function(chunk) { + .on('data', function (chunk) { var self = this t.notOk(paused, 'Only receive data when not paused') @@ -174,7 +173,7 @@ tape('pause when piping from a request object', function(t) { t.equal(chunk.toString(), 'Chunk 1') self.pause() paused = true - setTimeout(function() { + setTimeout(function () { paused = false self.resume() }, 100) @@ -185,8 +184,8 @@ tape('pause when piping from a request object', function(t) { .on('end', t.end.bind(t)) }) -tape('pause before piping from a request object', function(t) { - s.once('/pause-before', function(req, res) { +tape('pause before piping from a request object', function (t) { + s.once('/pause-before', function (req, res) { res.writeHead(200, { 'content-type': 'text/plain' }) @@ -198,22 +197,22 @@ tape('pause before piping from a request object', function(t) { url: s.url + '/pause-before' }) r.pause() - r.on('data', function(data) { + r.on('data', function (data) { t.notOk(paused, 'Only receive data when not paused') t.equal(data.toString(), 'Data') }) r.on('end', t.end.bind(t)) - setTimeout(function() { + setTimeout(function () { paused = false r.resume() }, 100) }) var fileContents = fs.readFileSync(__filename) -function testPipeFromFile(testName, hasContentLength) { - tape(testName, function(t) { - s.once('/pushjs', function(req, res) { +function testPipeFromFile (testName, hasContentLength) { + tape(testName, function (t) { + s.once('/pushjs', function (req, res) { if (req.method === 'PUT') { t.equal(req.headers['content-type'], 'application/javascript') t.equal( @@ -221,10 +220,10 @@ function testPipeFromFile(testName, hasContentLength) { (hasContentLength ? '' + fileContents.length : undefined)) var body = '' req.setEncoding('utf8') - req.on('data', function(data) { + req.on('data', function (data) { body += data }) - req.on('end', function() { + req.on('end', function () { res.end() t.equal(body, fileContents.toString()) t.end() @@ -245,8 +244,8 @@ function testPipeFromFile(testName, hasContentLength) { testPipeFromFile('piping from a file', false) testPipeFromFile('piping from a file with content-length', true) -tape('piping to and from same URL', function(t) { - s.once('catDone', function(req, res, body) { +tape('piping to and from same URL', function (t) { + s.once('catDone', function (req, res, body) { t.equal(req.headers['content-type'], 'text/plain-test') t.equal(req.headers['content-length'], '4') t.equal(body, 'asdf') @@ -256,12 +255,12 @@ tape('piping to and from same URL', function(t) { .pipe(request.put(s.url + '/cat')) }) -tape('piping between urls', function(t) { - s.once('/catresp', function(req, res) { +tape('piping between urls', function (t) { + s.once('/catresp', function (req, res) { request.get(s.url + '/cat').pipe(res) }) - request.get(s.url + '/catresp', function(err, res, body) { + request.get(s.url + '/catresp', function (err, res, body) { t.equal(err, null) t.equal(res.headers['content-type'], 'text/plain-test') t.equal(res.headers['content-length'], '4') @@ -269,12 +268,12 @@ tape('piping between urls', function(t) { }) }) -tape('writing to file', function(t) { +tape('writing to file', function (t) { var doodleWrite = fs.createWriteStream(path.join(__dirname, 'test.jpg')) request.get(s.url + '/doodle').pipe(doodleWrite) - doodleWrite.on('close', function() { + doodleWrite.on('close', function () { t.deepEqual( fs.readFileSync(path.join(__dirname, 'googledoodle.jpg')), fs.readFileSync(path.join(__dirname, 'test.jpg'))) @@ -283,8 +282,8 @@ tape('writing to file', function(t) { }) }) -tape('one-line proxy', function(t) { - s.once('/onelineproxy', function(req, res) { +tape('one-line proxy', function (t) { + s.once('/onelineproxy', function (req, res) { var x = request(s.url + '/doodle') req.pipe(x) x.pipe(res) @@ -293,36 +292,36 @@ tape('one-line proxy', function(t) { request.get({ uri: s.url + '/onelineproxy', headers: { 'x-oneline-proxy': 'nope' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.headers['x-oneline-proxy'], 'yup') t.end() }) }) -tape('piping after response', function(t) { - s.once('/afterresponse', function(req, res) { +tape('piping after response', function (t) { + s.once('/afterresponse', function (req, res) { res.write('d') res.end() }) var rAfterRes = request.post(s.url + '/afterresponse') - rAfterRes.on('response', function() { + rAfterRes.on('response', function () { var v = new ValidationStream(t, 'd') rAfterRes.pipe(v) - v.on('end', function() { + v.on('end', function () { t.end() }) }) }) -tape('piping through a redirect', function(t) { - s.once('/forward1', function(req, res) { +tape('piping through a redirect', function (t) { + s.once('/forward1', function (req, res) { res.writeHead(302, { location: '/forward2' }) res.end() }) - s.once('/forward2', function(req, res) { + s.once('/forward2', function (req, res) { res.writeHead('200', { 'content-type': 'image/png' }) res.write('d') res.end() @@ -332,27 +331,27 @@ tape('piping through a redirect', function(t) { request.get(s.url + '/forward1').pipe(validateForward) - validateForward.on('end', function() { + validateForward.on('end', function () { t.end() }) }) -tape('pipe options', function(t) { +tape('pipe options', function (t) { s.once('/opts', server.createGetResponse('opts response')) var optsStream = new stream.Stream() - , optsData = '' + var optsData = '' optsStream.writable = true - optsStream.write = function(buf) { + optsStream.write = function (buf) { optsData += buf if (optsData === 'opts response') { - setTimeout(function() { + setTimeout(function () { t.end() }, 10) } } - optsStream.end = function() { + optsStream.end = function () { t.fail('end called') } @@ -361,23 +360,23 @@ tape('pipe options', function(t) { }).pipe(optsStream, { end: false }) }) -tape('request.pipefilter is called correctly', function(t) { - s.once('/pipefilter', function(req, res) { +tape('request.pipefilter is called correctly', function (t) { + s.once('/pipefilter', function (req, res) { res.end('d') }) var validatePipeFilter = new ValidationStream(t, 'd') var r3 = request.get(s.url + '/pipefilter') r3.pipe(validatePipeFilter) - r3.pipefilter = function(res, dest) { + r3.pipefilter = function (res, dest) { t.equal(res, r3.response) t.equal(dest, validatePipeFilter) t.end() } }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-pool.js b/tests/test-pool.js index 6c5446936..f2d96bd1f 100644 --- a/tests/test-pool.js +++ b/tests/test-pool.js @@ -1,26 +1,26 @@ 'use strict' var request = require('../index') - , http = require('http') - , tape = require('tape') +var http = require('http') +var tape = require('tape') var s = http.createServer(function (req, res) { res.statusCode = 200 res.end('asdf') }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('pool', function(t) { +tape('pool', function (t) { request({ url: s.url, pool: false - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'asdf') @@ -31,12 +31,12 @@ tape('pool', function(t) { }) }) -tape('forever', function(t) { +tape('forever', function (t) { var r = request({ url: s.url, forever: true, pool: {maxSockets: 1024} - }, function(err, res, body) { + }, function (err, res, body) { // explicitly shut down the agent if (typeof r.agent.destroy === 'function') { r.agent.destroy() @@ -59,7 +59,7 @@ tape('forever', function(t) { }) }) -tape('forever, should use same agent in sequential requests', function(t) { +tape('forever, should use same agent in sequential requests', function (t) { var r = request.defaults({ forever: true }) @@ -77,7 +77,7 @@ tape('forever, should use same agent in sequential requests', function(t) { t.end() }) -tape('forever, should use same agent in sequential requests(with pool.maxSockets)', function(t) { +tape('forever, should use same agent in sequential requests(with pool.maxSockets)', function (t) { var r = request.defaults({ forever: true, pool: {maxSockets: 1024} @@ -97,7 +97,7 @@ tape('forever, should use same agent in sequential requests(with pool.maxSockets t.end() }) -tape('forever, should use same agent in request() and request.verb', function(t) { +tape('forever, should use same agent in request() and request.verb', function (t) { var r = request.defaults({ forever: true, pool: {maxSockets: 1024} @@ -117,7 +117,7 @@ tape('forever, should use same agent in request() and request.verb', function(t) t.end() }) -tape('should use different agent if pool option specified', function(t) { +tape('should use different agent if pool option specified', function (t) { var r = request.defaults({ forever: true, pool: {maxSockets: 1024} @@ -141,8 +141,8 @@ tape('should use different agent if pool option specified', function(t) { t.end() }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-promise.js b/tests/test-promise.js index f5343cf46..028d15fbb 100644 --- a/tests/test-promise.js +++ b/tests/test-promise.js @@ -1,23 +1,23 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - , Promise = require('bluebird') +var request = require('../index') +var tape = require('tape') +var Promise = require('bluebird') -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { res.writeHead(200, {}) res.end('ok') }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('promisify convenience method', function(t) { +tape('promisify convenience method', function (t) { var get = request.get var p = Promise.promisify(get, {multiArgs: true}) p(s.url) @@ -28,7 +28,7 @@ tape('promisify convenience method', function(t) { }) }) -tape('promisify request function', function(t) { +tape('promisify request function', function (t) { var p = Promise.promisify(request, {multiArgs: true}) p(s.url) .spread(function (res, body) { @@ -37,7 +37,7 @@ tape('promisify request function', function(t) { }) }) -tape('promisify all methods', function(t) { +tape('promisify all methods', function (t) { Promise.promisifyAll(request, {multiArgs: true}) request.getAsync(s.url) .spread(function (res, body) { @@ -46,8 +46,8 @@ tape('promisify all methods', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-proxy-connect.js b/tests/test-proxy-connect.js index 084e19fee..06800d00e 100644 --- a/tests/test-proxy-connect.js +++ b/tests/test-proxy-connect.js @@ -1,13 +1,13 @@ 'use strict' var request = require('../index') - , tape = require('tape') +var tape = require('tape') var called = false - , proxiedHost = 'google.com' - , data = '' +var proxiedHost = 'google.com' +var data = '' -var s = require('net').createServer(function(sock) { +var s = require('net').createServer(function (sock) { called = true sock.once('data', function (c) { data += c @@ -26,28 +26,28 @@ var s = require('net').createServer(function(sock) { }) }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('proxy', function(t) { +tape('proxy', function (t) { request({ tunnel: true, url: 'http://' + proxiedHost, proxy: s.url, headers: { - 'Proxy-Authorization' : 'Basic dXNlcjpwYXNz', - 'authorization' : 'Token deadbeef', - 'dont-send-to-proxy' : 'ok', - 'dont-send-to-dest' : 'ok', - 'accept' : 'yo', - 'user-agent' : 'just another foobar' + 'Proxy-Authorization': 'Basic dXNlcjpwYXNz', + 'authorization': 'Token deadbeef', + 'dont-send-to-proxy': 'ok', + 'dont-send-to-dest': 'ok', + 'accept': 'yo', + 'user-agent': 'just another foobar' }, proxyHeaderExclusiveList: ['Dont-send-to-dest'] - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'derp\n') @@ -73,8 +73,8 @@ tape('proxy', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-proxy.js b/tests/test-proxy.js index dd5cefbcb..77cb7a831 100644 --- a/tests/test-proxy.js +++ b/tests/test-proxy.js @@ -1,14 +1,14 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var s = server.createServer() - , currResponseHandler +var currResponseHandler -['http://google.com/', 'https://google.com/'].forEach(function(url) { - s.on(url, function(req, res) { +['http://google.com/', 'https://google.com/'].forEach(function (url) { + s.on(url, function (req, res) { currResponseHandler(req, res) res.writeHeader(200) res.end('ok') @@ -34,9 +34,9 @@ var proxyEnvVars = [ // `responseHandler` should be truthy to indicate that the proxy should be used // for this request, or falsy to indicate that the proxy should not be used for // this request. -function runTest(name, options, responseHandler) { - tape(name, function(t) { - proxyEnvVars.forEach(function(v) { +function runTest (name, options, responseHandler) { + tape(name, function (t) { + proxyEnvVars.forEach(function (v) { delete process.env[v] }) if (options.env) { @@ -47,7 +47,7 @@ function runTest(name, options, responseHandler) { } var called = false - currResponseHandler = function(req, res) { + currResponseHandler = function (req, res) { if (responseHandler) { called = true t.equal(req.headers.host, 'google.com') @@ -60,7 +60,7 @@ function runTest(name, options, responseHandler) { } options.url = options.url || 'http://google.com' - request(options, function(err, res, body) { + request(options, function (err, res, body) { if (responseHandler && !called) { t.fail('proxy response should be called') } @@ -79,190 +79,188 @@ function runTest(name, options, responseHandler) { }) } -function addTests() { +function addTests () { // If the `runTest` function is changed, run the following command and make // sure both of these tests fail: // // TEST_PROXY_HARNESS=y node tests/test-proxy.js if (process.env.TEST_PROXY_HARNESS) { - runTest('should fail with "proxy response should not be called"', { - proxy : s.url + proxy: s.url }, false) runTest('should fail with "proxy response should be called"', { - proxy : null + proxy: null }, true) - } else { // Run the real tests runTest('basic proxy', { - proxy : s.url, - headers : { + proxy: s.url, + headers: { 'proxy-authorization': 'Token Fooblez' } - }, function(t, req, res) { + }, function (t, req, res) { t.equal(req.headers['proxy-authorization'], 'Token Fooblez') }) runTest('proxy auth without uri auth', { - proxy : 'http://user:pass@localhost:' + s.port - }, function(t, req, res) { + proxy: 'http://user:pass@localhost:' + s.port + }, function (t, req, res) { t.equal(req.headers['proxy-authorization'], 'Basic dXNlcjpwYXNz') }) // http: urls and basic proxy settings runTest('HTTP_PROXY environment variable and http: url', { - env : { HTTP_PROXY : s.url } + env: { HTTP_PROXY: s.url } }, true) runTest('http_proxy environment variable and http: url', { - env : { http_proxy : s.url } + env: { http_proxy: s.url } }, true) runTest('HTTPS_PROXY environment variable and http: url', { - env : { HTTPS_PROXY : s.url } + env: { HTTPS_PROXY: s.url } }, false) runTest('https_proxy environment variable and http: url', { - env : { https_proxy : s.url } + env: { https_proxy: s.url } }, false) // https: urls and basic proxy settings runTest('HTTP_PROXY environment variable and https: url', { - env : { HTTP_PROXY : s.url }, - url : 'https://google.com', - tunnel : false, - pool : false + env: { HTTP_PROXY: s.url }, + url: 'https://google.com', + tunnel: false, + pool: false }, true) runTest('http_proxy environment variable and https: url', { - env : { http_proxy : s.url }, - url : 'https://google.com', - tunnel : false + env: { http_proxy: s.url }, + url: 'https://google.com', + tunnel: false }, true) runTest('HTTPS_PROXY environment variable and https: url', { - env : { HTTPS_PROXY : s.url }, - url : 'https://google.com', - tunnel : false + env: { HTTPS_PROXY: s.url }, + url: 'https://google.com', + tunnel: false }, true) runTest('https_proxy environment variable and https: url', { - env : { https_proxy : s.url }, - url : 'https://google.com', - tunnel : false + env: { https_proxy: s.url }, + url: 'https://google.com', + tunnel: false }, true) runTest('multiple environment variables and https: url', { - env : { - HTTPS_PROXY : s.url, - HTTP_PROXY : 'http://localhost:0/' + env: { + HTTPS_PROXY: s.url, + HTTP_PROXY: 'http://localhost:0/' }, - url : 'https://google.com', - tunnel : false + url: 'https://google.com', + tunnel: false }, true) // no_proxy logic runTest('NO_PROXY hostnames are case insensitive', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'GOOGLE.COM' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'GOOGLE.COM' } }, false) runTest('NO_PROXY hostnames are case insensitive 2', { - env : { - http_proxy : s.url, - NO_PROXY : 'GOOGLE.COM' + env: { + http_proxy: s.url, + NO_PROXY: 'GOOGLE.COM' } }, false) runTest('NO_PROXY hostnames are case insensitive 3', { - env : { - HTTP_PROXY : s.url, - no_proxy : 'GOOGLE.COM' + env: { + HTTP_PROXY: s.url, + no_proxy: 'GOOGLE.COM' } }, false) runTest('NO_PROXY ignored with explicit proxy passed', { - env : { NO_PROXY : '*' }, - proxy : s.url + env: { NO_PROXY: '*' }, + proxy: s.url }, true) runTest('NO_PROXY overrides HTTP_PROXY for specific hostname', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'google.com' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'google.com' } }, false) runTest('no_proxy overrides HTTP_PROXY for specific hostname', { - env : { - HTTP_PROXY : s.url, - no_proxy : 'google.com' + env: { + HTTP_PROXY: s.url, + no_proxy: 'google.com' } }, false) runTest('NO_PROXY does not override HTTP_PROXY if no hostnames match', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'foo.bar,bar.foo' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'foo.bar,bar.foo' } }, true) runTest('NO_PROXY overrides HTTP_PROXY if a hostname matches', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'foo.bar,google.com' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'foo.bar,google.com' } }, false) runTest('NO_PROXY allows an explicit port', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'google.com:80' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'google.com:80' } }, false) runTest('NO_PROXY only overrides HTTP_PROXY if the port matches', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'google.com:1234' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'google.com:1234' } }, true) runTest('NO_PROXY=* should override HTTP_PROXY for all hosts', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : '*' + env: { + HTTP_PROXY: s.url, + NO_PROXY: '*' } }, false) runTest('NO_PROXY should override HTTP_PROXY for all subdomains', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'google.com' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'google.com' }, - headers : { host : 'www.google.com' } + headers: { host: 'www.google.com' } }, false) runTest('NO_PROXY should not override HTTP_PROXY for partial domain matches', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'oogle.com' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'oogle.com' } }, true) runTest('NO_PROXY with port should not override HTTP_PROXY for partial domain matches', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'oogle.com:80' + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'oogle.com:80' } }, true) @@ -271,33 +269,33 @@ function addTests() { // this fails if the check 'isMatchedAt > -1' in lib/getProxyFromURI.js is // missing or broken runTest('http_proxy with length of one more than the URL', { - env : { - HTTP_PROXY : s.url, - NO_PROXY : 'elgoog1.com' // one more char than google.com + env: { + HTTP_PROXY: s.url, + NO_PROXY: 'elgoog1.com' // one more char than google.com } }, true) runTest('proxy: null should override HTTP_PROXY', { - env : { HTTP_PROXY : s.url }, - proxy : null, - timeout : 500 + env: { HTTP_PROXY: s.url }, + proxy: null, + timeout: 500 }, false) runTest('uri auth without proxy auth', { - url : 'http://user:pass@google.com', - proxy : s.url - }, function(t, req, res) { + url: 'http://user:pass@google.com', + proxy: s.url + }, function (t, req, res) { t.equal(req.headers['proxy-authorization'], undefined) t.equal(req.headers.authorization, 'Basic dXNlcjpwYXNz') }) } } -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { addTests() - tape('cleanup', function(t) { - s.close(function() { + tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-qs.js b/tests/test-qs.js index 85d741403..f70c685db 100644 --- a/tests/test-qs.js +++ b/tests/test-qs.js @@ -1,7 +1,7 @@ 'use strict' var request = require('../index') - , tape = require('tape') +var tape = require('tape') // Run a querystring test. `options` can have the following keys: // - suffix : a string to be added to the URL @@ -11,10 +11,10 @@ var request = require('../index') // - afterRequest : a function to execute after creating the request // - expected : the expected path of the request // - expectedQuerystring : expected path when using the querystring library -function runTest(name, options) { +function runTest (name, options) { var uri = 'http://www.google.com' + (options.suffix || '') var opts = { - uri : uri, + uri: uri, qsParseOptions: options.qsParseOptions, qsStringifyOptions: options.qsStringifyOptions } @@ -23,25 +23,25 @@ function runTest(name, options) { opts.qs = options.qs } - tape(name + ' - using qs', function(t) { + tape(name + ' - using qs', function (t) { var r = request.get(opts) if (typeof options.afterRequest === 'function') { options.afterRequest(r) } - process.nextTick(function() { + process.nextTick(function () { t.equal(r.path, options.expected) r.abort() t.end() }) }) - tape(name + ' - using querystring', function(t) { + tape(name + ' - using querystring', function (t) { opts.useQuerystring = true var r = request.get(opts) if (typeof options.afterRequest === 'function') { options.afterRequest(r) } - process.nextTick(function() { + process.nextTick(function () { t.equal(r.path, options.expectedQuerystring || options.expected) r.abort() t.end() @@ -49,87 +49,87 @@ function runTest(name, options) { }) } -function esc(str) { +function esc (str) { return str .replace(/\[/g, '%5B') .replace(/\]/g, '%5D') } runTest('adding a querystring', { - qs : { q : 'search' }, - expected : '/?q=search' + qs: { q: 'search' }, + expected: '/?q=search' }) runTest('replacing a querystring value', { - suffix : '?q=abc', - qs : { q : 'search' }, - expected : '/?q=search' + suffix: '?q=abc', + qs: { q: 'search' }, + expected: '/?q=search' }) runTest('appending a querystring value to the ones present in the uri', { - suffix : '?x=y', - qs : { q : 'search' }, - expected : '/?x=y&q=search' + suffix: '?x=y', + qs: { q: 'search' }, + expected: '/?x=y&q=search' }) runTest('leaving a querystring alone', { - suffix : '?x=y', - expected : '/?x=y' + suffix: '?x=y', + expected: '/?x=y' }) runTest('giving empty qs property', { - qs : {}, - expected : '/' + qs: {}, + expected: '/' }) runTest('modifying the qs after creating the request', { - qs : {}, - afterRequest : function(r) { - r.qs({ q : 'test' }) + qs: {}, + afterRequest: function (r) { + r.qs({ q: 'test' }) }, - expected : '/?q=test' + expected: '/?q=test' }) runTest('a query with an object for a value', { - qs : { where : { foo: 'bar' } }, - expected : esc('/?where[foo]=bar'), - expectedQuerystring : '/?where=' + qs: { where: { foo: 'bar' } }, + expected: esc('/?where[foo]=bar'), + expectedQuerystring: '/?where=' }) runTest('a query with an array for a value', { - qs : { order : ['bar', 'desc'] }, - expected : esc('/?order[0]=bar&order[1]=desc'), - expectedQuerystring : '/?order=bar&order=desc' + qs: { order: ['bar', 'desc'] }, + expected: esc('/?order[0]=bar&order[1]=desc'), + expectedQuerystring: '/?order=bar&order=desc' }) runTest('pass options to the qs module via the qsParseOptions key', { - suffix : '?a=1;b=2', + suffix: '?a=1;b=2', qs: {}, - qsParseOptions: { delimiter : ';' }, - qsStringifyOptions: { delimiter : ';' }, - expected : esc('/?a=1;b=2'), - expectedQuerystring : '/?a=1%3Bb%3D2' + qsParseOptions: { delimiter: ';' }, + qsStringifyOptions: { delimiter: ';' }, + expected: esc('/?a=1;b=2'), + expectedQuerystring: '/?a=1%3Bb%3D2' }) runTest('pass options to the qs module via the qsStringifyOptions key', { - qs : { order : ['bar', 'desc'] }, - qsStringifyOptions: { arrayFormat : 'brackets' }, - expected : esc('/?order[]=bar&order[]=desc'), - expectedQuerystring : '/?order=bar&order=desc' + qs: { order: ['bar', 'desc'] }, + qsStringifyOptions: { arrayFormat: 'brackets' }, + expected: esc('/?order[]=bar&order[]=desc'), + expectedQuerystring: '/?order=bar&order=desc' }) runTest('pass options to the querystring module via the qsParseOptions key', { - suffix : '?a=1;b=2', + suffix: '?a=1;b=2', qs: {}, - qsParseOptions: { sep : ';' }, - qsStringifyOptions: { sep : ';' }, - expected : esc('/?a=1%3Bb%3D2'), - expectedQuerystring : '/?a=1;b=2' + qsParseOptions: { sep: ';' }, + qsStringifyOptions: { sep: ';' }, + expected: esc('/?a=1%3Bb%3D2'), + expectedQuerystring: '/?a=1;b=2' }) runTest('pass options to the querystring module via the qsStringifyOptions key', { - qs : { order : ['bar', 'desc'] }, - qsStringifyOptions: { sep : ';' }, - expected : esc('/?order[0]=bar&order[1]=desc'), - expectedQuerystring : '/?order=bar;order=desc' + qs: { order: ['bar', 'desc'] }, + qsStringifyOptions: { sep: ';' }, + expected: esc('/?order[0]=bar&order[1]=desc'), + expectedQuerystring: '/?order=bar;order=desc' }) diff --git a/tests/test-redirect-auth.js b/tests/test-redirect-auth.js index 400ad5429..7aef6edcc 100644 --- a/tests/test-redirect-auth.js +++ b/tests/test-redirect-auth.js @@ -1,40 +1,40 @@ 'use strict' var server = require('./server') - , request = require('../index') - , util = require('util') - , tape = require('tape') - , destroyable = require('server-destroy') +var request = require('../index') +var util = require('util') +var tape = require('tape') +var destroyable = require('server-destroy') var s = server.createServer() - , ss = server.createSSLServer() +var ss = server.createSSLServer() destroyable(s) destroyable(ss) // always send basic auth and allow non-strict SSL request = request.defaults({ - auth : { - user : 'test', - pass : 'testing' + auth: { + user: 'test', + pass: 'testing' }, - rejectUnauthorized : false + rejectUnauthorized: false }) // redirect.from(proto, host).to(proto, host) returns an object with keys: // src : source URL // dst : destination URL var redirect = { - from : function(fromProto, fromHost) { + from: function (fromProto, fromHost) { return { - to : function(toProto, toHost) { + to: function (toProto, toHost) { var fromPort = (fromProto === 'http' ? s.port : ss.port) - , toPort = (toProto === 'http' ? s.port : ss.port) + var toPort = (toProto === 'http' ? s.port : ss.port) return { - src : util.format( + src: util.format( '%s://%s:%d/to/%s/%s', fromProto, fromHost, fromPort, toProto, toHost), - dst : util.format( + dst: util.format( '%s://%s:%d/from/%s/%s', toProto, toHost, toPort, fromProto, fromHost) } @@ -43,20 +43,20 @@ var redirect = { } } -function handleRequests(srv) { - ['http', 'https'].forEach(function(proto) { - ['localhost', '127.0.0.1'].forEach(function(host) { - srv.on(util.format('/to/%s/%s', proto, host), function(req, res) { +function handleRequests (srv) { + ['http', 'https'].forEach(function (proto) { + ['localhost', '127.0.0.1'].forEach(function (host) { + srv.on(util.format('/to/%s/%s', proto, host), function (req, res) { var r = redirect .from(srv.protocol, req.headers.host.split(':')[0]) .to(proto, host) res.writeHead(301, { - location : r.dst + location: r.dst }) res.end() }) - srv.on(util.format('/from/%s/%s', proto, host), function(req, res) { + srv.on(util.format('/from/%s/%s', proto, host), function (req, res) { res.end('auth: ' + (req.headers.authorization || '(nothing)')) }) }) @@ -66,9 +66,9 @@ function handleRequests(srv) { handleRequests(s) handleRequests(ss) -function runTest(name, redir, expectAuth) { - tape('redirect to ' + name, function(t) { - request(redir.src, function(err, res, body) { +function runTest (name, redir, expectAuth) { + tape('redirect to ' + name, function (t) { + request(redir.src, function (err, res, body) { t.equal(err, null) t.equal(res.request.uri.href, redir.dst) t.equal(res.statusCode, 200) @@ -80,7 +80,7 @@ function runTest(name, redir, expectAuth) { }) } -function addTests() { +function addTests () { runTest('same host and protocol', redirect.from('http', 'localhost').to('http', 'localhost'), true) @@ -98,13 +98,13 @@ function addTests() { false) } -tape('setup', function(t) { - s.listen(0, function() { - ss.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { + ss.listen(0, function () { addTests() - tape('cleanup', function(t) { - s.destroy(function() { - ss.destroy(function() { + tape('cleanup', function (t) { + s.destroy(function () { + ss.destroy(function () { t.end() }) }) @@ -114,18 +114,18 @@ tape('setup', function(t) { }) }) -tape('redirect URL helper', function(t) { +tape('redirect URL helper', function (t) { t.deepEqual( redirect.from('http', 'localhost').to('https', '127.0.0.1'), { - src : util.format('http://localhost:%d/to/https/127.0.0.1', s.port), - dst : util.format('https://127.0.0.1:%d/from/http/localhost', ss.port) + src: util.format('http://localhost:%d/to/https/127.0.0.1', s.port), + dst: util.format('https://127.0.0.1:%d/from/http/localhost', ss.port) }) t.deepEqual( redirect.from('https', 'localhost').to('http', 'localhost'), { - src : util.format('https://localhost:%d/to/http/localhost', ss.port), - dst : util.format('http://localhost:%d/from/https/localhost', s.port) + src: util.format('https://localhost:%d/to/http/localhost', ss.port), + dst: util.format('http://localhost:%d/from/https/localhost', s.port) }) t.end() }) diff --git a/tests/test-redirect-complex.js b/tests/test-redirect-complex.js index 4f11ab56b..072b5986c 100644 --- a/tests/test-redirect-complex.js +++ b/tests/test-redirect-complex.js @@ -1,27 +1,29 @@ 'use strict' var server = require('./server') - , request = require('../index') - , events = require('events') - , tape = require('tape') - , destroyable = require('server-destroy') +var request = require('../index') +var events = require('events') +var tape = require('tape') +var destroyable = require('server-destroy') var s = server.createServer() - , ss = server.createSSLServer() - , e = new events.EventEmitter() +var ss = server.createSSLServer() +var e = new events.EventEmitter() destroyable(s) destroyable(ss) -function bouncy(s, serverUrl) { - var redirs = { a: 'b' - , b: 'c' - , c: 'd' - , d: 'e' - , e: 'f' - , f: 'g' - , g: 'h' - , h: 'end' } +function bouncy (s, serverUrl) { + var redirs = { + a: 'b', + b: 'c', + c: 'd', + d: 'e', + e: 'f', + f: 'g', + g: 'h', + h: 'end' + } var perm = true Object.keys(redirs).forEach(function (p) { @@ -31,7 +33,7 @@ function bouncy(s, serverUrl) { var type = perm ? 301 : 302 perm = !perm s.on('/' + p, function (req, res) { - setTimeout(function() { + setTimeout(function () { res.writeHead(type, { location: serverUrl + '/' + t }) res.end() }, Math.round(Math.random() * 25)) @@ -46,9 +48,9 @@ function bouncy(s, serverUrl) { }) } -tape('setup', function(t) { - s.listen(0, function() { - ss.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { + ss.listen(0, function () { bouncy(s, ss.url) bouncy(ss, s.url) t.end() @@ -56,23 +58,23 @@ tape('setup', function(t) { }) }) -tape('lots of redirects', function(t) { +tape('lots of redirects', function (t) { var n = 10 t.plan(n * 4) - function doRedirect(i) { + function doRedirect (i) { var key = 'test_' + i request({ url: (i % 2 ? s.url : ss.url) + '/a', headers: { 'x-test-key': key }, rejectUnauthorized: false - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, key) }) - e.once('hit-' + key, function(v) { + e.once('hit-' + key, function (v) { t.equal(v, key) }) } @@ -82,9 +84,9 @@ tape('lots of redirects', function(t) { } }) -tape('cleanup', function(t) { - s.destroy(function() { - ss.destroy(function() { +tape('cleanup', function (t) { + s.destroy(function () { + ss.destroy(function () { t.end() }) }) diff --git a/tests/test-redirect.js b/tests/test-redirect.js index decfd2602..b7b5ca676 100644 --- a/tests/test-redirect.js +++ b/tests/test-redirect.js @@ -1,34 +1,34 @@ 'use strict' var server = require('./server') - , assert = require('assert') - , request = require('../index') - , tape = require('tape') - , http = require('http') - , destroyable = require('server-destroy') +var assert = require('assert') +var request = require('../index') +var tape = require('tape') +var http = require('http') +var destroyable = require('server-destroy') var s = server.createServer() - , ss = server.createSSLServer() - , hits = {} - , jar = request.jar() +var ss = server.createSSLServer() +var hits = {} +var jar = request.jar() destroyable(s) destroyable(ss) -s.on('/ssl', function(req, res) { +s.on('/ssl', function (req, res) { res.writeHead(302, { - location : ss.url + '/' + location: ss.url + '/' }) res.end() }) -ss.on('/', function(req, res) { +ss.on('/', function (req, res) { res.writeHead(200) res.end('SSL') }) -function createRedirectEndpoint(code, label, landing) { - s.on('/' + label, function(req, res) { +function createRedirectEndpoint (code, label, landing) { + s.on('/' + label, function (req, res) { hits[label] = true res.writeHead(code, { 'location': s.url + '/' + landing, @@ -38,8 +38,8 @@ function createRedirectEndpoint(code, label, landing) { }) } -function createLandingEndpoint(landing) { - s.on('/' + landing, function(req, res) { +function createLandingEndpoint (landing) { + s.on('/' + landing, function (req, res) { // Make sure the cookie doesn't get included twice, see #139: // Make sure cookies are set properly after redirect assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs') @@ -49,11 +49,11 @@ function createLandingEndpoint(landing) { }) } -function bouncer(code, label, hops) { - var hop, - landing = label + '_landing', - currentLabel, - currentLanding +function bouncer (code, label, hops) { + var hop + var landing = label + '_landing' + var currentLabel + var currentLanding hops = hops || 1 @@ -71,9 +71,9 @@ function bouncer(code, label, hops) { createLandingEndpoint(landing) } -tape('setup', function(t) { - s.listen(0, function() { - ss.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { + ss.listen(0, function () { bouncer(301, 'temp') bouncer(301, 'double', 2) bouncer(301, 'treble', 3) @@ -85,14 +85,14 @@ tape('setup', function(t) { }) }) -tape('permanent bounce', function(t) { +tape('permanent bounce', function (t) { jar.setCookie('quux=baz', s.url) hits = {} request({ uri: s.url + '/perm', jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.perm, 'Original request is to /perm') @@ -102,7 +102,7 @@ tape('permanent bounce', function(t) { }) }) -tape('preserve HEAD method when using followAllRedirects', function(t) { +tape('preserve HEAD method when using followAllRedirects', function (t) { jar.setCookie('quux=baz', s.url) hits = {} request({ @@ -111,7 +111,7 @@ tape('preserve HEAD method when using followAllRedirects', function(t) { followAllRedirects: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.perm, 'Original request is to /perm') @@ -121,13 +121,13 @@ tape('preserve HEAD method when using followAllRedirects', function(t) { }) }) -tape('temporary bounce', function(t) { +tape('temporary bounce', function (t) { hits = {} request({ uri: s.url + '/temp', jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.temp, 'Original request is to /temp') @@ -137,14 +137,14 @@ tape('temporary bounce', function(t) { }) }) -tape('prevent bouncing', function(t) { +tape('prevent bouncing', function (t) { hits = {} request({ uri: s.url + '/nope', jar: jar, headers: { cookie: 'foo=bar' }, followRedirect: false - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 302) t.ok(hits.nope, 'Original request to /nope') @@ -154,12 +154,12 @@ tape('prevent bouncing', function(t) { }) }) -tape('should not follow post redirects by default', function(t) { +tape('should not follow post redirects by default', function (t) { hits = {} request.post(s.url + '/temp', { jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 301) t.ok(hits.temp, 'Original request is to /temp') @@ -169,14 +169,14 @@ tape('should not follow post redirects by default', function(t) { }) }) -tape('should follow post redirects when followallredirects true', function(t) { +tape('should follow post redirects when followallredirects true', function (t) { hits = {} request.post({ uri: s.url + '/temp', followAllRedirects: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.temp, 'Original request is to /temp') @@ -186,7 +186,7 @@ tape('should follow post redirects when followallredirects true', function(t) { }) }) -tape('should follow post redirects when followallredirects true and followOriginalHttpMethod is enabled', function(t) { +tape('should follow post redirects when followallredirects true and followOriginalHttpMethod is enabled', function (t) { hits = {} request.post({ uri: s.url + '/temp', @@ -194,7 +194,7 @@ tape('should follow post redirects when followallredirects true and followOrigin followOriginalHttpMethod: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.temp, 'Original request is to /temp') @@ -204,14 +204,14 @@ tape('should follow post redirects when followallredirects true and followOrigin }) }) -tape('should not follow post redirects when followallredirects false', function(t) { +tape('should not follow post redirects when followallredirects false', function (t) { hits = {} request.post({ uri: s.url + '/temp', followAllRedirects: false, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 301) t.ok(hits.temp, 'Original request is to /temp') @@ -221,12 +221,12 @@ tape('should not follow post redirects when followallredirects false', function( }) }) -tape('should not follow delete redirects by default', function(t) { +tape('should not follow delete redirects by default', function (t) { hits = {} request.del(s.url + '/temp', { jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.ok(res.statusCode >= 301 && res.statusCode < 400, 'Status is a redirect') t.ok(hits.temp, 'Original request is to /temp') @@ -236,13 +236,13 @@ tape('should not follow delete redirects by default', function(t) { }) }) -tape('should not follow delete redirects even if followredirect is set to true', function(t) { +tape('should not follow delete redirects even if followredirect is set to true', function (t) { hits = {} request.del(s.url + '/temp', { followRedirect: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 301) t.ok(hits.temp, 'Original request is to /temp') @@ -252,13 +252,13 @@ tape('should not follow delete redirects even if followredirect is set to true', }) }) -tape('should follow delete redirects when followallredirects true', function(t) { +tape('should follow delete redirects when followallredirects true', function (t) { hits = {} request.del(s.url + '/temp', { followAllRedirects: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.temp, 'Original request is to /temp') @@ -268,13 +268,13 @@ tape('should follow delete redirects when followallredirects true', function(t) }) }) -tape('should follow 307 delete redirects when followallredirects true', function(t) { +tape('should follow 307 delete redirects when followallredirects true', function (t) { hits = {} request.del(s.url + '/fwd', { followAllRedirects: true, jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.fwd, 'Original request is to /fwd') @@ -284,13 +284,13 @@ tape('should follow 307 delete redirects when followallredirects true', function }) }) -tape('double bounce', function(t) { +tape('double bounce', function (t) { hits = {} request({ uri: s.url + '/double', jar: jar, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.ok(hits.double, 'Original request is to /double') @@ -301,8 +301,8 @@ tape('double bounce', function(t) { }) }) -tape('double bounce terminated after first redirect', function(t) { - function filterDouble(response) { +tape('double bounce terminated after first redirect', function (t) { + function filterDouble (response) { return (response.headers.location || '').indexOf('double_2') === -1 } @@ -312,7 +312,7 @@ tape('double bounce terminated after first redirect', function(t) { jar: jar, headers: { cookie: 'foo=bar' }, followRedirect: filterDouble - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 301) t.ok(hits.double, 'Original request is to /double') @@ -321,8 +321,8 @@ tape('double bounce terminated after first redirect', function(t) { }) }) -tape('triple bounce terminated after second redirect', function(t) { - function filterTreble(response) { +tape('triple bounce terminated after second redirect', function (t) { + function filterTreble (response) { return (response.headers.location || '').indexOf('treble_3') === -1 } @@ -332,7 +332,7 @@ tape('triple bounce terminated after second redirect', function(t) { jar: jar, headers: { cookie: 'foo=bar' }, followRedirect: filterTreble - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 301) t.ok(hits.treble, 'Original request is to /treble') @@ -341,12 +341,12 @@ tape('triple bounce terminated after second redirect', function(t) { }) }) -tape('http to https redirect', function(t) { +tape('http to https redirect', function (t) { hits = {} request.get({ uri: require('url').parse(s.url + '/ssl'), rejectUnauthorized: false - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'SSL', 'Got SSL redirect') @@ -354,67 +354,67 @@ tape('http to https redirect', function(t) { }) }) -tape('should have referer header by default when following redirect', function(t) { +tape('should have referer header by default when following redirect', function (t) { request.post({ uri: s.url + '/temp', jar: jar, followAllRedirects: true, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.end() }) - .on('redirect', function() { + .on('redirect', function () { t.equal(this.headers.referer, s.url + '/temp') }) }) -tape('should not have referer header when removeRefererHeader is true', function(t) { +tape('should not have referer header when removeRefererHeader is true', function (t) { request.post({ uri: s.url + '/temp', jar: jar, followAllRedirects: true, removeRefererHeader: true, headers: { cookie: 'foo=bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.end() }) - .on('redirect', function() { + .on('redirect', function () { t.equal(this.headers.referer, undefined) }) }) -tape('should preserve referer header set in the initial request when removeRefererHeader is true', function(t) { +tape('should preserve referer header set in the initial request when removeRefererHeader is true', function (t) { request.post({ uri: s.url + '/temp', jar: jar, followAllRedirects: true, removeRefererHeader: true, headers: { cookie: 'foo=bar', referer: 'http://awesome.com' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.end() }) - .on('redirect', function() { + .on('redirect', function () { t.equal(this.headers.referer, 'http://awesome.com') }) }) -tape('should use same agent class on redirect', function(t) { +tape('should use same agent class on redirect', function (t) { var agent var calls = 0 var agentOptions = {} - function FakeAgent(agentOptions) { + function FakeAgent (agentOptions) { var createConnection agent = new http.Agent(agentOptions) createConnection = agent.createConnection - agent.createConnection = function() { + agent.createConnection = function () { calls++ return createConnection.apply(agent, arguments) } @@ -429,7 +429,7 @@ tape('should use same agent class on redirect', function(t) { headers: { cookie: 'foo=bar' }, agentOptions: agentOptions, agentClass: FakeAgent - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'GET temp_landing', 'Got temporary landing content') @@ -440,9 +440,9 @@ tape('should use same agent class on redirect', function(t) { }) }) -tape('cleanup', function(t) { - s.destroy(function() { - ss.destroy(function() { +tape('cleanup', function (t) { + s.destroy(function () { + ss.destroy(function () { t.end() }) }) diff --git a/tests/test-rfc3986.js b/tests/test-rfc3986.js index a48bd31db..a3918628d 100644 --- a/tests/test-rfc3986.js +++ b/tests/test-rfc3986.js @@ -1,22 +1,19 @@ 'use strict' var http = require('http') - , request = require('../index') - , tape = require('tape') - +var request = require('../index') +var tape = require('tape') function runTest (t, options) { - - var server = http.createServer(function(req, res) { - + var server = http.createServer(function (req, res) { var data = '' req.setEncoding('utf8') - req.on('data', function(d) { + req.on('data', function (d) { data += d }) - req.on('end', function() { + req.on('end', function () { if (options.qs) { t.equal(req.url, '/?rfc3986=%21%2A%28%29%27') } @@ -27,11 +24,11 @@ function runTest (t, options) { }) }) - server.listen(0, function() { + server.listen(0, function () { var port = this.address().port - request.post('http://localhost:' + port, options, function(err, res, body) { + request.post('http://localhost:' + port, options, function (err, res, body) { t.equal(err, null) - server.close(function() { + server.close(function () { t.end() }) }) @@ -39,60 +36,60 @@ function runTest (t, options) { } var bodyEscaped = 'rfc3986=%21%2A%28%29%27' - , bodyJson = '{"rfc3986":"!*()\'"}' +var bodyJson = '{"rfc3986":"!*()\'"}' var cases = [ { _name: 'qs', - qs: {rfc3986: '!*()\''}, + qs: {rfc3986: "!*()'"}, _expectBody: '' }, { _name: 'qs + json', - qs: {rfc3986: '!*()\''}, + qs: {rfc3986: "!*()'"}, json: true, _expectBody: '' }, { _name: 'form', - form: {rfc3986: '!*()\''}, + form: {rfc3986: "!*()'"}, _expectBody: bodyEscaped }, { _name: 'form + json', - form: {rfc3986: '!*()\''}, + form: {rfc3986: "!*()'"}, json: true, _expectBody: bodyEscaped }, { _name: 'qs + form', - qs: {rfc3986: '!*()\''}, - form: {rfc3986: '!*()\''}, + qs: {rfc3986: "!*()'"}, + form: {rfc3986: "!*()'"}, _expectBody: bodyEscaped }, { _name: 'qs + form + json', - qs: {rfc3986: '!*()\''}, - form: {rfc3986: '!*()\''}, + qs: {rfc3986: "!*()'"}, + form: {rfc3986: "!*()'"}, json: true, _expectBody: bodyEscaped }, { _name: 'body + header + json', headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, - body: 'rfc3986=!*()\'', + body: "rfc3986=!*()'", json: true, _expectBody: bodyEscaped }, { _name: 'body + json', - body: {rfc3986: '!*()\''}, + body: {rfc3986: "!*()'"}, json: true, _expectBody: bodyJson }, { _name: 'json object', - json: {rfc3986: '!*()\''}, + json: {rfc3986: "!*()'"}, _expectBody: bodyJson } ] @@ -102,7 +99,7 @@ var libs = ['qs', 'querystring'] libs.forEach(function (lib) { cases.forEach(function (options) { options.useQuerystring = (lib === 'querystring') - tape(lib + ' rfc3986 ' + options._name, function(t) { + tape(lib + ' rfc3986 ' + options._name, function (t) { runTest(t, options) }) }) diff --git a/tests/test-stream.js b/tests/test-stream.js index 88142f030..1d7bf3de0 100644 --- a/tests/test-stream.js +++ b/tests/test-stream.js @@ -1,4 +1,3 @@ - var fs = require('fs') var path = require('path') var http = require('http') @@ -6,13 +5,12 @@ var tape = require('tape') var request = require('../') var server - tape('before', function (t) { server = http.createServer() server.on('request', function (req, res) { req.pipe(res) }) - server.listen(0, function() { + server.listen(0, function () { server.url = 'http://localhost:' + this.address().port t.end() }) @@ -27,6 +25,7 @@ tape('request body stream', function (t) { body: input, encoding: null }, function (err, res, body) { + t.error(err) t.equal(body.length, fs.statSync(fpath).size) t.end() }) diff --git a/tests/test-timeout.js b/tests/test-timeout.js index 7fb733c52..2ab0639c4 100644 --- a/tests/test-timeout.js +++ b/tests/test-timeout.js @@ -1,12 +1,12 @@ 'use strict' -function checkErrCode(t, err) { +function checkErrCode (t, err) { t.notEqual(err, null) t.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT', 'Error ETIMEDOUT or ESOCKETTIMEDOUT') } -function checkEventHandlers(t, socket) { +function checkEventHandlers (t, socket) { var connectListeners = socket.listeners('connect') var found = false for (var i = 0; i < connectListeners.length; ++i) { @@ -20,52 +20,52 @@ function checkEventHandlers(t, socket) { } var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') var s = server.createServer() // Request that waits for 200ms -s.on('/timeout', function(req, res) { - setTimeout(function() { - res.writeHead(200, {'content-type':'text/plain'}) +s.on('/timeout', function (req, res) { + setTimeout(function () { + res.writeHead(200, {'content-type': 'text/plain'}) res.write('waited') res.end() }, 200) }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { t.end() }) }) -tape('should timeout', function(t) { +tape('should timeout', function (t) { var shouldTimeout = { url: s.url + '/timeout', timeout: 100 } - request(shouldTimeout, function(err, res, body) { + request(shouldTimeout, function (err, res, body) { checkErrCode(t, err) t.end() }) }) -tape('should set connect to false', function(t) { +tape('should set connect to false', function (t) { var shouldTimeout = { url: s.url + '/timeout', timeout: 100 } - request(shouldTimeout, function(err, res, body) { + request(shouldTimeout, function (err, res, body) { checkErrCode(t, err) t.ok(err.connect === false, 'Read Timeout Error should set \'connect\' property to false') t.end() }) }) -tape('should timeout with events', function(t) { +tape('should timeout with events', function (t) { t.plan(3) var shouldTimeoutWithEvents = { @@ -75,49 +75,49 @@ tape('should timeout with events', function(t) { var eventsEmitted = 0 request(shouldTimeoutWithEvents) - .on('error', function(err) { + .on('error', function (err) { eventsEmitted++ t.equal(1, eventsEmitted) checkErrCode(t, err) }) }) -tape('should not timeout', function(t) { +tape('should not timeout', function (t) { var shouldntTimeout = { url: s.url + '/timeout', timeout: 1200 } var socket - request(shouldntTimeout, function(err, res, body) { + request(shouldntTimeout, function (err, res, body) { t.equal(err, null) t.equal(body, 'waited') checkEventHandlers(t, socket) t.end() - }).on('socket', function(socket_) { + }).on('socket', function (socket_) { socket = socket_ }) }) -tape('no timeout', function(t) { +tape('no timeout', function (t) { var noTimeout = { url: s.url + '/timeout' } - request(noTimeout, function(err, res, body) { + request(noTimeout, function (err, res, body) { t.equal(err, null) t.equal(body, 'waited') t.end() }) }) -tape('negative timeout', function(t) { // should be treated a zero or the minimum delay +tape('negative timeout', function (t) { // should be treated a zero or the minimum delay var negativeTimeout = { url: s.url + '/timeout', timeout: -1000 } - request(negativeTimeout, function(err, res, body) { + request(negativeTimeout, function (err, res, body) { // Only verify error if it is set, since using a timeout value of 0 can lead // to inconsistent results, depending on a variety of factors if (err) { @@ -127,13 +127,13 @@ tape('negative timeout', function(t) { // should be treated a zero or the minimu }) }) -tape('float timeout', function(t) { // should be rounded by setTimeout anyway +tape('float timeout', function (t) { // should be rounded by setTimeout anyway var floatTimeout = { url: s.url + '/timeout', timeout: 100.76 } - request(floatTimeout, function(err, res, body) { + request(floatTimeout, function (err, res, body) { checkErrCode(t, err) t.end() }) @@ -151,7 +151,7 @@ var nonRoutable = [ '172.31.255.255' ] var nrIndex = 0 -function getNonRoutable() { +function getNonRoutable () { var ip = nonRoutable[nrIndex] if (!ip) { throw new Error('No more non-routable addresses') @@ -159,14 +159,14 @@ function getNonRoutable() { ++nrIndex return ip } -tape('connect timeout', function tryConnect(t) { +tape('connect timeout', function tryConnect (t) { var tarpitHost = 'http://' + getNonRoutable() var shouldConnectTimeout = { url: tarpitHost + '/timeout', timeout: 100 } var socket - request(shouldConnectTimeout, function(err) { + request(shouldConnectTimeout, function (err) { t.notEqual(err, null) if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) { // With some network configurations, some addresses will be reported as @@ -179,19 +179,19 @@ tape('connect timeout', function tryConnect(t) { checkEventHandlers(t, socket) nrIndex = 0 t.end() - }).on('socket', function(socket_) { + }).on('socket', function (socket_) { socket = socket_ }) }) -tape('connect timeout with non-timeout error', function tryConnect(t) { +tape('connect timeout with non-timeout error', function tryConnect (t) { var tarpitHost = 'http://' + getNonRoutable() var shouldConnectTimeout = { url: tarpitHost + '/timeout', timeout: 1000 } var socket - request(shouldConnectTimeout, function(err) { + request(shouldConnectTimeout, function (err) { t.notEqual(err, null) if (err.code === 'ENETUNREACH' && nrIndex < nonRoutable.length) { // With some network configurations, some addresses will be reported as @@ -201,26 +201,27 @@ tape('connect timeout with non-timeout error', function tryConnect(t) { } // Delay the check since the 'connect' handler is removed in a separate // 'error' handler which gets triggered after this callback - setImmediate(function() { + setImmediate(function () { checkEventHandlers(t, socket) nrIndex = 0 t.end() }) - }).on('socket', function(socket_) { + }).on('socket', function (socket_) { socket = socket_ - setImmediate(function() { + setImmediate(function () { socket.emit('error', new Error('Fake Error')) }) }) }) -tape('request timeout with keep-alive connection', function(t) { - var agent = new require('http').Agent({ keepAlive: true }) +tape('request timeout with keep-alive connection', function (t) { + var Agent = require('http').Agent + var agent = new Agent({ keepAlive: true }) var firstReq = { url: s.url + '/timeout', agent: agent } - request(firstReq, function(err) { + request(firstReq, function (err) { // We should now still have a socket open. For the second request we should // see a request timeout on the active socket ... t.equal(err, null) @@ -229,22 +230,22 @@ tape('request timeout with keep-alive connection', function(t) { timeout: 100, agent: agent } - request(shouldReqTimeout, function(err) { + request(shouldReqTimeout, function (err) { checkErrCode(t, err) t.ok(err.connect === false, 'Error should have been a request timeout error') t.end() - }).on('socket', function(socket) { + }).on('socket', function (socket) { var isConnecting = socket._connecting || socket.connecting t.ok(isConnecting !== true, 'Socket should already be connected') }) - }).on('socket', function(socket) { + }).on('socket', function (socket) { var isConnecting = socket._connecting || socket.connecting t.ok(isConnecting === true, 'Socket should be new') }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-timing.js b/tests/test-timing.js index 4e87afcee..f3e77f929 100644 --- a/tests/test-timing.js +++ b/tests/test-timing.js @@ -1,35 +1,38 @@ 'use strict' var server = require('./server') - , request = require('../index') - , tape = require('tape') +var request = require('../index') +var tape = require('tape') +var http = require('http') -var plain_server = server.createServer() - , redirect_mock_time = 10 +var plainServer = server.createServer() +var redirectMockTime = 10 -tape('setup', function(t) { - plain_server.listen(0, function() { - plain_server.on('/', function (req, res) { +tape('setup', function (t) { + plainServer.listen(0, function () { + plainServer.on('/', function (req, res) { res.writeHead(200) res.end('plain') }) - plain_server.on('/redir', function (req, res) { + plainServer.on('/redir', function (req, res) { // fake redirect delay to ensure strong signal for rollup check - setTimeout(function() { - res.writeHead(301, { 'location': 'http://localhost:' + plain_server.port + '/' }) + setTimeout(function () { + res.writeHead(301, { 'location': 'http://localhost:' + plainServer.port + '/' }) res.end() - }, redirect_mock_time) + }, redirectMockTime) }) t.end() }) }) -tape('non-redirected request is timed', function(t) { +tape('non-redirected request is timed', function (t) { var options = {time: true} + var start = new Date().getTime() - var r = request('http://localhost:' + plain_server.port + '/', options, function(err, res, body) { + var r = request('http://localhost:' + plainServer.port + '/', options, function (err, res, body) { var end = new Date().getTime() + t.equal(err, null) t.equal(typeof res.elapsedTime, 'number') t.equal(typeof res.responseStartTime, 'number') @@ -63,7 +66,7 @@ tape('non-redirected request is timed', function(t) { t.deepEqual(propNames, ['socket', 'lookup', 'connect', 'response', 'end']) propNames = [] - for (var propName in res.timingPhases) { + for (propName in res.timingPhases) { if (res.timingPhases.hasOwnProperty(propName)) { propNames.push(propName) } @@ -74,25 +77,26 @@ tape('non-redirected request is timed', function(t) { }) }) -tape('redirected request is timed with rollup', function(t) { +tape('redirected request is timed with rollup', function (t) { var options = {time: true} - var r = request('http://localhost:' + plain_server.port + '/redir', options, function(err, res, body) { + var r = request('http://localhost:' + plainServer.port + '/redir', options, function (err, res, body) { t.equal(err, null) t.equal(typeof res.elapsedTime, 'number') t.equal(typeof res.responseStartTime, 'number') t.equal((res.elapsedTime > 0), true) t.equal((res.responseStartTime > 0), true) - t.equal((res.elapsedTime > redirect_mock_time), true) + t.equal((res.elapsedTime > redirectMockTime), true) t.equal((res.responseStartTime > r.startTime), true) t.end() }) }) -tape('keepAlive is timed', function(t) { - var agent = new require('http').Agent({ keepAlive: true }) +tape('keepAlive is timed', function (t) { + var agent = new http.Agent({ keepAlive: true }) var options = { time: true, agent: agent } var start1 = new Date().getTime() - request('http://localhost:' + plain_server.port + '/', options, function(err1, res1, body1) { + + request('http://localhost:' + plainServer.port + '/', options, function (err1, res1, body1) { var end1 = new Date().getTime() // ensure the first request's timestamps look ok @@ -106,7 +110,7 @@ tape('keepAlive is timed', function(t) { // open a second request with the same agent so we re-use the same connection var start2 = new Date().getTime() - request('http://localhost:' + plain_server.port + '/', options, function(err2, res2, body2) { + request('http://localhost:' + plainServer.port + '/', options, function (err2, res2, body2) { var end2 = new Date().getTime() // ensure the second request's timestamps look ok @@ -115,8 +119,8 @@ tape('keepAlive is timed', function(t) { // ensure socket==lookup==connect for the second request t.equal((res2.timings.socket >= 0), true) - t.equal((res2.timings.lookup == res2.timings.socket), true) - t.equal((res2.timings.connect == res2.timings.lookup), true) + t.equal((res2.timings.lookup === res2.timings.socket), true) + t.equal((res2.timings.connect === res2.timings.lookup), true) t.equal((res2.timings.response >= res2.timings.connect), true) // explicitly shut down the agent @@ -136,8 +140,8 @@ tape('keepAlive is timed', function(t) { }) }) -tape('cleanup', function(t) { - plain_server.close(function() { +tape('cleanup', function (t) { + plainServer.close(function () { t.end() }) }) diff --git a/tests/test-toJSON.js b/tests/test-toJSON.js index 4549844d4..43fa79169 100644 --- a/tests/test-toJSON.js +++ b/tests/test-toJSON.js @@ -1,45 +1,45 @@ 'use strict' var request = require('../index') - , http = require('http') - , tape = require('tape') +var http = require('http') +var tape = require('tape') var s = http.createServer(function (req, resp) { resp.statusCode = 200 resp.end('asdf') }) -tape('setup', function(t) { - s.listen(0, function() { +tape('setup', function (t) { + s.listen(0, function () { s.url = 'http://localhost:' + this.address().port t.end() }) }) -tape('request().toJSON()', function(t) { +tape('request().toJSON()', function (t) { var r = request({ url: s.url, headers: { foo: 'bar' } - }, function(err, res) { - var json_r = JSON.parse(JSON.stringify(r)) - , json_res = JSON.parse(JSON.stringify(res)) + }, function (err, res) { + var jsonR = JSON.parse(JSON.stringify(r)) + var jsonRes = JSON.parse(JSON.stringify(res)) t.equal(err, null) - t.equal(json_r.uri.href, r.uri.href) - t.equal(json_r.method, r.method) - t.equal(json_r.headers.foo, r.headers.foo) + t.equal(jsonR.uri.href, r.uri.href) + t.equal(jsonR.method, r.method) + t.equal(jsonR.headers.foo, r.headers.foo) - t.equal(json_res.statusCode, res.statusCode) - t.equal(json_res.body, res.body) - t.equal(json_res.headers.date, res.headers.date) + t.equal(jsonRes.statusCode, res.statusCode) + t.equal(jsonRes.body, res.body) + t.equal(jsonRes.headers.date, res.headers.date) t.end() }) }) -tape('cleanup', function(t) { - s.close(function() { +tape('cleanup', function (t) { + s.close(function () { t.end() }) }) diff --git a/tests/test-tunnel.js b/tests/test-tunnel.js index 75847345a..fa2ebce33 100644 --- a/tests/test-tunnel.js +++ b/tests/test-tunnel.js @@ -1,33 +1,34 @@ 'use strict' var server = require('./server') - , tape = require('tape') - , request = require('../index') - , https = require('https') - , net = require('net') - , fs = require('fs') - , path = require('path') - , util = require('util') - , url = require('url') - , destroyable = require('server-destroy') +var tape = require('tape') +var request = require('../index') +var https = require('https') +var net = require('net') +var fs = require('fs') +var path = require('path') +var util = require('util') +var url = require('url') +var destroyable = require('server-destroy') var events = [] - , caFile = path.resolve(__dirname, 'ssl/ca/ca.crt') - , ca = fs.readFileSync(caFile) - , clientCert = fs.readFileSync(path.resolve(__dirname, 'ssl/ca/client.crt')) - , clientKey = fs.readFileSync(path.resolve(__dirname, 'ssl/ca/client-enc.key')) - , clientPassword = 'password' - , sslOpts = { - key : path.resolve(__dirname, 'ssl/ca/localhost.key'), - cert : path.resolve(__dirname, 'ssl/ca/localhost.crt') - } - , mutualSSLOpts = { - key : path.resolve(__dirname, 'ssl/ca/localhost.key'), - cert : path.resolve(__dirname, 'ssl/ca/localhost.crt'), - ca : caFile, - requestCert : true, - rejectUnauthorized : true - } +var caFile = path.resolve(__dirname, 'ssl/ca/ca.crt') +var ca = fs.readFileSync(caFile) +var clientCert = fs.readFileSync(path.resolve(__dirname, 'ssl/ca/client.crt')) +var clientKey = fs.readFileSync(path.resolve(__dirname, 'ssl/ca/client-enc.key')) +var clientPassword = 'password' +var sslOpts = { + key: path.resolve(__dirname, 'ssl/ca/localhost.key'), + cert: path.resolve(__dirname, 'ssl/ca/localhost.crt') +} + +var mutualSSLOpts = { + key: path.resolve(__dirname, 'ssl/ca/localhost.key'), + cert: path.resolve(__dirname, 'ssl/ca/localhost.crt'), + ca: caFile, + requestCert: true, + rejectUnauthorized: true +} // this is needed for 'https over http, tunnel=false' test // from https://github.com/coolaj86/node-ssl-root-cas/blob/v1.1.9-beta/ssl-root-cas.js#L4267-L4281 @@ -36,8 +37,8 @@ httpsOpts.ca = httpsOpts.ca || [] httpsOpts.ca.push(ca) var s = server.createServer() - , ss = server.createSSLServer(sslOpts) - , ss2 = server.createSSLServer(mutualSSLOpts) +var ss = server.createSSLServer(sslOpts) +var ss2 = server.createSSLServer(mutualSSLOpts) // XXX when tunneling https over https, connections get left open so the server // doesn't want to close normally (and same issue with http server on v0.8.x) @@ -45,17 +46,17 @@ destroyable(s) destroyable(ss) destroyable(ss2) -function event() { +function event () { events.push(util.format.apply(null, arguments)) } -function setListeners(server, type) { - server.on('/', function(req, res) { +function setListeners (server, type) { + server.on('/', function (req, res) { event('%s response', type) res.end(type + ' ok') }) - server.on('request', function(req, res) { + server.on('request', function (req, res) { if (/^https?:/.test(req.url)) { // This is a proxy request var dest = req.url.split(':')[0] @@ -65,29 +66,29 @@ function setListeners(server, type) { dest += '->' + match[1] } event('%s proxy to %s', type, dest) - request(req.url, { followRedirect : false }).pipe(res) + request(req.url, { followRedirect: false }).pipe(res) } }) - server.on('/redirect/http', function(req, res) { + server.on('/redirect/http', function (req, res) { event('%s redirect to http', type) res.writeHead(301, { - location : s.url + location: s.url }) res.end() }) - server.on('/redirect/https', function(req, res) { + server.on('/redirect/https', function (req, res) { event('%s redirect to https', type) res.writeHead(301, { - location : ss.url + location: ss.url }) res.end() }) - server.on('connect', function(req, client, head) { + server.on('connect', function (req, client, head) { var u = url.parse(req.url) - var server = net.connect(u.host, u.port, function() { + var server = net.connect(u.host, u.port, function () { event('%s connect to %s', type, req.url) client.write('HTTP/1.1 200 Connection established\r\n\r\n') client.pipe(server) @@ -105,7 +106,7 @@ setListeners(ss2, 'https') // proxy in tunnel-agent (this is necessary for "* over https" tests) var customCaCount = 0 var httpsRequestOld = https.request -https.request = function(options) { +https.request = function (options) { if (customCaCount) { options.ca = ca customCaCount-- @@ -113,13 +114,13 @@ https.request = function(options) { return httpsRequestOld.apply(this, arguments) } -function runTest(name, opts, expected) { - tape(name, function(t) { +function runTest (name, opts, expected) { + tape(name, function (t) { opts.ca = ca if (opts.proxy === ss.url) { customCaCount = (opts.url === ss.url ? 2 : 1) } - request(opts, function(err, res, body) { + request(opts, function (err, res, body) { event(err ? 'err ' + err.message : res.statusCode + ' ' + body) t.deepEqual(events, expected) events = [] @@ -128,13 +129,13 @@ function runTest(name, opts, expected) { }) } -function addTests() { +function addTests () { // HTTP OVER HTTP runTest('http over http, tunnel=true', { - url : s.url, - proxy : s.url, - tunnel : true + url: s.url, + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + s.port, 'http response', @@ -142,9 +143,9 @@ function addTests() { ]) runTest('http over http, tunnel=false', { - url : s.url, - proxy : s.url, - tunnel : false + url: s.url, + proxy: s.url, + tunnel: false }, [ 'http proxy to http', 'http response', @@ -152,21 +153,20 @@ function addTests() { ]) runTest('http over http, tunnel=default', { - url : s.url, - proxy : s.url + url: s.url, + proxy: s.url }, [ 'http proxy to http', 'http response', '200 http ok' ]) - // HTTP OVER HTTPS runTest('http over https, tunnel=true', { - url : s.url, - proxy : ss.url, - tunnel : true + url: s.url, + proxy: ss.url, + tunnel: true }, [ 'https connect to localhost:' + s.port, 'http response', @@ -174,9 +174,9 @@ function addTests() { ]) runTest('http over https, tunnel=false', { - url : s.url, - proxy : ss.url, - tunnel : false + url: s.url, + proxy: ss.url, + tunnel: false }, [ 'https proxy to http', 'http response', @@ -184,21 +184,20 @@ function addTests() { ]) runTest('http over https, tunnel=default', { - url : s.url, - proxy : ss.url + url: s.url, + proxy: ss.url }, [ 'https proxy to http', 'http response', '200 http ok' ]) - // HTTPS OVER HTTP runTest('https over http, tunnel=true', { - url : ss.url, - proxy : s.url, - tunnel : true + url: ss.url, + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + ss.port, 'https response', @@ -206,9 +205,9 @@ function addTests() { ]) runTest('https over http, tunnel=false', { - url : ss.url, - proxy : s.url, - tunnel : false + url: ss.url, + proxy: s.url, + tunnel: false }, [ 'http proxy to https', 'https response', @@ -216,21 +215,20 @@ function addTests() { ]) runTest('https over http, tunnel=default', { - url : ss.url, - proxy : s.url + url: ss.url, + proxy: s.url }, [ 'http connect to localhost:' + ss.port, 'https response', '200 https ok' ]) - // HTTPS OVER HTTPS runTest('https over https, tunnel=true', { - url : ss.url, - proxy : ss.url, - tunnel : true + url: ss.url, + proxy: ss.url, + tunnel: true }, [ 'https connect to localhost:' + ss.port, 'https response', @@ -238,10 +236,10 @@ function addTests() { ]) runTest('https over https, tunnel=false', { - url : ss.url, - proxy : ss.url, - tunnel : false, - pool : false // must disable pooling here or Node.js hangs + url: ss.url, + proxy: ss.url, + tunnel: false, + pool: false // must disable pooling here or Node.js hangs }, [ 'https proxy to https', 'https response', @@ -249,21 +247,20 @@ function addTests() { ]) runTest('https over https, tunnel=default', { - url : ss.url, - proxy : ss.url + url: ss.url, + proxy: ss.url }, [ 'https connect to localhost:' + ss.port, 'https response', '200 https ok' ]) - // HTTP->HTTP OVER HTTP runTest('http->http over http, tunnel=true', { - url : s.url + '/redirect/http', - proxy : s.url, - tunnel : true + url: s.url + '/redirect/http', + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + s.port, 'http redirect to http', @@ -273,9 +270,9 @@ function addTests() { ]) runTest('http->http over http, tunnel=false', { - url : s.url + '/redirect/http', - proxy : s.url, - tunnel : false + url: s.url + '/redirect/http', + proxy: s.url, + tunnel: false }, [ 'http proxy to http->http', 'http redirect to http', @@ -285,8 +282,8 @@ function addTests() { ]) runTest('http->http over http, tunnel=default', { - url : s.url + '/redirect/http', - proxy : s.url + url: s.url + '/redirect/http', + proxy: s.url }, [ 'http proxy to http->http', 'http redirect to http', @@ -295,13 +292,12 @@ function addTests() { '200 http ok' ]) - // HTTP->HTTPS OVER HTTP runTest('http->https over http, tunnel=true', { - url : s.url + '/redirect/https', - proxy : s.url, - tunnel : true + url: s.url + '/redirect/https', + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + s.port, 'http redirect to https', @@ -311,9 +307,9 @@ function addTests() { ]) runTest('http->https over http, tunnel=false', { - url : s.url + '/redirect/https', - proxy : s.url, - tunnel : false + url: s.url + '/redirect/https', + proxy: s.url, + tunnel: false }, [ 'http proxy to http->https', 'http redirect to https', @@ -323,8 +319,8 @@ function addTests() { ]) runTest('http->https over http, tunnel=default', { - url : s.url + '/redirect/https', - proxy : s.url + url: s.url + '/redirect/https', + proxy: s.url }, [ 'http proxy to http->https', 'http redirect to https', @@ -333,13 +329,12 @@ function addTests() { '200 https ok' ]) - // HTTPS->HTTP OVER HTTP runTest('https->http over http, tunnel=true', { - url : ss.url + '/redirect/http', - proxy : s.url, - tunnel : true + url: ss.url + '/redirect/http', + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + ss.port, 'https redirect to http', @@ -349,9 +344,9 @@ function addTests() { ]) runTest('https->http over http, tunnel=false', { - url : ss.url + '/redirect/http', - proxy : s.url, - tunnel : false + url: ss.url + '/redirect/http', + proxy: s.url, + tunnel: false }, [ 'http proxy to https->http', 'https redirect to http', @@ -361,8 +356,8 @@ function addTests() { ]) runTest('https->http over http, tunnel=default', { - url : ss.url + '/redirect/http', - proxy : s.url + url: ss.url + '/redirect/http', + proxy: s.url }, [ 'http connect to localhost:' + ss.port, 'https redirect to http', @@ -371,13 +366,12 @@ function addTests() { '200 http ok' ]) - // HTTPS->HTTPS OVER HTTP runTest('https->https over http, tunnel=true', { - url : ss.url + '/redirect/https', - proxy : s.url, - tunnel : true + url: ss.url + '/redirect/https', + proxy: s.url, + tunnel: true }, [ 'http connect to localhost:' + ss.port, 'https redirect to https', @@ -387,9 +381,9 @@ function addTests() { ]) runTest('https->https over http, tunnel=false', { - url : ss.url + '/redirect/https', - proxy : s.url, - tunnel : false + url: ss.url + '/redirect/https', + proxy: s.url, + tunnel: false }, [ 'http proxy to https->https', 'https redirect to https', @@ -399,8 +393,8 @@ function addTests() { ]) runTest('https->https over http, tunnel=default', { - url : ss.url + '/redirect/https', - proxy : s.url + url: ss.url + '/redirect/https', + proxy: s.url }, [ 'http connect to localhost:' + ss.port, 'https redirect to https', @@ -409,16 +403,15 @@ function addTests() { '200 https ok' ]) - // MUTUAL HTTPS OVER HTTP runTest('mutual https over http, tunnel=true', { - url : ss2.url, - proxy : s.url, - tunnel : true, - cert : clientCert, - key : clientKey, - passphrase : clientPassword + url: ss2.url, + proxy: s.url, + tunnel: true, + cert: clientCert, + key: clientKey, + passphrase: clientPassword }, [ 'http connect to localhost:' + ss2.port, 'https response', @@ -440,11 +433,11 @@ function addTests() { // ]) runTest('mutual https over http, tunnel=default', { - url : ss2.url, - proxy : s.url, - cert : clientCert, - key : clientKey, - passphrase : clientPassword + url: ss2.url, + proxy: s.url, + cert: clientCert, + key: clientKey, + passphrase: clientPassword }, [ 'http connect to localhost:' + ss2.port, 'https response', @@ -452,15 +445,15 @@ function addTests() { ]) } -tape('setup', function(t) { - s.listen(0, function() { - ss.listen(0, function() { - ss2.listen(0, 'localhost', function() { +tape('setup', function (t) { + s.listen(0, function () { + ss.listen(0, function () { + ss2.listen(0, 'localhost', function () { addTests() - tape('cleanup', function(t) { - s.destroy(function() { - ss.destroy(function() { - ss2.destroy(function() { + tape('cleanup', function (t) { + s.destroy(function () { + ss.destroy(function () { + ss2.destroy(function () { t.end() }) }) diff --git a/tests/test-unix.js b/tests/test-unix.js index 7a75023b3..acf883273 100644 --- a/tests/test-unix.js +++ b/tests/test-unix.js @@ -1,23 +1,23 @@ 'use strict' var request = require('../index') - , http = require('http') - , fs = require('fs') - , rimraf = require('rimraf') - , assert = require('assert') - , tape = require('tape') - , url = require('url') +var http = require('http') +var fs = require('fs') +var rimraf = require('rimraf') +var assert = require('assert') +var tape = require('tape') +var url = require('url') var rawPath = [null, 'raw', 'path'].join('/') - , queryPath = [null, 'query', 'path'].join('/') - , searchString = '?foo=bar' - , socket = [__dirname, 'tmp-socket'].join('/') - , expectedBody = 'connected' - , statusCode = 200 +var queryPath = [null, 'query', 'path'].join('/') +var searchString = '?foo=bar' +var socket = [__dirname, 'tmp-socket'].join('/') +var expectedBody = 'connected' +var statusCode = 200 rimraf.sync(socket) -var s = http.createServer(function(req, res) { +var s = http.createServer(function (req, res) { var incomingUrl = url.parse(req.url) switch (incomingUrl.pathname) { case rawPath: @@ -36,14 +36,14 @@ var s = http.createServer(function(req, res) { res.end(expectedBody) }) -tape('setup', function(t) { - s.listen(socket, function() { +tape('setup', function (t) { + s.listen(socket, function () { t.end() }) }) -tape('unix socket connection', function(t) { - request( 'http://unix:' + socket + ':' + rawPath, function(err, res, body) { +tape('unix socket connection', function (t) { + request('http://unix:' + socket + ':' + rawPath, function (err, res, body) { t.equal(err, null, 'no error in connection') t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response') t.equal(body, expectedBody, 'expected response body is received') @@ -51,13 +51,13 @@ tape('unix socket connection', function(t) { }) }) -tape('unix socket connection with qs', function(t) { +tape('unix socket connection with qs', function (t) { request({ uri: 'http://unix:' + socket + ':' + queryPath, qs: { foo: 'bar' } - }, function(err, res, body) { + }, function (err, res, body) { t.equal(err, null, 'no error in connection') t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response') t.equal(body, expectedBody, 'expected response body is received') @@ -65,9 +65,9 @@ tape('unix socket connection with qs', function(t) { }) }) -tape('cleanup', function(t) { - s.close(function() { - fs.unlink(socket, function() { +tape('cleanup', function (t) { + s.close(function () { + fs.unlink(socket, function () { t.end() }) })