File tree Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change 1
1
var http = module . exports ;
2
2
var EventEmitter = require ( 'events' ) . EventEmitter ;
3
3
var Request = require ( './lib/request' ) ;
4
+ var url = require ( 'url' )
4
5
5
6
http . request = function ( params , cb ) {
7
+ if ( typeof params === 'string' ) {
8
+ params = url . parse ( params )
9
+ }
6
10
if ( ! params ) params = { } ;
7
11
if ( ! params . host && ! params . port ) {
8
12
params . port = parseInt ( window . location . port , 10 ) ;
9
13
}
10
14
if ( ! params . host && params . hostname ) {
11
15
params . host = params . hostname ;
12
16
}
13
-
17
+
14
18
if ( ! params . scheme ) params . scheme = window . location . protocol . split ( ':' ) [ 0 ] ;
15
19
if ( ! params . host ) {
16
20
params . host = window . location . hostname || window . location . host ;
@@ -22,7 +26,7 @@ http.request = function (params, cb) {
22
26
params . host = params . host . split ( ':' ) [ 0 ] ;
23
27
}
24
28
if ( ! params . port ) params . port = params . scheme == 'https' ? 443 : 80 ;
25
-
29
+
26
30
var req = new Request ( new xhrHttp , params ) ;
27
31
if ( cb ) req . on ( 'response' , cb ) ;
28
32
return req ;
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ test('Test simple url string', function(t) {
27
27
28
28
29
29
test ( 'Test full url object' , function ( t ) {
30
- var url = {
30
+ var url = {
31
31
host : "localhost:8081" ,
32
32
hostname : "localhost" ,
33
33
href : "http://localhost:8081/api/foo?bar=baz" ,
@@ -46,4 +46,14 @@ test('Test full url object', function(t) {
46
46
t . equal ( request . uri , 'http://localhost:8081/api/foo?bar=baz' , 'Url should be correct' ) ;
47
47
t . end ( ) ;
48
48
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
+ } )
You can’t perform that action at this time.
0 commit comments