Better and tested fixUrl().
This commit is contained in:
parent
debcf086b5
commit
f93f115e13
@ -15,30 +15,17 @@ function notConnected() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fix URL if necessary.
|
// Fix URL if necessary.
|
||||||
|
var URL_RE = /^(?:(?:http|ws)(s)?:\/\/)?(.*?)\/*(?:\/api\/)?$/;
|
||||||
function fixUrl(url) {
|
function fixUrl(url) {
|
||||||
// Add HTTP protocol if missing.
|
var matches = URL_RE.exec(url);
|
||||||
if (!/^https?:/.test(url)) {
|
var isSecure = !!matches[1];
|
||||||
url = 'http:'+ url;
|
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 [
|
return [
|
||||||
url.protocol, '//',
|
isSecure ? 'wss' : 'ws',
|
||||||
url.host,
|
'://',
|
||||||
path,
|
rest,
|
||||||
url.search,
|
'/api/',
|
||||||
url.hash,
|
|
||||||
].join('');
|
].join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
packages/xo-lib/index.spec.js
Normal file
38
packages/xo-lib/index.spec.js
Normal file
@ -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/');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -19,7 +19,7 @@
|
|||||||
"node": ">=0.8.0"
|
"node": ">=0.8.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "gulp test"
|
"test": "mocha index.spec.js"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"index.js"
|
"index.js"
|
||||||
@ -29,5 +29,9 @@
|
|||||||
"lodash.assign": "^2.4.1",
|
"lodash.assign": "^2.4.1",
|
||||||
"lodash.foreach": "^2.4.1",
|
"lodash.foreach": "^2.4.1",
|
||||||
"ws": "^0.4.31"
|
"ws": "^0.4.31"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^2.1.0",
|
||||||
|
"must": "^0.12.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user