Skip to content

Commit 839ce3c

Browse files
authored
Merge pull request #20 from hyperwallet/feature/HW-50653-SDK-TypeError-thrown-when-response-status-is-204-No-Content
HW-50653: SDK - TypeError thrown when response status is 204 No Content
2 parents d65e227 + 0cd2920 commit 839ce3c

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: php
22
php:
33
- '5.6'
44
- '7.0'
5-
- hhvm
5+
- hhvm-3.18
66
cache:
77
directories:
88
- $HOME/.composer/cache

src/Hyperwallet/Util/ApiClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ private function doRequest($method, $url, array $urlParams, array $options) {
190190
*/
191191
private function checkResponseHeaderContentType($response) {
192192
$contentType = implode('', $response->getHeader('Content-Type'));
193-
if (empty($contentType) || ((!$this->isEncrypted && strpos($contentType, 'application/json') === false) ||
194-
($this->isEncrypted && strpos($contentType, 'application/jose+json') === false ))) {
193+
$expectedContentType = $this->isEncrypted ? 'application/jose+json' : 'application/json';
194+
$invalidContentType = $response->getStatusCode() !== 204 && !empty($contentType) && strpos($contentType, $expectedContentType) === false;
195+
if ($invalidContentType) {
195196
throw new HyperwalletException('Invalid Content-Type specified in Response Header');
196197
}
197198
}

tests/Hyperwallet/Tests/Util/ApiClientTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ public function testDoPost_with_encryption_successful() {
7070
$this->validateRequest('POST', '/test', 'test=true', array('test2' => 'value2'), true, array(), true);
7171
}
7272

73+
public function testDoPost_with_encryption_return_response_204_status() {
74+
// Setup data
75+
$clientPath = __DIR__ . "/../../../resources/private-jwkset1";
76+
$hyperwalletPath = __DIR__ . "/../../../resources/public-jwkset1";
77+
$originalMessage = array('test2' => 'value2');
78+
$encryption = new HyperwalletEncryption($clientPath, $hyperwalletPath);
79+
$encryptedMessage = $encryption->encrypt($originalMessage);
80+
81+
// Execute test
82+
$mockHandler = new MockHandler(array(
83+
new Response(204)
84+
));
85+
$this->createApiClientWithEncryption($mockHandler);
86+
87+
$model = new BaseModel(array(), $originalMessage);
88+
89+
// Execute test
90+
$data = $this->apiClient->doPost('/test', array(), null, array());
91+
$this->assertEquals(array(), $data);
92+
93+
// Validate api request
94+
$this->validateRequest('POST', '/test', '', array(), true, array(), true);
95+
}
96+
7397
public function testDoPost_with_encryption_charset_in_content_type() {
7498
// Setup data
7599
$clientPath = __DIR__ . "/../../../resources/private-jwkset1";
@@ -292,6 +316,21 @@ public function testDoPost_throw_exception_when_response_has_wrong_content_type_
292316
$this->validateRequest('POST', '/test', '', array('test2' => 'value2'), true);
293317
}
294318

319+
public function testDoPost_return_response_204_status() {
320+
// Setup data
321+
$mockHandler = new MockHandler(array(
322+
new Response(204)
323+
));
324+
$this->createApiClient($mockHandler);
325+
326+
// Execute test
327+
$data = $this->apiClient->doPost('/test', array(), null, array());
328+
$this->assertEquals(array(), $data);
329+
330+
// Validate api request
331+
$this->validateRequest('POST', '/test', '', array(), true);
332+
}
333+
295334
public function testDoPost_throw_exception_connection_issue() {
296335
// Setup data
297336
$mockHandler = new MockHandler(array(
@@ -510,6 +549,23 @@ public function testDoPut_return_response_with_path_placeholder() {
510549
$this->validateRequest('PUT', '/test/token', '', array('test2' => 'value2'), true);
511550
}
512551

552+
public function testDoPut_return_response_204_status() {
553+
// Setup data
554+
$mockHandler = new MockHandler(array(
555+
new Response(204)
556+
));
557+
$this->createApiClient($mockHandler);
558+
559+
$model = new BaseModel(array(), array());
560+
561+
// Execute test
562+
$data = $this->apiClient->doPut('/test', array(), $model, array());
563+
$this->assertEquals(array(), $data);
564+
565+
// Validate api request
566+
$this->validateRequest('PUT', '/test', '', array(), true);
567+
}
568+
513569
public function testDoPut_throw_exception_connection_issue() {
514570
// Setup data
515571
$mockHandler = new MockHandler(array(

0 commit comments

Comments
 (0)