Simon Hunt

Cleaned up urlBase() readability.

Updated unit tests, and added a test for the app context in the URL.

Change-Id: I110b62ff8366a503c3309df0f26a7dd213ae0c5f
......@@ -34,19 +34,20 @@
}
function urlBase(protocol, port, host) {
// A little bit of funky here. It is possible that ONOS sits behind a proxy
// and has an app prefix, e.g. http://host:port/my/app/onos/ui... This bit
// of regex grabs everything after the host:port and before the uiContext
// (/onos/ui/) and uses that as an app prefix by inserting it back into
// the WS URL, if no prefix, then no insert.
var prefix = ""
if ($loc.absUrl) {
var p = $loc.absUrl().match(".*//[^/]+/(.+)"+uiContext);
prefix = p ? '/' + p[1] : '';
}
// A little bit of funky here. It is possible that ONOS sits
// behind a proxy and has an app prefix, e.g.
// http://host:port/my/app/onos/ui...
// This bit of regex grabs everything after the host:port and
// before the uiContext (/onos/ui/) and uses that as an app
// prefix by inserting it back into the WS URL.
// If no prefix, then no insert.
var match = $loc.absUrl().match('.*//[^/]+/(.+)' + uiContext),
appPrefix = match ? '/' + match[1] : '';
return matchSecure(protocol) + '://' +
(host || $loc.host()) + ':' + (port || $loc.port()) + prefix;
(host || $loc.host()) + ':' +
(port || $loc.port()) + appPrefix;
}
function httpPrefix(suffix) {
......
......@@ -18,21 +18,24 @@
ONOS GUI -- Remote -- REST Service - Unit Tests
*/
describe('factory: fw/remote/rest.js', function() {
var $log, $httpBackend, fs, rs, promise;
var $log, $httpBackend, fs, rs;
beforeEach(module('onosUtil', 'onosRemote'));
beforeEach(module(function($provide) {
$provide.factory('$location', function (){
$provide.factory('$location', function () {
return {
protocol: function () { return 'http'; },
host: function () { return 'foo'; },
port: function () { return '80'; },
search: function() {
return {debug: 'true'};
},
absUrl: function () {
return 'http://foo:123/onos/ui/rs/path';
}
};
})
});
}));
beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) {
......
......@@ -21,21 +21,25 @@
describe('factory: fw/remote/urlfn.js', function () {
var $log, $loc, ufs, fs;
var protocol, host, port;
var protocol, host, port, context;
beforeEach(module('onosRemote'));
beforeEach(module(function($provide) {
$provide.factory('$location', function (){
$provide.factory('$location', function () {
return {
protocol: function () { return protocol; },
host: function () { return host; },
port: function () { return port; },
search: function() {
return {debug: 'true'};
},
absUrl: function () {
return protocol + '://' + host + ':' + port +
context + '/onos/ui/';
}
};
})
});
}));
beforeEach(inject(function (_$log_, $location, UrlFnService, FnService) {
......@@ -45,10 +49,11 @@ describe('factory: fw/remote/urlfn.js', function () {
fs = FnService;
}));
function setLoc(prot, h, p) {
function setLoc(prot, h, p, ctx) {
protocol = prot;
host = h;
port = p;
context = ctx || '';
}
it('should define UrlFnService', function () {
......@@ -90,4 +95,9 @@ describe('factory: fw/remote/urlfn.js', function () {
setLoc('http', 'foo', '123');
expect(ufs.wsUrl('core', 456, 'bar')).toEqual('ws://bar:456/onos/ui/websock/core');
});
it('should allow us to inject an app context', function () {
setLoc('http', 'foo', '123', '/my/app');
expect(ufs.wsUrl('path')).toEqual('ws://foo:123/my/app/onos/ui/websock/path');
});
});
......
......@@ -57,6 +57,9 @@ describe('factory: fw/remote/websocket.js', function () {
port: function () { return '80'; },
search: function() {
return {debug: 'true'};
},
absUrl: function () {
return 'ws://foo:123/onos/ui/websock/path';
}
};
})
......