|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2019 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import ServerAddress from '../../src/v1/internal/server-address'; |
| 21 | + |
| 22 | +describe('ServerAddress', () => { |
| 23 | + |
| 24 | + it('should construct with correct values', () => { |
| 25 | + verifyAddress(ServerAddress.fromUrl('host.some.domain:8687'), { |
| 26 | + host: 'host.some.domain', |
| 27 | + port: 8687, |
| 28 | + hostAndPort: 'host.some.domain:8687', |
| 29 | + key: 'host.some.domain:8687', |
| 30 | + toString: 'host.some.domain:8687' |
| 31 | + }); |
| 32 | + |
| 33 | + verifyAddress(ServerAddress.fromUrl('http://host.some.domain:8687'), { |
| 34 | + host: 'host.some.domain', |
| 35 | + port: 8687, |
| 36 | + hostAndPort: 'host.some.domain:8687', |
| 37 | + key: 'host.some.domain:8687', |
| 38 | + toString: 'host.some.domain:8687' |
| 39 | + }); |
| 40 | + |
| 41 | + verifyAddress(ServerAddress.fromUrl('host2.some.domain'), { |
| 42 | + host: 'host2.some.domain', |
| 43 | + port: 7687, |
| 44 | + hostAndPort: 'host2.some.domain:7687', |
| 45 | + key: 'host2.some.domain:7687', |
| 46 | + toString: 'host2.some.domain:7687' |
| 47 | + }); |
| 48 | + |
| 49 | + verifyAddress(ServerAddress.fromUrl('https://host2.some.domain'), { |
| 50 | + host: 'host2.some.domain', |
| 51 | + port: 7473, |
| 52 | + hostAndPort: 'host2.some.domain:7473', |
| 53 | + key: 'host2.some.domain:7473', |
| 54 | + toString: 'host2.some.domain:7473' |
| 55 | + }); |
| 56 | + |
| 57 | + verifyAddress(ServerAddress.fromUrl('10.10.192.0'), { |
| 58 | + host: '10.10.192.0', |
| 59 | + port: 7687, |
| 60 | + hostAndPort: '10.10.192.0:7687', |
| 61 | + key: '10.10.192.0:7687', |
| 62 | + toString: '10.10.192.0:7687' |
| 63 | + }); |
| 64 | + |
| 65 | + verifyAddress(ServerAddress.fromUrl('[1afc:0:a33:85a3::ff2f]:8889'), { |
| 66 | + host: '1afc:0:a33:85a3::ff2f', |
| 67 | + port: 8889, |
| 68 | + hostAndPort: '[1afc:0:a33:85a3::ff2f]:8889', |
| 69 | + key: '[1afc:0:a33:85a3::ff2f]:8889', |
| 70 | + toString: '[1afc:0:a33:85a3::ff2f]:8889' |
| 71 | + }); |
| 72 | + |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return correct values when resolved', () => { |
| 76 | + const address = ServerAddress.fromUrl('host.some.domain:8787'); |
| 77 | + const resolved1 = address.resolveWith('172.0.0.1'); |
| 78 | + const resolved2 = address.resolveWith('172.0.1.1'); |
| 79 | + |
| 80 | + verifyAddress(resolved1, { |
| 81 | + host: 'host.some.domain', |
| 82 | + port: 8787, |
| 83 | + hostAndPort: 'host.some.domain:8787', |
| 84 | + key: 'host.some.domain:8787', |
| 85 | + toString: 'host.some.domain:8787(172.0.0.1)', |
| 86 | + resolvedHost: '172.0.0.1' |
| 87 | + }); |
| 88 | + |
| 89 | + verifyAddress(resolved2, { |
| 90 | + host: 'host.some.domain', |
| 91 | + port: 8787, |
| 92 | + hostAndPort: 'host.some.domain:8787', |
| 93 | + key: 'host.some.domain:8787', |
| 94 | + toString: 'host.some.domain:8787(172.0.1.1)', |
| 95 | + resolvedHost: '172.0.1.1' |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should not lose host info if resolved', () => { |
| 100 | + const address = ServerAddress.fromUrl('host.some.domain:8787'); |
| 101 | + const resolved1 = address.resolveWith('192.168.0.1'); |
| 102 | + const resolved2 = resolved1.resolveWith('192.168.100.1'); |
| 103 | + |
| 104 | + verifyAddress(resolved2, { |
| 105 | + host: 'host.some.domain', |
| 106 | + port: 8787, |
| 107 | + hostAndPort: 'host.some.domain:8787', |
| 108 | + key: 'host.some.domain:8787', |
| 109 | + toString: 'host.some.domain:8787(192.168.100.1)', |
| 110 | + resolvedHost: '192.168.100.1' |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +function verifyAddress(address, { host, port, hostAndPort, key, toString, resolvedHost = null } = {}) { |
| 116 | + expect(address.host()).toEqual(host); |
| 117 | + expect(address.port()).toEqual(port); |
| 118 | + expect(address.asHostPort()).toEqual(hostAndPort); |
| 119 | + expect(address.asKey()).toEqual(key); |
| 120 | + expect(address.toString()).toEqual(toString); |
| 121 | + expect(address.resolvedHost()).toEqual(resolvedHost ? resolvedHost : host); |
| 122 | +} |
0 commit comments