Skip to content

Commit

Permalink
Added support for a "query" option value that is a hash of querystrin…
Browse files Browse the repository at this point in the history
…g values that is merged (taking precedence over) with the querystring passed in the uri string.
  • Loading branch information
Chris Sainty committed Feb 18, 2012
1 parent 12f4997 commit 2f34fd1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ request.post({url:url, oauth:oauth}, function (e, r, body) {
The first argument can be either a url or an options object. The only required option is uri, all others are optional.

* `uri` || `url` - fully qualified uri or a parsed url object from url.parse()
* `query` - object containing querystring values to be appended to the uri
* `method` - http method, defaults to GET
* `headers` - http headers, defaults to {}
* `body` - entity body for POST and PUT requests. Must be buffer or string.
Expand Down
13 changes: 7 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function Request (options) {
if (!self.uri) {
throw new Error("options.uri is a required argument")
} else {
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
if (typeof self.uri == "string") self.uri = url.parse(self.uri, true)
}
if (self.proxy) {
if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
Expand Down Expand Up @@ -201,14 +201,15 @@ function Request (options) {
self.headers['proxy-authorization'] = "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
}

if (self.uri.path) {
self.path = self.uri.path
} else {
self.path = self.uri.pathname + (self.uri.search || "")
}
self.path = self.uri.pathname
self.uri.query = self.uri.query || { }

if (self.query) for (q in self.query) self.uri.query[q] = self.query[q]

if (self.path.length === 0) self.path = '/'

if (self.uri.query) self.path += '?' + qs.stringify(self.uri.query)

if (self.proxy) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)

if (options.json) {
Expand Down
3 changes: 2 additions & 1 deletion tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ var fs = require('fs')
, events = require('events')
, stream = require('stream')
, assert = require('assert')
, url = require('url')
;

exports.createServer = function (port) {
port = port || 6767
var s = http.createServer(function (req, resp) {
s.emit(req.url, req, resp);
s.emit(url.parse(req.url).pathname, req, resp);
})
s.port = port
s.url = 'http://localhost:'+port
Expand Down
52 changes: 52 additions & 0 deletions tests/test-qs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var request = request = require('../main.js')
, assert = require('assert')
, url = require('url')
, qs = require('querystring')
, server = require('./server')
, s = server.createServer()
;

s.listen(s.port, function () {
var serverUri = 'http://localhost:' + s.port
, numTests = 0
, numOutstandingTests = 0

function createTest(requestObj, query, serverAssertFn) {
var testNumber = numTests;
numTests += 1;
numOutstandingTests += 1;
s.on('/' + testNumber, function (req, res) {
serverAssertFn(req, res);
res.writeHead(200);
res.end();
});
requestObj.url = serverUri + '/' + testNumber + query
request(requestObj, function (err, res, body) {
assert.ok(!err)
assert.equal(res.statusCode, 200)
numOutstandingTests -= 1
if (numOutstandingTests === 0) {
console.log(numTests + ' tests passed.')
s.close()
}
})
}

// Test adding a querystring
createTest({ query: { q : 'search' } }, '', function (req, res) {
var result = qs.parse(url.parse(req.url).query)
assert.deepEqual({ q : 'search' }, result)
})

// Test replacing a querystring value
createTest({ query: { q : 'search' } }, '?q=abc', function (req, res) {
var result = qs.parse(url.parse(req.url).query)
assert.deepEqual({ q : 'search' }, result)
})

// Test appending a querystring value to the ones present in the uri
createTest({ query: { q : 'search' } }, '?x=y', function (req, res) {
var result = qs.parse(url.parse(req.url).query)
assert.deepEqual({ q : 'search', x : 'y' }, result)
})
})

0 comments on commit 2f34fd1

Please sign in to comment.