|
16 | 16 |
|
17 | 17 | package com.google.cloud.spanner.connection;
|
18 | 18 |
|
| 19 | +import static com.google.cloud.spanner.connection.ConnectionOptions.Builder.SPANNER_URI_PATTERN; |
| 20 | +import static com.google.cloud.spanner.connection.ConnectionOptions.determineHost; |
19 | 21 | import static com.google.common.truth.Truth.assertThat;
|
20 | 22 | import static org.junit.Assert.assertEquals;
|
21 | 23 | import static org.junit.Assert.assertFalse;
|
|
33 | 35 | import com.google.cloud.spanner.ErrorCode;
|
34 | 36 | import com.google.cloud.spanner.SpannerException;
|
35 | 37 | import com.google.cloud.spanner.SpannerOptions;
|
| 38 | +import com.google.common.collect.ImmutableMap; |
36 | 39 | import com.google.common.io.BaseEncoding;
|
37 | 40 | import com.google.common.io.Files;
|
38 | 41 | import java.io.File;
|
39 | 42 | import java.nio.charset.StandardCharsets;
|
40 | 43 | import java.util.Arrays;
|
41 | 44 | import java.util.Collections;
|
42 | 45 | import java.util.Objects;
|
| 46 | +import java.util.regex.Matcher; |
43 | 47 | import org.junit.Test;
|
44 | 48 | import org.junit.function.ThrowingRunnable;
|
45 | 49 | import org.junit.runner.RunWith;
|
@@ -153,6 +157,105 @@ public void testBuildWithAutoConfigEmulator() {
|
153 | 157 | assertTrue(options.isUsePlainText());
|
154 | 158 | }
|
155 | 159 |
|
| 160 | + @Test |
| 161 | + public void testDetermineHost() { |
| 162 | + final String uriWithoutHost = |
| 163 | + "cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123"; |
| 164 | + Matcher matcherWithoutHost = SPANNER_URI_PATTERN.matcher(uriWithoutHost); |
| 165 | + assertTrue(matcherWithoutHost.find()); |
| 166 | + final String uriWithHost = |
| 167 | + "cloudspanner://custom.host.domain:1234/projects/test-project-123/instances/test-instance-123/databases/test-database-123"; |
| 168 | + Matcher matcherWithHost = SPANNER_URI_PATTERN.matcher(uriWithHost); |
| 169 | + assertTrue(matcherWithHost.find()); |
| 170 | + |
| 171 | + assertEquals( |
| 172 | + DEFAULT_HOST, |
| 173 | + determineHost( |
| 174 | + matcherWithoutHost, |
| 175 | + /* autoConfigEmulator= */ false, |
| 176 | + /* usePlainText= */ false, |
| 177 | + ImmutableMap.of())); |
| 178 | + assertEquals( |
| 179 | + DEFAULT_HOST, |
| 180 | + determineHost( |
| 181 | + matcherWithoutHost, |
| 182 | + /* autoConfigEmulator= */ false, |
| 183 | + /* usePlainText= */ false, |
| 184 | + ImmutableMap.of("FOO", "bar"))); |
| 185 | + assertEquals( |
| 186 | + "http://localhost:9010", |
| 187 | + determineHost( |
| 188 | + matcherWithoutHost, |
| 189 | + /* autoConfigEmulator= */ true, |
| 190 | + /* usePlainText= */ false, |
| 191 | + ImmutableMap.of())); |
| 192 | + assertEquals( |
| 193 | + "http://localhost:9011", |
| 194 | + determineHost( |
| 195 | + matcherWithoutHost, |
| 196 | + /* autoConfigEmulator= */ true, |
| 197 | + /* usePlainText= */ false, |
| 198 | + ImmutableMap.of("SPANNER_EMULATOR_HOST", "localhost:9011"))); |
| 199 | + assertEquals( |
| 200 | + "http://localhost:9010", |
| 201 | + determineHost( |
| 202 | + matcherWithoutHost, |
| 203 | + /* autoConfigEmulator= */ true, |
| 204 | + /* usePlainText= */ true, |
| 205 | + ImmutableMap.of())); |
| 206 | + assertEquals( |
| 207 | + "http://localhost:9011", |
| 208 | + determineHost( |
| 209 | + matcherWithoutHost, |
| 210 | + /* autoConfigEmulator= */ true, |
| 211 | + /* usePlainText= */ true, |
| 212 | + ImmutableMap.of("SPANNER_EMULATOR_HOST", "localhost:9011"))); |
| 213 | + |
| 214 | + // A host in the connection string has precedence over all other options. |
| 215 | + assertEquals( |
| 216 | + "https://custom.host.domain:1234", |
| 217 | + determineHost( |
| 218 | + matcherWithHost, |
| 219 | + /* autoConfigEmulator= */ false, |
| 220 | + /* usePlainText= */ false, |
| 221 | + ImmutableMap.of())); |
| 222 | + assertEquals( |
| 223 | + "http://custom.host.domain:1234", |
| 224 | + determineHost( |
| 225 | + matcherWithHost, |
| 226 | + /* autoConfigEmulator= */ false, |
| 227 | + /* usePlainText= */ true, |
| 228 | + ImmutableMap.of())); |
| 229 | + assertEquals( |
| 230 | + "http://custom.host.domain:1234", |
| 231 | + determineHost( |
| 232 | + matcherWithHost, |
| 233 | + /* autoConfigEmulator= */ false, |
| 234 | + /* usePlainText= */ true, |
| 235 | + ImmutableMap.of())); |
| 236 | + assertEquals( |
| 237 | + "https://custom.host.domain:1234", |
| 238 | + determineHost( |
| 239 | + matcherWithHost, |
| 240 | + /* autoConfigEmulator= */ true, |
| 241 | + /* usePlainText= */ false, |
| 242 | + ImmutableMap.of())); |
| 243 | + assertEquals( |
| 244 | + "http://custom.host.domain:1234", |
| 245 | + determineHost( |
| 246 | + matcherWithHost, |
| 247 | + /* autoConfigEmulator= */ false, |
| 248 | + /* usePlainText= */ true, |
| 249 | + ImmutableMap.of("SPANNER_EMULATOR_HOST", "localhost:9011"))); |
| 250 | + assertEquals( |
| 251 | + "https://custom.host.domain:1234", |
| 252 | + determineHost( |
| 253 | + matcherWithHost, |
| 254 | + /* autoConfigEmulator= */ true, |
| 255 | + /* usePlainText= */ false, |
| 256 | + ImmutableMap.of("SPANNER_EMULATOR_HOST", "localhost:9011"))); |
| 257 | + } |
| 258 | + |
156 | 259 | @Test
|
157 | 260 | public void testBuildWithRouteToLeader() {
|
158 | 261 | final String BASE_URI =
|
|
0 commit comments