Cleaned up urlBase() readability.
Updated unit tests, and added a test for the app context in the URL.

Change-Id: I110b62ff8366a503c3309df0f26a7dd213ae0c5f
diff --git a/web/gui/src/main/webapp/app/fw/remote/urlfn.js b/web/gui/src/main/webapp/app/fw/remote/urlfn.js
index 03ae09e..bf142d4 100644
--- a/web/gui/src/main/webapp/app/fw/remote/urlfn.js
+++ b/web/gui/src/main/webapp/app/fw/remote/urlfn.js
@@ -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) {
diff --git a/web/gui/src/main/webapp/tests/app/fw/remote/rest-spec.js b/web/gui/src/main/webapp/tests/app/fw/remote/rest-spec.js
index cd859ba..406da03 100644
--- a/web/gui/src/main/webapp/tests/app/fw/remote/rest-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/remote/rest-spec.js
@@ -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) {
diff --git a/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js b/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
index a998e0a..7b9c114 100644
--- a/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/remote/urlfn-spec.js
@@ -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 @@
         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 @@
         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');
+    });
 });
diff --git a/web/gui/src/main/webapp/tests/app/fw/remote/websocket-spec.js b/web/gui/src/main/webapp/tests/app/fw/remote/websocket-spec.js
index c3599cb..6b0d4a7 100644
--- a/web/gui/src/main/webapp/tests/app/fw/remote/websocket-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/remote/websocket-spec.js
@@ -57,6 +57,9 @@
                 port: function () { return '80'; },
                 search: function() {
                     return {debug: 'true'};
+                },
+                absUrl: function () {
+                    return 'ws://foo:123/onos/ui/websock/path';
                 }
             };
         })