Skip to content

Commit

Permalink
Query strings now cooperate with unix sockets
Browse files Browse the repository at this point in the history
Fixes issue #1766
  • Loading branch information
JoshWillik committed Sep 10, 2015
1 parent 4b5488c commit 2423e76
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
29 changes: 18 additions & 11 deletions request.js
Expand Up @@ -256,17 +256,7 @@ Request.prototype.init = function (options) {

// Support Unix Sockets
if (self.uri.host === 'unix') {
// Get the socket & request paths from the URL
var unixParts = self.uri.path.split(':')
, host = unixParts[0]
, path = unixParts[1]
// Apply unix properties to request
self.socketPath = host
self.uri.pathname = path
self.uri.path = path
self.uri.host = host
self.uri.hostname = host
self.uri.isUnix = true
self.enableUnixSocket()
}

if (self.strictSSL === false) {
Expand Down Expand Up @@ -1163,6 +1153,10 @@ Request.prototype.qs = function (q, clobber) {
self.url = self.uri
self.path = self.uri.path

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

return self
}
Request.prototype.form = function (form) {
Expand Down Expand Up @@ -1411,6 +1405,19 @@ 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
13 changes: 11 additions & 2 deletions tests/test-unix.js
Expand Up @@ -6,16 +6,20 @@ var request = require('../index')
, rimraf = require('rimraf')
, assert = require('assert')
, tape = require('tape')
, url = require('url')

var path = [null, 'test', 'path'].join('/')
, searchString = '?foo=bar'
, socket = [__dirname, 'tmp-socket'].join('/')
, expectedBody = 'connected'
, statusCode = 200

rimraf.sync(socket)

var s = http.createServer(function(req, res) {
assert.equal(req.url, path, 'requested path is sent to server')
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')
res.statusCode = statusCode
res.end(expectedBody)
})
Expand All @@ -27,7 +31,12 @@ tape('setup', function(t) {
})

tape('unix socket connection', function(t) {
request('http://unix:' + socket + ':' + path, function(err, res, body) {
request({
uri: 'http://unix:' + socket + ':' + path,
qs: {
foo: 'bar'
}
}, 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')
Expand Down

0 comments on commit 2423e76

Please sign in to comment.