diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 51d10d06b..81ea7525e 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -24,6 +24,7 @@ use Google\Service\Drive; use Google\AuthHandler\AuthHandlerFactory; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\CredentialsLoader; use Google\Auth\GCECache; use Google\Auth\Credentials\GCECredentials; use GuzzleHttp\Client as GuzzleClient; @@ -38,6 +39,7 @@ use ReflectionMethod; use InvalidArgumentException; use Exception; +use DomainException; class ClientTest extends BaseTest { @@ -925,4 +927,21 @@ public function testQueryParamsForAuthUrl() ]); $this->assertStringContainsString('&enable_serial_consent=true', $authUrl1); } + public function testUniverseDomainMismatch() + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage( + 'The configured universe domain (example.com) does not match the credential universe domain (foo.com)' + ); + + $credentials = $this->prophesize(CredentialsLoader::class); + $credentials->getUniverseDomain() + ->shouldBeCalledOnce() + ->willReturn('foo.com'); + $client = new Client([ + 'universe_domain' => 'example.com', + 'credentials' => $credentials->reveal(), + ]); + $client->authorize(); + } } diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index fb8aaa2c9..86e0bef24 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -35,10 +35,11 @@ class TestService extends \Google\Service { - public function __construct(Client $client) + public function __construct(Client $client, $rootUrl = null) { parent::__construct($client); - $this->rootUrl = "https://test.example.com"; + $this->rootUrl = $rootUrl ?: "https://test.example.com"; + $this->rootUrlTemplate = $rootUrl ?: "https://test.UNIVERSE_DOMAIN"; $this->servicePath = ""; $this->version = "v1beta1"; $this->serviceName = "test"; @@ -59,7 +60,7 @@ public function setUp(): void $this->client->getLogger()->willReturn($logger->reveal()); $this->client->shouldDefer()->willReturn(true); $this->client->getHttpClient()->willReturn(new GuzzleClient()); - $this->client->getUniverseDomain()->willReturn('googleapis.com'); + $this->client->getUniverseDomain()->willReturn('example.com'); $this->service = new TestService($this->client->reveal()); } @@ -107,6 +108,37 @@ public function testCall() $this->assertFalse($request->hasHeader('Content-Type')); } + public function testCallWithUniverseDomainTemplate() + { + $client = $this->prophesize(Client::class); + $logger = $this->prophesize("Monolog\Logger"); + $this->client->getLogger()->willReturn($logger->reveal()); + $this->client->shouldDefer()->willReturn(true); + $this->client->getHttpClient()->willReturn(new GuzzleClient()); + $this->client->getUniverseDomain()->willReturn('example-universe-domain.com'); + + $this->service = new TestService($this->client->reveal()); + + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], + "path" => "method/path", + "httpMethod" => "POST", + ] + ] + ] + ); + $request = $resource->call("testMethod", [[]]); + $this->assertEquals("https://test.example-universe-domain.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + $this->assertFalse($request->hasHeader('Content-Type')); + } + public function testCallWithPostBody() { $resource = new GoogleResource( @@ -131,9 +163,9 @@ public function testCallWithPostBody() public function testCallServiceDefinedRoot() { - $this->service->rootUrl = "https://sample.example.com"; + $service = new TestService($this->client->reveal(), "https://sample.example.com"); $resource = new GoogleResource( - $this->service, + $service, "test", "testResource", [