From f93f115e13528150cc2f2a56b8f0ffb090a7fcff Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 5 Feb 2015 12:41:25 +0100 Subject: [PATCH] Better and tested fixUrl(). --- packages/xo-lib/index.js | 29 ++++++++------------------ packages/xo-lib/index.spec.js | 38 +++++++++++++++++++++++++++++++++++ packages/xo-lib/package.json | 6 +++++- 3 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 packages/xo-lib/index.spec.js diff --git a/packages/xo-lib/index.js b/packages/xo-lib/index.js index c3e913a5d..a3d7d6787 100644 --- a/packages/xo-lib/index.js +++ b/packages/xo-lib/index.js @@ -15,30 +15,17 @@ function notConnected() { } // Fix URL if necessary. +var URL_RE = /^(?:(?:http|ws)(s)?:\/\/)?(.*?)\/*(?:\/api\/)?$/; function fixUrl(url) { - // Add HTTP protocol if missing. - if (!/^https?:/.test(url)) { - url = 'http:'+ url; - } + var matches = URL_RE.exec(url); + var isSecure = !!matches[1]; + var rest = matches[2]; - url = parseUrl(url); - - // Suffix path with /api/ if missing. - var path = url.pathname || ''; - if ('/' !== path[path.length - 1]) { - path += '/'; - } - if (!/\/api\/$/.test(path)) { - path += 'api/'; - } - - // Reconstruct the URL. return [ - url.protocol, '//', - url.host, - path, - url.search, - url.hash, + isSecure ? 'wss' : 'ws', + '://', + rest, + '/api/', ].join(''); } diff --git a/packages/xo-lib/index.spec.js b/packages/xo-lib/index.spec.js new file mode 100644 index 000000000..c9cbb4537 --- /dev/null +++ b/packages/xo-lib/index.spec.js @@ -0,0 +1,38 @@ +'use strict'; + +//==================================================================== + +var expect = require('must'); + +//==================================================================== + +describe('fixUrl()', function () { + var fixUrl = require('./').fixUrl; + + describe('protocol', function () { + it('is added if missing', function () { + expect(fixUrl('localhost/api/')).to.equal('ws://localhost/api/'); + }); + + it('HTTP(s) is converted to WS(s)', function () { + expect(fixUrl('http://localhost/api/')).to.equal('ws://localhost/api/'); + expect(fixUrl('https://localhost/api/')).to.equal('wss://localhost/api/'); + }); + + it('is not added if already present', function () { + expect(fixUrl('ws://localhost/api/')).to.equal('ws://localhost/api/'); + expect(fixUrl('wss://localhost/api/')).to.equal('wss://localhost/api/'); + }); + }); + + describe('/api/ path', function () { + it('is added if missing', function () { + expect(fixUrl('ws://localhost')).to.equal('ws://localhost/api/'); + expect(fixUrl('ws://localhost/')).to.equal('ws://localhost/api/'); + }); + + it('is not added if already present', function () { + expect(fixUrl('ws://localhost/api/')).to.equal('ws://localhost/api/'); + }); + }); +}); diff --git a/packages/xo-lib/package.json b/packages/xo-lib/package.json index 7fcebc70e..c58d9ace6 100644 --- a/packages/xo-lib/package.json +++ b/packages/xo-lib/package.json @@ -19,7 +19,7 @@ "node": ">=0.8.0" }, "scripts": { - "test": "gulp test" + "test": "mocha index.spec.js" }, "files": [ "index.js" @@ -29,5 +29,9 @@ "lodash.assign": "^2.4.1", "lodash.foreach": "^2.4.1", "ws": "^0.4.31" + }, + "devDependencies": { + "mocha": "^2.1.0", + "must": "^0.12.0" } }