Browse Source

Accept http.request object as first argument

http2
Vsevolod Strukchinsky 10 years ago
parent
commit
47da1181a5
  1. 4
      index.js
  2. 4
      readme.md
  3. 9
      test/server.js
  4. 40
      test/test-arguments.js
  5. 2
      test/test-error.js

4
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;

4
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

9
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;

40
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();
});

2
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();

Loading…
Cancel
Save