Skip to content

Commit 62b5720

Browse files
TehShrikeJames Halliday
authored andcommitted
Support for a string as a parameter
1 parent 3969edd commit 62b5720

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
var http = module.exports;
22
var EventEmitter = require('events').EventEmitter;
33
var Request = require('./lib/request');
4+
var url = require('url')
45

56
http.request = function (params, cb) {
7+
if (typeof params === 'string') {
8+
params = url.parse(params)
9+
}
610
if (!params) params = {};
711
if (!params.host && !params.port) {
812
params.port = parseInt(window.location.port, 10);
913
}
1014
if (!params.host && params.hostname) {
1115
params.host = params.hostname;
1216
}
13-
17+
1418
if (!params.scheme) params.scheme = window.location.protocol.split(':')[0];
1519
if (!params.host) {
1620
params.host = window.location.hostname || window.location.host;
@@ -22,7 +26,7 @@ http.request = function (params, cb) {
2226
params.host = params.host.split(':')[0];
2327
}
2428
if (!params.port) params.port = params.scheme == 'https' ? 443 : 80;
25-
29+
2630
var req = new Request(new xhrHttp, params);
2731
if (cb) req.on('response', cb);
2832
return req;

test/request_url.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('Test simple url string', function(t) {
2727

2828

2929
test('Test full url object', function(t) {
30-
var url = {
30+
var url = {
3131
host: "localhost:8081",
3232
hostname: "localhost",
3333
href: "http://localhost:8081/api/foo?bar=baz",
@@ -46,4 +46,14 @@ test('Test full url object', function(t) {
4646
t.equal( request.uri, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct');
4747
t.end();
4848

49-
});
49+
});
50+
51+
52+
test('Test string as parameters', function(t) {
53+
var url = '/api/foo';
54+
var request = http.get(url, noop);
55+
56+
t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct');
57+
t.end();
58+
59+
})

0 commit comments

Comments
 (0)