blob: 0aa14ea84780e3fde923d93a5fc5e66a75c7cca8 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2015-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { TestBed, inject } from '@angular/core/testing';
17
Sean Condon49e15be2018-05-16 16:58:29 +010018import { LogService } from '../../../../app/log.service';
19import { ConsoleLoggerService } from '../../../../app/consolelogger.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010020import { UrlFnService } from '../../../../app/fw/remote/urlfn.service';
Sean Condonfd6d11b2018-06-02 20:29:49 +010021import { FnService } from '../../../../app/fw/util/fn.service';
22import { ActivatedRoute, Params } from '@angular/router';
23import { of } from 'rxjs';
24
25class MockActivatedRoute extends ActivatedRoute {
26 constructor(params: Params) {
27 super();
28 this.queryParams = of(params);
29 }
30}
Sean Condon83fc39f2018-04-19 18:56:13 +010031
32/**
33 * ONOS GUI -- Remote -- General Functions - Unit Tests
34 */
35describe('UrlFnService', () => {
Sean Condon49e15be2018-05-16 16:58:29 +010036 let log: LogService;
Sean Condonfd6d11b2018-06-02 20:29:49 +010037 let ufs: UrlFnService;
38 let fs: FnService;
39 let ar: MockActivatedRoute;
40 let windowMock: Window;
Sean Condon83fc39f2018-04-19 18:56:13 +010041
Sean Condon49e15be2018-05-16 16:58:29 +010042 beforeEach(() => {
43 log = new ConsoleLoggerService();
Sean Condonfd6d11b2018-06-02 20:29:49 +010044 ar = new MockActivatedRoute({'debug': 'TestService'});
45 windowMock = <any>{
46 location: <any> {
47 hostname: '',
48 host: '',
49 port: '',
50 protocol: '',
51 search: { debug: 'true'},
52 href: ''
53 }
54 };
55
56 fs = new FnService(ar, log, windowMock);
Sean Condon49e15be2018-05-16 16:58:29 +010057
58 TestBed.configureTestingModule({
59 providers: [UrlFnService,
60 { provide: LogService, useValue: log },
Sean Condona00bf382018-06-23 07:54:01 +010061 { provide: 'Window', useFactory: (() => windowMock ) },
Sean Condon49e15be2018-05-16 16:58:29 +010062 ]
63 });
Sean Condonfd6d11b2018-06-02 20:29:49 +010064
65 ufs = TestBed.get(UrlFnService);
Sean Condon49e15be2018-05-16 16:58:29 +010066 });
67
Sean Condonfd6d11b2018-06-02 20:29:49 +010068 function setLoc(prot: string, h: string, p: string, ctx: string = '') {
69 windowMock.location.host = h;
70 windowMock.location.hostname = h;
71 windowMock.location.port = p;
72 windowMock.location.protocol = prot;
73 windowMock.location.href = prot + '://' + h + ':' + p +
74 ctx + '/onos/ui/';
75 }
76
77 it('should define UrlFnService', () => {
78 expect(ufs).toBeDefined();
79 });
80
81 it('should define api functions', () => {
82 expect(fs.areFunctions(ufs, [
83 'rsUrl', 'wsUrl', 'urlBase', 'httpPrefix',
84 'wsPrefix', 'matchSecure'
85 ])).toBeTruthy();
86 });
87
88 it('should return the correct (http) RS url', () => {
89 setLoc('http', 'foo', '123');
90 expect(ufs.rsUrl('path')).toEqual('http://foo:123/onos/ui/rs/path');
91 });
92
93 it('should return the correct (https) RS url', () => {
94 setLoc('https', 'foo', '123');
95 expect(ufs.rsUrl('path')).toEqual('https://foo:123/onos/ui/rs/path');
96 });
97
98 it('should return the correct (ws) WS url', () => {
99 setLoc('http', 'foo', '123');
100 expect(ufs.wsUrl('path')).toEqual('ws://foo:123/onos/ui/websock/path');
101 });
102
103 it('should return the correct (wss) WS url', () => {
104 setLoc('https', 'foo', '123');
105 expect(ufs.wsUrl('path')).toEqual('wss://foo:123/onos/ui/websock/path');
106 });
107
108 it('should allow us to define an alternate WS port', () => {
109 setLoc('http', 'foo', '123');
110 expect(ufs.wsUrl('xyyzy', '456')).toEqual('ws://foo:456/onos/ui/websock/xyyzy');
111 });
112
113 it('should allow us to define an alternate host', () => {
114 setLoc('http', 'foo', '123');
115 expect(ufs.wsUrl('core', '456', 'bar')).toEqual('ws://bar:456/onos/ui/websock/core');
116 });
117
118 it('should allow us to inject an app context', () => {
119 setLoc('http', 'foo', '123', '/my/app');
120 expect(ufs.wsUrl('path')).toEqual('ws://foo:123/my/app/onos/ui/websock/path');
121 });
122
Sean Condon83fc39f2018-04-19 18:56:13 +0100123});