Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { TestBed, inject } from '@angular/core/testing'; |
| 17 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame^] | 18 | import { LogService } from '../../log.service'; |
| 19 | import { ConsoleLoggerService } from '../../consolelogger.service'; |
| 20 | import { UrlFnService } from './urlfn.service'; |
| 21 | import { FnService } from '../util/fn.service'; |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 22 | import { ActivatedRoute, Params } from '@angular/router'; |
| 23 | import { of } from 'rxjs'; |
| 24 | |
| 25 | class MockActivatedRoute extends ActivatedRoute { |
| 26 | constructor(params: Params) { |
| 27 | super(); |
| 28 | this.queryParams = of(params); |
| 29 | } |
| 30 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * ONOS GUI -- Remote -- General Functions - Unit Tests |
| 34 | */ |
| 35 | describe('UrlFnService', () => { |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 36 | let log: LogService; |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 37 | let ufs: UrlFnService; |
| 38 | let fs: FnService; |
| 39 | let ar: MockActivatedRoute; |
| 40 | let windowMock: Window; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 41 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 42 | beforeEach(() => { |
| 43 | log = new ConsoleLoggerService(); |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 44 | 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 Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 57 | |
| 58 | TestBed.configureTestingModule({ |
| 59 | providers: [UrlFnService, |
| 60 | { provide: LogService, useValue: log }, |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 61 | { provide: 'Window', useFactory: (() => windowMock ) }, |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 62 | ] |
| 63 | }); |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 64 | |
| 65 | ufs = TestBed.get(UrlFnService); |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 66 | }); |
| 67 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 68 | 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 Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 123 | }); |