Skip to content

Commit ee9f14c

Browse files
authored
Merge pull request #48 from chadicus/dev/content-type
Only set Content-Type header if request has body
2 parents 1fc33f9 + 0685f9e commit ee9f14c

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"require": {
2424
"php": "^7.0",
2525
"ext-curl": "*",
26+
"ext-json": "*",
2627
"lib-curl": "~7.15",
28+
"fig/http-message-util": "^1.1",
2729
"guzzlehttp/guzzle": "^6.3",
2830
"psr/http-message": "^1.0",
2931
"psr/simple-cache": "^1.0",

src/Client.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ public function startDelete(string $resource, string $id = null, array $data = n
271271
}
272272

273273
$json = $data !== null ? json_encode($data) : null;
274-
return $this->start($url, 'DELETE', $json, ['Content-Type' => 'application/json']);
274+
$headers = [];
275+
if ($json !== null) {
276+
$headers['Content-Type'] = 'application/json';
277+
}
278+
279+
return $this->start($url, 'DELETE', $json, $headers);
275280
}
276281

277282
/**
@@ -309,7 +314,12 @@ public function startSend(string $method, string $uri, array $data = null) : str
309314
{
310315
$url = "{$this->baseUrl}/{$uri}";
311316
$json = $data !== null ? json_encode($data) : null;
312-
return $this->start($url, $method, $json, ['Content-Type' => 'application/json']);
317+
$headers = [];
318+
if ($json !== null) {
319+
$headers['Content-Type'] = 'application/json';
320+
}
321+
322+
return $this->start($url, $method, $json, $headers);
313323
}
314324

315325
/**

tests/ClientTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace TraderInteractive\Api;
44

5-
use ArrayObject;
5+
use Fig\Http\Message\StatusCodeInterface as StatusCodes;
66
use SubjectivePHP\Psr\SimpleCache\InMemoryCache;
77
use DominionEnterprises\Util\Arrays;
88
use DominionEnterprises\Util\Http;
@@ -585,7 +585,6 @@ function (RequestInterface $request) {
585585
if ($request->getMethod() === 'DELETE'
586586
&& (string)$request->getUri() === 'baseUrl/v1/resource+name/the+id'
587587
&& $request->getHeaders() === [
588-
'Content-Type' => ['application/json'],
589588
'Accept-Encoding' => ['gzip'],
590589
'Authorization' => ['Bearer a token'],
591590
]
@@ -967,6 +966,37 @@ function (RequestInterface $request) {
967966
$this->assertSame(['a body'], $client->index('foos')->getResponse());
968967
}
969968

969+
/**
970+
* @test
971+
* @covers ::send
972+
* @covers ::startSend
973+
*/
974+
public function startSendDoesNotAddContentTypeHeaderIfNoJsonIsGiven()
975+
{
976+
$adapter = new FakeAdapter(
977+
function (RequestInterface $request) {
978+
if (substr($request->getUri(), -5) === 'token') {
979+
return new Psr7Response(
980+
StatusCodes::STATUS_OK,
981+
['Content-Type' => ['application/json']],
982+
json_encode(['access_token' => 'a token', 'expires_in' => 1])
983+
);
984+
}
985+
986+
$this->assertSame('GET', $request->getMethod());
987+
$this->assertSame('baseUrl/v1/feeds/123/transport', (string)$request->getUri());
988+
$this->assertSame('', (string)$request->getBody());
989+
$this->assertSame([], $request->getHeader('Content-Type'));
990+
return new Psr7Response(StatusCodes::STATUS_OK);
991+
}
992+
);
993+
994+
$client = new Client($adapter, $this->getAuthentication(), 'baseUrl/v1');
995+
996+
$response = $client->send('GET', 'feeds/123/transport');
997+
$this->assertSame(StatusCodes::STATUS_OK, $response->getHttpCode());
998+
}
999+
9701000
private function getAuthentication() : Authentication
9711001
{
9721002
return Authentication::createClientCredentials('not under test', 'not under test');

0 commit comments

Comments
 (0)