File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,12 @@ http.request = function (opts, cb) {
11
11
else
12
12
opts = extend ( opts )
13
13
14
- var protocol = opts . protocol || ''
14
+ // Normally, the page is loaded from http or https, so not specifying a protocol
15
+ // will result in a (valid) protocol-relative url. However, this won't work if
16
+ // the protocol is something else, like 'file:'
17
+ var defaultProtocol = global . location . protocol . search ( / ^ h t t p s ? : $ / ) === - 1 ? 'http:' : ''
18
+
19
+ var protocol = opts . protocol || defaultProtocol
15
20
var host = opts . hostname || opts . host
16
21
var port = opts . port
17
22
var path = opts . path || '/'
Original file line number Diff line number Diff line change @@ -64,6 +64,25 @@ test('Test alt protocol', function(t) {
64
64
t . end ( )
65
65
} )
66
66
67
+ test ( 'Test page with \'file:\' protocol' , function ( t ) {
68
+ var params = {
69
+ hostname : 'localhost' ,
70
+ port : 3000 ,
71
+ path : '/bar'
72
+ }
73
+
74
+ var fileLocation = 'file:///home/me/stuff/index.html'
75
+
76
+ var normalLocation = global . location
77
+ global . location = url . parse ( fileLocation ) // Temporarily change the location
78
+ var request = http . get ( params , noop )
79
+ global . location = normalLocation // Reset the location
80
+
81
+ var resolved = url . resolve ( fileLocation , request . _opts . url )
82
+ t . equal ( resolved , 'http://localhost:3000/bar' , 'Url should be correct' )
83
+ t . end ( )
84
+ } )
85
+
67
86
test ( 'Test string as parameters' , function ( t ) {
68
87
var testUrl = '/api/foo'
69
88
var request = http . get ( testUrl , noop )
You can’t perform that action at this time.
0 commit comments