fixUrl() properly handles search and hash parts.

This commit is contained in:
Julien Fontanet 2015-02-12 12:22:49 +01:00
parent 28d5fb1822
commit 21bd5ba376
2 changed files with 15 additions and 3 deletions

View File

@ -74,17 +74,19 @@ function fibonacci(start) {
//====================================================================
// Fix URL if necessary.
var URL_RE = /^(?:(?:http|ws)(s)?:\/\/)?(.*?)\/*(?:\/api\/)?$/;
var URL_RE = /^(?:(?:http|ws)(s)?:\/\/)?(.*?)\/*(?:\/api\/)?(\?.*?)?(?:#.*)?$/;
function fixUrl(url) {
var matches = URL_RE.exec(url);
var isSecure = !!matches[1];
var rest = matches[2];
var hostAndPath = matches[2];
var search = matches[3];
return [
isSecure ? 'wss' : 'ws',
'://',
rest,
hostAndPath,
'/api/',
search,
].join('');
}
exports.fixUrl = fixUrl;

View File

@ -6,6 +6,8 @@ var expect = require('must');
//====================================================================
/* jshint mocha: true */
describe('fixUrl()', function () {
var fixUrl = require('./').fixUrl;
@ -34,5 +36,13 @@ describe('fixUrl()', function () {
it('is not added if already present', function () {
expect(fixUrl('ws://localhost/api/')).to.equal('ws://localhost/api/');
});
it('removes the hash part', function () {
expect(fixUrl('ws://localhost/#foo')).to.equal('ws://localhost/api/');
});
it('conserve the search part', function () {
expect(fixUrl('ws://localhost/?foo')).to.equal('ws://localhost/api/?foo');
});
});
});