Skip to content

Commit

Permalink
Merge pull request #20 from hyperwallet/feature/HW-50653-SDK-TypeErro…
Browse files Browse the repository at this point in the history
…r-thrown-when-response-status-is-204-No-Content

HW-50653: SDK - TypeError thrown when response status is 204 No Content
  • Loading branch information
wmews-hw authored Apr 12, 2019
2 parents d65e227 + 0cd2920 commit 839ce3c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: php
php:
- '5.6'
- '7.0'
- hhvm
- hhvm-3.18
cache:
directories:
- $HOME/.composer/cache
Expand Down
5 changes: 3 additions & 2 deletions src/Hyperwallet/Util/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ private function doRequest($method, $url, array $urlParams, array $options) {
*/
private function checkResponseHeaderContentType($response) {
$contentType = implode('', $response->getHeader('Content-Type'));
if (empty($contentType) || ((!$this->isEncrypted && strpos($contentType, 'application/json') === false) ||
($this->isEncrypted && strpos($contentType, 'application/jose+json') === false ))) {
$expectedContentType = $this->isEncrypted ? 'application/jose+json' : 'application/json';
$invalidContentType = $response->getStatusCode() !== 204 && !empty($contentType) && strpos($contentType, $expectedContentType) === false;
if ($invalidContentType) {
throw new HyperwalletException('Invalid Content-Type specified in Response Header');
}
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Hyperwallet/Tests/Util/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ public function testDoPost_with_encryption_successful() {
$this->validateRequest('POST', '/test', 'test=true', array('test2' => 'value2'), true, array(), true);
}

public function testDoPost_with_encryption_return_response_204_status() {
// Setup data
$clientPath = __DIR__ . "/../../../resources/private-jwkset1";
$hyperwalletPath = __DIR__ . "/../../../resources/public-jwkset1";
$originalMessage = array('test2' => 'value2');
$encryption = new HyperwalletEncryption($clientPath, $hyperwalletPath);
$encryptedMessage = $encryption->encrypt($originalMessage);

// Execute test
$mockHandler = new MockHandler(array(
new Response(204)
));
$this->createApiClientWithEncryption($mockHandler);

$model = new BaseModel(array(), $originalMessage);

// Execute test
$data = $this->apiClient->doPost('/test', array(), null, array());
$this->assertEquals(array(), $data);

// Validate api request
$this->validateRequest('POST', '/test', '', array(), true, array(), true);
}

public function testDoPost_with_encryption_charset_in_content_type() {
// Setup data
$clientPath = __DIR__ . "/../../../resources/private-jwkset1";
Expand Down Expand Up @@ -292,6 +316,21 @@ public function testDoPost_throw_exception_when_response_has_wrong_content_type_
$this->validateRequest('POST', '/test', '', array('test2' => 'value2'), true);
}

public function testDoPost_return_response_204_status() {
// Setup data
$mockHandler = new MockHandler(array(
new Response(204)
));
$this->createApiClient($mockHandler);

// Execute test
$data = $this->apiClient->doPost('/test', array(), null, array());
$this->assertEquals(array(), $data);

// Validate api request
$this->validateRequest('POST', '/test', '', array(), true);
}

public function testDoPost_throw_exception_connection_issue() {
// Setup data
$mockHandler = new MockHandler(array(
Expand Down Expand Up @@ -510,6 +549,23 @@ public function testDoPut_return_response_with_path_placeholder() {
$this->validateRequest('PUT', '/test/token', '', array('test2' => 'value2'), true);
}

public function testDoPut_return_response_204_status() {
// Setup data
$mockHandler = new MockHandler(array(
new Response(204)
));
$this->createApiClient($mockHandler);

$model = new BaseModel(array(), array());

// Execute test
$data = $this->apiClient->doPut('/test', array(), $model, array());
$this->assertEquals(array(), $data);

// Validate api request
$this->validateRequest('PUT', '/test', '', array(), true);
}

public function testDoPut_throw_exception_connection_issue() {
// Setup data
$mockHandler = new MockHandler(array(
Expand Down

0 comments on commit 839ce3c

Please sign in to comment.