diff --git a/index.js b/index.js index 31c1ca7..d3fa4f3 100644 --- a/index.js +++ b/index.js @@ -70,10 +70,12 @@ function got(url, opts, cb) { } function get(url, opts, cb) { - var parsedUrl = urlLib.parse(prependHttp(url)); + var parsedUrl = typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url; var fn = parsedUrl.protocol === 'https:' ? https : http; var arg = objectAssign({}, parsedUrl, opts); + url = typeof url === 'string' ? prependHttp(url) : urlLib.format(url); + if (arg.agent === undefined) { arg.agent = infinityAgent[fn === https ? 'https' : 'http'].globalAgent; diff --git a/readme.md b/readme.md index b0334e7..9e46d94 100644 --- a/readme.md +++ b/readme.md @@ -44,9 +44,9 @@ It's a `GET` request by default, but can be changed in `options`. ##### url *Required* -Type: `string` +Type: `string`, `Object` -The URL to request. +The URL to request or bare [http.request options](https://nodejs.org/api/http.html#http_http_request_options_callback) object. ##### options diff --git a/test/server.js b/test/server.js index dd41bcb..ff7542a 100644 --- a/test/server.js +++ b/test/server.js @@ -2,32 +2,37 @@ var http = require('http'); var https = require('https'); +exports.host = 'localhost'; exports.port = 6767; exports.portSSL = 16167; exports.createServer = function (port) { + var host = exports.host; port = port || exports.port; var s = http.createServer(function (req, resp) { s.emit(req.url, req, resp); }); + s.host = host; s.port = port; - s.url = 'http://localhost:' + port; + s.url = 'http://' + host + ':' + port; s.protocol = 'http'; return s; }; exports.createSSLServer = function (port, opts) { + var host = exports.host; port = port || exports.portSSL; var s = https.createServer(opts, function (req, resp) { s.emit(req.url, req, resp); }); + s.host = host; s.port = port; - s.url = 'https://localhost:' + port; + s.url = 'https://' + host + ':' + port; s.protocol = 'https'; return s; diff --git a/test/test-arguments.js b/test/test-arguments.js new file mode 100644 index 0000000..695bc97 --- /dev/null +++ b/test/test-arguments.js @@ -0,0 +1,40 @@ +'use strict'; +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); +var s = server.createServer(); + +s.on('/test', function (req, res) { + res.end(req.url); +}); + +s.on('/?test=wow', function (req, res) { + res.end(req.url); +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('accepts url.parse object as first argument', function (t) { + got({host: s.host, port: s.port, path: '/test'}, function (err, data) { + t.error(err); + t.equal(data, '/test'); + t.end(); + }); +}); + +tape('extends parsed string with opts', function (t) { + got(s.url, {path: '/test'}, function (err, data) { + t.error(err); + t.equal(data, '/test'); + t.end(); + }); +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +}); diff --git a/test/test-error.js b/test/test-error.js index 9c43772..aeed4ab 100644 --- a/test/test-error.js +++ b/test/test-error.js @@ -26,7 +26,7 @@ tape('error message', function (t) { tape('dns error message', function (t) { got('.com', function (err) { t.ok(err); - t.equal(err.message, 'Request to .com failed'); + t.equal(err.message, 'Request to http://.com failed'); t.ok(err.nested); t.ok(/getaddrinfo ENOTFOUND/.test(err.nested.message)); t.end();