From 6e8f18150866b30f568709b3b2a5d5791c04d78e Mon Sep 17 00:00:00 2001 From: VojtaB Date: Fri, 9 Sep 2022 17:52:10 +0200 Subject: [PATCH 1/2] reading http headers case insensitive keys fix --- src/Http/HttpResponse.php | 15 ++++++++++++ src/Service/ApiService.php | 8 ++----- tests/Http/HttpResponseTest.php | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 tests/Http/HttpResponseTest.php diff --git a/src/Http/HttpResponse.php b/src/Http/HttpResponse.php index 39faba7..eb91848 100644 --- a/src/Http/HttpResponse.php +++ b/src/Http/HttpResponse.php @@ -98,6 +98,21 @@ public function getHeaders() return $this->headers; } + /** + * @param non-empty-string $key + * @return string|null + */ + public function getHeader($key) + { + foreach ((array) $this->headers as $headerKey => $header) { + if (strtolower($headerKey) === strtolower($key)) { + return $header; + } + } + + return null; + } + /** * @return string|null */ diff --git a/src/Service/ApiService.php b/src/Service/ApiService.php index 0af8307..5a1522c 100644 --- a/src/Service/ApiService.php +++ b/src/Service/ApiService.php @@ -129,9 +129,7 @@ public function getAccountTransactionHistory(TransactionFilter $filter, $page = throw $this->buildException($url, $response); } - $headers = $response->getHeaders(); - - return new TransactionCollection(Json::decode($response->getBody(), true), $page, $limit, (int) $headers['X-Total-Count']); + return new TransactionCollection(Json::decode($response->getBody(), true), $page, $limit, (int) $response->getHeader('X-Total-Count')); } /** @@ -203,9 +201,7 @@ public function getPayments(PaymentsFilter $filter, $page = 1, $limit = 25) throw $this->buildException($url, $response); } - $headers = $response->getHeaders(); - - return new PaymentCollection($response->getBody(), $page, $limit, (int) $headers['X-Total-Count']); + return new PaymentCollection($response->getBody(), $page, $limit, (int) $response->getHeader('X-Total-Count')); } /** diff --git a/tests/Http/HttpResponseTest.php b/tests/Http/HttpResponseTest.php new file mode 100644 index 0000000..0852c0d --- /dev/null +++ b/tests/Http/HttpResponseTest.php @@ -0,0 +1,42 @@ +|null $headers + * + * @return void + */ + public function testGetHeader($expectedValue, $headerKey, array $headers = null) + { + $response = new HttpResponse(null, null, '', $headers); + self::assertSame($expectedValue, $response->getHeader($headerKey)); + } + + /** + * @return non-empty-array|null}> + */ + public function dataProviderGetHeader() + { + return array( + array(null, 'no-headers', null), + array(null, 'empty-headers', array()), + array(null, 'key-not-found', array('another-key' => 'another-value')), + array('value-for-lower-key', 'lower-key', array('Lower-Key' => 'value-for-lower-key')), + array('value-for-upper-key', 'UPPER-KEY', array('Upper-Key' => 'value-for-upper-key')), + array('value-for-same-key', 'same-key', array('same-key' => 'value-for-same-key')), + ); + } +} From 860ecbfcb43a903169f49ee6fc8fda50a7010842 Mon Sep 17 00:00:00 2001 From: VojtaB Date: Fri, 9 Sep 2022 17:54:33 +0200 Subject: [PATCH 2/2] VERSION constant udate --- src/TheClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TheClient.php b/src/TheClient.php index dfae6da..8f860fb 100644 --- a/src/TheClient.php +++ b/src/TheClient.php @@ -42,7 +42,7 @@ class TheClient { /** @var string */ - const VERSION = '1.4.2'; + const VERSION = '1.4.3'; /** @var TheConfig */ private $config;