From 654c0e29ab78aba8bfef52fd3d06a3b2b39c4e0d Mon Sep 17 00:00:00 2001 From: Konstantin Kopachev Date: Mon, 18 Jul 2022 15:23:54 -0700 Subject: [PATCH] fix: don't send content-type header if no post body exists (#2288) --- src/Service/Resource.php | 2 +- tests/Google/Service/ResourceTest.php | 39 +++++++++++++++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Service/Resource.php b/src/Service/Resource.php index b8b08d3cd..ecf402b18 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -203,7 +203,7 @@ public function call($name, $arguments, $expectedClass = null) $request = new Request( $method['httpMethod'], $url, - ['content-type' => 'application/json'], + $postBody ? ['content-type' => 'application/json'] : [], $postBody ? json_encode($postBody) : '' ); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 870286608..c2c814d4e 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -96,15 +96,38 @@ public function testCall() "methods" => [ "testMethod" => [ "parameters" => [], - "path" => "method/path", - "httpMethod" => "POST", - ] + "path" => "method/path", + "httpMethod" => "POST", + ] ] ] ); $request = $resource->call("testMethod", [[]]); $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); + $this->assertFalse($request->hasHeader('Content-Type')); + } + + public function testCallWithPostBody() + { + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], + "path" => "method/path", + "httpMethod" => "POST", + ] + ] + ] + ); + $request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]); + $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + $this->assertTrue($request->hasHeader('Content-Type')); } public function testCallServiceDefinedRoot() @@ -130,11 +153,11 @@ public function testCallServiceDefinedRoot() } /** - * Some Google Service (Google\Service\Directory\Resource\Channels and - * Google\Service\Reports\Resource\Channels) use a different servicePath value - * that should override the default servicePath value, it's represented by a / - * before the resource path. All other Services have no / before the path - */ + * Some Google Service (Google\Service\Directory\Resource\Channels and + * Google\Service\Reports\Resource\Channels) use a different servicePath value + * that should override the default servicePath value, it's represented by a / + * before the resource path. All other Services have no / before the path + */ public function testCreateRequestUriForASelfDefinedServicePath() { $this->service->servicePath = '/admin/directory/v1/';