diff --git a/src/ClientOptionsTrait.php b/src/ClientOptionsTrait.php index d4a323b26..43deaef7c 100644 --- a/src/ClientOptionsTrait.php +++ b/src/ClientOptionsTrait.php @@ -97,8 +97,7 @@ private function buildClientOptions(array $options) 'libVersion' => null, 'apiEndpoint' => null, 'clientCertSource' => null, - // if the universe domain hasn't been explicitly set, assume GDU ("googleapis.com") - 'universeDomain' => GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN, + 'universeDomain' => null, ]; $supportedTransports = $this->supportedTransports(); @@ -178,6 +177,11 @@ private function buildClientOptions(array $options) $apiEndpoint = self::determineMtlsEndpoint($options['apiEndpoint']); } + // If the user has not supplied a universe domain, use the environment variable if set. + // Otherwise, use the default ("googleapis.com"). + $options['universeDomain'] ??= getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN') + ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN; + // mTLS: It is not valid to configure mTLS outside of "googleapis.com" (yet) if (isset($options['clientCertSource']) && $options['universeDomain'] !== GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN diff --git a/tests/Tests/Unit/ClientOptionsTraitTest.php b/tests/Tests/Unit/ClientOptionsTraitTest.php index 03deacc65..d07a46d9a 100644 --- a/tests/Tests/Unit/ClientOptionsTraitTest.php +++ b/tests/Tests/Unit/ClientOptionsTraitTest.php @@ -475,13 +475,17 @@ public function testMtlsClientOptionWithDefaultClientCertSource() /** * @dataProvider provideServiceAddressTemplate + * @runInSeparateProcess */ - public function testServiceAddressTemplate(array $options, string $expectedEndpoint) + public function testServiceAddressTemplate(array $options, string $expectedEndpoint, string $envVar = null) { + if ($envVar) { + putenv($envVar); + } $client = new UniverseDomainStubClientOptionsClient(); $updatedOptions = $client->buildClientOptions($options); - $this->assertEquals($updatedOptions['apiEndpoint'], $expectedEndpoint); + $this->assertEquals($expectedEndpoint, $updatedOptions['apiEndpoint']); } public function provideServiceAddressTemplate() @@ -503,6 +507,21 @@ public function provideServiceAddressTemplate() ['universeDomain' => 'foo.com', 'apiEndpoint' => 'new.test.address.com'], 'new.test.address.com', // set through api endpoint (universe domain is not used) ], + [ + [], + 'stub.googleapis.com', + 'GOOGLE_CLOUD_UNIVERSE_DOMAIN=', // env var is ignored when empty + ], + [ + ['universeDomain' => 'foo.com'], + 'stub.foo.com', + 'GOOGLE_CLOUD_UNIVERSE_DOMAIN=bar.com', // env var is ignored when client option is set + ], + [ + [], + 'stub.bar.com', + 'GOOGLE_CLOUD_UNIVERSE_DOMAIN=bar.com', // env var is used when client option isn't set + ], ]; }