Skip to content

Commit

Permalink
Merge pull request #27 from ThePay/hotfix-4213-header-case-sensitivity
Browse files Browse the repository at this point in the history
Hotfix 4213 header case sensitivity
  • Loading branch information
tymajiri authored Sep 12, 2022
2 parents 728e4b5 + 860ecbf commit beb32e7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/Http/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 2 additions & 6 deletions src/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/TheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
class TheClient
{
/** @var string */
const VERSION = '1.4.2';
const VERSION = '1.4.3';

/** @var TheConfig */
private $config;
Expand Down
42 changes: 42 additions & 0 deletions tests/Http/HttpResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ThePay\ApiClient\Tests\Http;

use PHPUnit\Framework\TestCase;
use ThePay\ApiClient\Http\HttpResponse;

/**
* @covers \ThePay\ApiClient\Http\HttpResponse
*/
final class HttpResponseTest extends TestCase
{
/**
* @dataProvider dataProviderGetHeader
*
* @param string|null $expectedValue
* @param non-empty-string $headerKey
* @param array<string, string>|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<array{string|null, non-empty-string, array<string, string>|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')),
);
}
}

0 comments on commit beb32e7

Please sign in to comment.