From 15e1257699fd811c426c16a56071e834fc647d7d Mon Sep 17 00:00:00 2001 From: Andrii Severyn Date: Wed, 10 Apr 2019 19:40:59 +0300 Subject: [PATCH 1/4] HW-50653: SDK - TypeError thrown when response status is 204 No Content --- src/Hyperwallet/Util/ApiClient.php | 5 +++-- tests/Hyperwallet/Tests/Util/ApiClientTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Hyperwallet/Util/ApiClient.php b/src/Hyperwallet/Util/ApiClient.php index bb3b4440..15f94c81 100644 --- a/src/Hyperwallet/Util/ApiClient.php +++ b/src/Hyperwallet/Util/ApiClient.php @@ -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'); } } diff --git a/tests/Hyperwallet/Tests/Util/ApiClientTest.php b/tests/Hyperwallet/Tests/Util/ApiClientTest.php index 266f6997..c2183bed 100644 --- a/tests/Hyperwallet/Tests/Util/ApiClientTest.php +++ b/tests/Hyperwallet/Tests/Util/ApiClientTest.php @@ -292,6 +292,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( From fddb0ed06ccdf1a20efc641b21e9ba4edf1e23c6 Mon Sep 17 00:00:00 2001 From: Andrii Severyn Date: Wed, 10 Apr 2019 20:00:55 +0300 Subject: [PATCH 2/4] HW-50653: Fix ApiClient --- src/Hyperwallet/Util/ApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hyperwallet/Util/ApiClient.php b/src/Hyperwallet/Util/ApiClient.php index 15f94c81..e36adb7b 100644 --- a/src/Hyperwallet/Util/ApiClient.php +++ b/src/Hyperwallet/Util/ApiClient.php @@ -191,7 +191,7 @@ private function doRequest($method, $url, array $urlParams, array $options) { private function checkResponseHeaderContentType($response) { $contentType = implode('', $response->getHeader('Content-Type')); $expectedContentType = $this->isEncrypted ? 'application/jose+json' : 'application/json'; - $invalidContentType = $response->getStatusCode() !== 204 && !empty($contentType) && strpos($contentType, $expectedContentType) === false + $invalidContentType = $response->getStatusCode() !== 204 && !empty($contentType) && strpos($contentType, $expectedContentType) === false; if ($invalidContentType) { throw new HyperwalletException('Invalid Content-Type specified in Response Header'); } From f0c87a16639da19767e18968b528ae2ee1a78e41 Mon Sep 17 00:00:00 2001 From: Andrii Severyn Date: Thu, 11 Apr 2019 01:08:11 +0300 Subject: [PATCH 3/4] HW-50653: Set version for hhvm --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a619bb05..52ec054b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: php php: - '5.6' - '7.0' -- hhvm +- hhvm-3.18 cache: directories: - $HOME/.composer/cache From 0cd29208cf4fa5f3fcce8c743b37effda1b19271 Mon Sep 17 00:00:00 2001 From: Andrii Severyn Date: Thu, 11 Apr 2019 18:47:12 +0300 Subject: [PATCH 4/4] HW-50653: Added tests for ApiClientTest with 204 response status code --- .../Hyperwallet/Tests/Util/ApiClientTest.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/Hyperwallet/Tests/Util/ApiClientTest.php b/tests/Hyperwallet/Tests/Util/ApiClientTest.php index c2183bed..d3d3b7e4 100644 --- a/tests/Hyperwallet/Tests/Util/ApiClientTest.php +++ b/tests/Hyperwallet/Tests/Util/ApiClientTest.php @@ -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"; @@ -525,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(