Skip to content

Commit

Permalink
Accept http.request object as first argument
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Apr 25, 2015
1 parent 9f14fa6 commit 47da118
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions test/test-arguments.js
Original file line number Diff line number Diff line change
@@ -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();
});
2 changes: 1 addition & 1 deletion test/test-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 47da118

Please sign in to comment.