Skip to content

Commit 750d8e8

Browse files
authored
[fix] Fixes relative path resolving #199 #200 (#201)
* [fix] Fixes relative path resolving #199 #200 * [test] Additional extractProtocol tests
1 parent 3ac7774 commit 750d8e8

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ function extractProtocol(address) {
118118

119119
var match = protocolre.exec(address)
120120
, protocol = match[1] ? match[1].toLowerCase() : ''
121-
, slashes = !!(match[2] && match[2].length >= 2);
121+
, slashes = !!(match[2] && match[2].length >= 2)
122+
, rest = match[2] && match[2].length === 1 ? '/' + match[3] : match[3];
122123

123124
return {
124125
protocol: protocol,
125126
slashes: slashes,
126-
rest: match[3]
127+
rest: rest
127128
};
128129
}
129130

test/test.js

+28
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ describe('url-parse', function () {
8383
});
8484
});
8585

86+
it('correctly resolves paths', function () {
87+
assume(parse.extractProtocol('/foo')).eql({
88+
slashes: false,
89+
protocol: '',
90+
rest: '/foo'
91+
});
92+
93+
assume(parse.extractProtocol('//foo/bar')).eql({
94+
slashes: true,
95+
protocol: '',
96+
rest: 'foo/bar'
97+
});
98+
});
99+
86100
it('does not truncate the input string', function () {
87101
var input = 'foo\nbar\rbaz\u2028qux\u2029';
88102

@@ -209,6 +223,20 @@ describe('url-parse', function () {
209223
assume(parsed.href).equals('http://example.com/');
210224
});
211225

226+
it('correctly parses pathnames for relative paths', function () {
227+
var url = '/dataApi/PROD/ws'
228+
, parsed = parse(url, 'http://localhost:3000/PROD/trends');
229+
230+
assume(parsed.pathname).equals('/dataApi/PROD/ws');
231+
232+
url = '/sections/?project=default'
233+
parsed = parse(url, 'http://example.com/foo/bar');
234+
235+
assume(parsed.pathname).equals('/sections/');
236+
assume(parsed.hostname).equals('example.com');
237+
assume(parsed.href).equals('http://example.com/sections/?project=default');
238+
});
239+
212240
it('does not care about spaces', function () {
213241
var url = 'http://x.com/path?that\'s#all, folks'
214242
, parsed = parse(url);

0 commit comments

Comments
 (0)