Skip to content

Commit

Permalink
More tests for unix socket + query string
Browse files Browse the repository at this point in the history
Also brings .enableUnixSocket calls in line with the rest of
the code stylistically.
  • Loading branch information
JoshWillik committed Sep 11, 2015
1 parent 2423e76 commit cf6df69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
29 changes: 15 additions & 14 deletions request.js
Expand Up @@ -1153,7 +1153,7 @@ Request.prototype.qs = function (q, clobber) {
self.url = self.uri
self.path = self.uri.path

if ( self.uri.host === 'unix' ) {
if (self.uri.host === 'unix') {
self.enableUnixSocket()
}

Expand Down Expand Up @@ -1240,6 +1240,20 @@ Request.prototype.getHeader = function (name, headers) {
})
return result
}
Request.prototype.enableUnixSocket = function () {
// Get the socket & request paths from the URL
var unixParts = this.uri.path.split(':')
, host = unixParts[0]
, path = unixParts[1]
// Apply unix properties to request
this.socketPath = host
this.uri.pathname = path
this.uri.path = path
this.uri.host = host
this.uri.hostname = host
this.uri.isUnix = true
}


Request.prototype.auth = function (user, pass, sendImmediately, bearer) {
var self = this
Expand Down Expand Up @@ -1405,19 +1419,6 @@ Request.prototype.destroy = function () {
self.response.destroy()
}
}
Request.prototype.enableUnixSocket = function () {
// Get the socket & request paths from the URL
var unixParts = this.uri.path.split(':')
, host = unixParts[0]
, path = unixParts[1]
// Apply unix properties to request
this.socketPath = host
this.uri.pathname = path
this.uri.path = path
this.uri.host = host
this.uri.hostname = host
this.uri.isUnix = true
}

Request.defaultProxyHeaderWhiteList =
Tunnel.defaultProxyHeaderWhiteList.slice()
Expand Down
29 changes: 25 additions & 4 deletions tests/test-unix.js
Expand Up @@ -8,7 +8,8 @@ var request = require('../index')
, tape = require('tape')
, url = require('url')

var path = [null, 'test', 'path'].join('/')
var rawPath = [null, 'raw', 'path'].join('/')
, queryPath = [null, 'query', 'path'].join('/')
, searchString = '?foo=bar'
, socket = [__dirname, 'tmp-socket'].join('/')
, expectedBody = 'connected'
Expand All @@ -18,8 +19,19 @@ rimraf.sync(socket)

var s = http.createServer(function(req, res) {
var incomingUrl = url.parse(req.url)
assert.equal(incomingUrl.pathname, path, 'requested path is sent to server')
assert.equal(incomingUrl.search, searchString, 'query string is sent to server')
switch (incomingUrl.pathname) {
case rawPath:
assert.equal(incomingUrl.pathname, rawPath, 'requested path is sent to server')
break

case queryPath:
assert.equal(incomingUrl.pathname, queryPath, 'requested path is sent to server')
assert.equal(incomingUrl.search, searchString, 'query string is sent to server')
break

default:
assert(false, 'A valid path was requested')
}
res.statusCode = statusCode
res.end(expectedBody)
})
Expand All @@ -31,8 +43,17 @@ tape('setup', function(t) {
})

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')
t.end()
})
})

tape('unix socket connection with qs', function(t) {
request({
uri: 'http://unix:' + socket + ':' + path,
uri: 'http://unix:' + socket + ':' + queryPath,
qs: {
foo: 'bar'
}
Expand Down

0 comments on commit cf6df69

Please sign in to comment.