|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import { expect } from 'chai'; |
| 18 | +import { |
| 19 | + getDefaultEmulatorHost, |
| 20 | + getDefaultEmulatorHostnameAndPort |
| 21 | +} from '../src/defaults'; |
| 22 | +import { getGlobal } from '../src/environment'; |
| 23 | + |
| 24 | +describe('getDefaultEmulatorHost', () => { |
| 25 | + after(() => { |
| 26 | + delete getGlobal().__FIREBASE_DEFAULTS__; |
| 27 | + }); |
| 28 | + |
| 29 | + context('with no config', () => { |
| 30 | + it('returns undefined', () => { |
| 31 | + expect(getDefaultEmulatorHost('firestore')).to.be.undefined; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + context('with global config not listing the emulator', () => { |
| 36 | + before(() => { |
| 37 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 38 | + emulatorHosts: { |
| 39 | + /* no firestore */ |
| 40 | + database: '127.0.0.1:8080' |
| 41 | + } |
| 42 | + }; |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns undefined', () => { |
| 46 | + expect(getDefaultEmulatorHost('firestore')).to.be.undefined; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + context('with IPv4 hostname in global config', () => { |
| 51 | + before(() => { |
| 52 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 53 | + emulatorHosts: { |
| 54 | + firestore: '127.0.0.1:8080' |
| 55 | + } |
| 56 | + }; |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns host', () => { |
| 60 | + expect(getDefaultEmulatorHost('firestore')).to.equal('127.0.0.1:8080'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + context('with quoted IPv6 hostname in global config', () => { |
| 65 | + before(() => { |
| 66 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 67 | + emulatorHosts: { |
| 68 | + firestore: '[::1]:8080' |
| 69 | + } |
| 70 | + }; |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns host', () => { |
| 74 | + expect(getDefaultEmulatorHost('firestore')).to.equal('[::1]:8080'); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('getDefaultEmulatorHostnameAndPort', () => { |
| 80 | + after(() => { |
| 81 | + delete getGlobal().__FIREBASE_DEFAULTS__; |
| 82 | + }); |
| 83 | + |
| 84 | + context('with no config', () => { |
| 85 | + it('returns undefined', () => { |
| 86 | + expect(getDefaultEmulatorHostnameAndPort('firestore')).to.be.undefined; |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + context('with global config not listing the emulator', () => { |
| 91 | + before(() => { |
| 92 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 93 | + emulatorHosts: { |
| 94 | + /* no firestore */ |
| 95 | + database: '127.0.0.1:8080' |
| 96 | + } |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns undefined', () => { |
| 101 | + expect(getDefaultEmulatorHostnameAndPort('firestore')).to.be.undefined; |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + context('with IPv4 hostname in global config', () => { |
| 106 | + before(() => { |
| 107 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 108 | + emulatorHosts: { |
| 109 | + firestore: '127.0.0.1:8080' |
| 110 | + } |
| 111 | + }; |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns hostname and port splitted', () => { |
| 115 | + expect(getDefaultEmulatorHostnameAndPort('firestore')).to.eql([ |
| 116 | + '127.0.0.1', |
| 117 | + 8080 |
| 118 | + ]); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + context('with quoted IPv6 hostname in global config', () => { |
| 123 | + before(() => { |
| 124 | + getGlobal().__FIREBASE_DEFAULTS__ = { |
| 125 | + emulatorHosts: { |
| 126 | + firestore: '[::1]:8080' |
| 127 | + } |
| 128 | + }; |
| 129 | + }); |
| 130 | + |
| 131 | + it('returns unquoted hostname and port splitted', () => { |
| 132 | + expect(getDefaultEmulatorHostnameAndPort('firestore')).to.eql([ |
| 133 | + '::1', |
| 134 | + 8080 |
| 135 | + ]); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments