From 21bd5ba3768bfd1618a5135a7357d00cb6ab2c62 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 12 Feb 2015 12:22:49 +0100 Subject: [PATCH] fixUrl() properly handles search and hash parts. --- packages/xo-lib/index.js | 8 +++++--- packages/xo-lib/index.spec.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/xo-lib/index.js b/packages/xo-lib/index.js index 18f26221b..61894a8a4 100644 --- a/packages/xo-lib/index.js +++ b/packages/xo-lib/index.js @@ -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; diff --git a/packages/xo-lib/index.spec.js b/packages/xo-lib/index.spec.js index c9cbb4537..d00e88292 100644 --- a/packages/xo-lib/index.spec.js +++ b/packages/xo-lib/index.spec.js @@ -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'); + }); }); });