Cleaned up urlBase() readability.
Updated unit tests, and added a test for the app context in the URL. Change-Id: I110b62ff8366a503c3309df0f26a7dd213ae0c5f (cherry picked from commit fa875bb1)
Showing
4 changed files
with
35 additions
and
18 deletions
... | @@ -34,19 +34,20 @@ | ... | @@ -34,19 +34,20 @@ |
34 | } | 34 | } |
35 | 35 | ||
36 | function urlBase(protocol, port, host) { | 36 | function urlBase(protocol, port, host) { |
37 | - // A little bit of funky here. It is possible that ONOS sits behind a proxy | 37 | + // A little bit of funky here. It is possible that ONOS sits |
38 | - // and has an app prefix, e.g. http://host:port/my/app/onos/ui... This bit | 38 | + // behind a proxy and has an app prefix, e.g. |
39 | - // of regex grabs everything after the host:port and before the uiContext | 39 | + // http://host:port/my/app/onos/ui... |
40 | - // (/onos/ui/) and uses that as an app prefix by inserting it back into | 40 | + // This bit of regex grabs everything after the host:port and |
41 | - // the WS URL, if no prefix, then no insert. | 41 | + // before the uiContext (/onos/ui/) and uses that as an app |
42 | - var prefix = "" | 42 | + // prefix by inserting it back into the WS URL. |
43 | - if ($loc.absUrl) { | 43 | + // If no prefix, then no insert. |
44 | - var p = $loc.absUrl().match(".*//[^/]+/(.+)"+uiContext); | 44 | + |
45 | - prefix = p ? '/' + p[1] : ''; | 45 | + var match = $loc.absUrl().match('.*//[^/]+/(.+)' + uiContext), |
46 | - } | 46 | + appPrefix = match ? '/' + match[1] : ''; |
47 | 47 | ||
48 | return matchSecure(protocol) + '://' + | 48 | return matchSecure(protocol) + '://' + |
49 | - (host || $loc.host()) + ':' + (port || $loc.port()) + prefix; | 49 | + (host || $loc.host()) + ':' + |
50 | + (port || $loc.port()) + appPrefix; | ||
50 | } | 51 | } |
51 | 52 | ||
52 | function httpPrefix(suffix) { | 53 | function httpPrefix(suffix) { | ... | ... |
... | @@ -18,21 +18,24 @@ | ... | @@ -18,21 +18,24 @@ |
18 | ONOS GUI -- Remote -- REST Service - Unit Tests | 18 | ONOS GUI -- Remote -- REST Service - Unit Tests |
19 | */ | 19 | */ |
20 | describe('factory: fw/remote/rest.js', function() { | 20 | describe('factory: fw/remote/rest.js', function() { |
21 | - var $log, $httpBackend, fs, rs, promise; | 21 | + var $log, $httpBackend, fs, rs; |
22 | 22 | ||
23 | beforeEach(module('onosUtil', 'onosRemote')); | 23 | beforeEach(module('onosUtil', 'onosRemote')); |
24 | 24 | ||
25 | beforeEach(module(function($provide) { | 25 | beforeEach(module(function($provide) { |
26 | - $provide.factory('$location', function (){ | 26 | + $provide.factory('$location', function () { |
27 | return { | 27 | return { |
28 | protocol: function () { return 'http'; }, | 28 | protocol: function () { return 'http'; }, |
29 | host: function () { return 'foo'; }, | 29 | host: function () { return 'foo'; }, |
30 | port: function () { return '80'; }, | 30 | port: function () { return '80'; }, |
31 | search: function() { | 31 | search: function() { |
32 | return {debug: 'true'}; | 32 | return {debug: 'true'}; |
33 | + }, | ||
34 | + absUrl: function () { | ||
35 | + return 'http://foo:123/onos/ui/rs/path'; | ||
33 | } | 36 | } |
34 | }; | 37 | }; |
35 | - }) | 38 | + }); |
36 | })); | 39 | })); |
37 | 40 | ||
38 | beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) { | 41 | beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) { | ... | ... |
... | @@ -21,21 +21,25 @@ | ... | @@ -21,21 +21,25 @@ |
21 | describe('factory: fw/remote/urlfn.js', function () { | 21 | describe('factory: fw/remote/urlfn.js', function () { |
22 | var $log, $loc, ufs, fs; | 22 | var $log, $loc, ufs, fs; |
23 | 23 | ||
24 | - var protocol, host, port; | 24 | + var protocol, host, port, context; |
25 | 25 | ||
26 | beforeEach(module('onosRemote')); | 26 | beforeEach(module('onosRemote')); |
27 | 27 | ||
28 | beforeEach(module(function($provide) { | 28 | beforeEach(module(function($provide) { |
29 | - $provide.factory('$location', function (){ | 29 | + $provide.factory('$location', function () { |
30 | return { | 30 | return { |
31 | protocol: function () { return protocol; }, | 31 | protocol: function () { return protocol; }, |
32 | host: function () { return host; }, | 32 | host: function () { return host; }, |
33 | port: function () { return port; }, | 33 | port: function () { return port; }, |
34 | search: function() { | 34 | search: function() { |
35 | return {debug: 'true'}; | 35 | return {debug: 'true'}; |
36 | + }, | ||
37 | + absUrl: function () { | ||
38 | + return protocol + '://' + host + ':' + port + | ||
39 | + context + '/onos/ui/'; | ||
36 | } | 40 | } |
37 | }; | 41 | }; |
38 | - }) | 42 | + }); |
39 | })); | 43 | })); |
40 | 44 | ||
41 | beforeEach(inject(function (_$log_, $location, UrlFnService, FnService) { | 45 | beforeEach(inject(function (_$log_, $location, UrlFnService, FnService) { |
... | @@ -45,10 +49,11 @@ describe('factory: fw/remote/urlfn.js', function () { | ... | @@ -45,10 +49,11 @@ describe('factory: fw/remote/urlfn.js', function () { |
45 | fs = FnService; | 49 | fs = FnService; |
46 | })); | 50 | })); |
47 | 51 | ||
48 | - function setLoc(prot, h, p) { | 52 | + function setLoc(prot, h, p, ctx) { |
49 | protocol = prot; | 53 | protocol = prot; |
50 | host = h; | 54 | host = h; |
51 | port = p; | 55 | port = p; |
56 | + context = ctx || ''; | ||
52 | } | 57 | } |
53 | 58 | ||
54 | it('should define UrlFnService', function () { | 59 | it('should define UrlFnService', function () { |
... | @@ -90,4 +95,9 @@ describe('factory: fw/remote/urlfn.js', function () { | ... | @@ -90,4 +95,9 @@ describe('factory: fw/remote/urlfn.js', function () { |
90 | setLoc('http', 'foo', '123'); | 95 | setLoc('http', 'foo', '123'); |
91 | expect(ufs.wsUrl('core', 456, 'bar')).toEqual('ws://bar:456/onos/ui/websock/core'); | 96 | expect(ufs.wsUrl('core', 456, 'bar')).toEqual('ws://bar:456/onos/ui/websock/core'); |
92 | }); | 97 | }); |
98 | + | ||
99 | + it('should allow us to inject an app context', function () { | ||
100 | + setLoc('http', 'foo', '123', '/my/app'); | ||
101 | + expect(ufs.wsUrl('path')).toEqual('ws://foo:123/my/app/onos/ui/websock/path'); | ||
102 | + }); | ||
93 | }); | 103 | }); | ... | ... |
... | @@ -57,6 +57,9 @@ describe('factory: fw/remote/websocket.js', function () { | ... | @@ -57,6 +57,9 @@ describe('factory: fw/remote/websocket.js', function () { |
57 | port: function () { return '80'; }, | 57 | port: function () { return '80'; }, |
58 | search: function() { | 58 | search: function() { |
59 | return {debug: 'true'}; | 59 | return {debug: 'true'}; |
60 | + }, | ||
61 | + absUrl: function () { | ||
62 | + return 'ws://foo:123/onos/ui/websock/path'; | ||
60 | } | 63 | } |
61 | }; | 64 | }; |
62 | }) | 65 | }) | ... | ... |
-
Please register or login to post a comment