Skip to content

Commit

Permalink
fix: don't send content-type header if no post body exists (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkopachev committed Jul 18, 2022
1 parent a69131b commit 654c0e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) : ''
);

Expand Down
39 changes: 31 additions & 8 deletions tests/Google/Service/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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/';
Expand Down

0 comments on commit 654c0e2

Please sign in to comment.