blob: 2c31610f741aad4a45e78c356450e08e1daf2418 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
19import { ConsoleLoggerService } from '../consolelogger.service';
Sean Condon28ecc5f2018-06-25 12:50:16 +010020import { UrlFnService } from './urlfn.service';
21import { FnService } from '../util/fn.service';
Sean Condonfd6d11b2018-06-02 20:29:49 +010022import { 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 +
Sean Condonbf7ff4f2019-03-17 16:18:42 +000074 ctx + '/onos/ui/';
Sean Condonfd6d11b2018-06-02 20:29:49 +010075 }
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');
Sean Condonbf7ff4f2019-03-17 16:18:42 +000090 expect(ufs.rsUrl('path')).toEqual('http://foo:123/onos/ui/rs/path');
Sean Condonfd6d11b2018-06-02 20:29:49 +010091 });
92
93 it('should return the correct (https) RS url', () => {
94 setLoc('https', 'foo', '123');
Sean Condonbf7ff4f2019-03-17 16:18:42 +000095 expect(ufs.rsUrl('path')).toEqual('https://foo:123/onos/ui/rs/path');
Sean Condonfd6d11b2018-06-02 20:29:49 +010096 });
97
98 it('should return the correct (ws) WS url', () => {
99 setLoc('http', 'foo', '123');
Sean Condonbf7ff4f2019-03-17 16:18:42 +0000100 expect(ufs.wsUrl('path')).toEqual('ws://foo:123/onos/ui/websock/path');
Sean Condonfd6d11b2018-06-02 20:29:49 +0100101 });
102
103 it('should return the correct (wss) WS url', () => {
104 setLoc('https', 'foo', '123');
Sean Condonbf7ff4f2019-03-17 16:18:42 +0000105 expect(ufs.wsUrl('path')).toEqual('wss://foo:123/onos/ui/websock/path');
Sean Condonfd6d11b2018-06-02 20:29:49 +0100106 });
107
108 it('should allow us to define an alternate WS port', () => {
109 setLoc('http', 'foo', '123');
Sean Condonbf7ff4f2019-03-17 16:18:42 +0000110 expect(ufs.wsUrl('xyyzy', '456')).toEqual('ws://foo:456/onos/ui/websock/xyyzy');
Sean Condonfd6d11b2018-06-02 20:29:49 +0100111 });
112
113 it('should allow us to define an alternate host', () => {
114 setLoc('http', 'foo', '123');
Sean Condonbf7ff4f2019-03-17 16:18:42 +0000115 expect(ufs.wsUrl('core', '456', 'bar')).toEqual('ws://bar:456/onos/ui/websock/core');
Sean Condonfd6d11b2018-06-02 20:29:49 +0100116 });
117
118 it('should allow us to inject an app context', () => {
119 setLoc('http', 'foo', '123', '/my/app');
Sean Condonbf7ff4f2019-03-17 16:18:42 +0000120 expect(ufs.wsUrl('path')).toEqual('ws://foo:123/my/app/onos/ui/websock/path');
Sean Condonfd6d11b2018-06-02 20:29:49 +0100121 });
122
Sean Condon83fc39f2018-04-19 18:56:13 +0100123});