From 4e0c1ebf4c49438b1b4c3826a5b80ad3ac26c908 Mon Sep 17 00:00:00 2001 From: atymic Date: Tue, 7 Jan 2020 20:23:07 +1100 Subject: [PATCH 1/3] fix: broken test with header casing issues --- .gitignore | 1 + tests/Httpful/HttpfulTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/.gitignore b/.gitignore index 6cf3a90..7b39a0b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock vendor downloads .idea/* +tests/.phpunit.result.cache diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 71b66ba..be9de52 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -32,6 +32,15 @@ class HttpfulTest extends \PHPUnit\Framework\TestCase Content-Type: application/json Connection: keep-alive Transfer-Encoding: chunked\r\n"; + const SAMPLE_JSON_HEADER_LOWERCASE = + "HTTP/2 200 +date: Tue, 07 Jan 2020 09:11:21 GMT +content-type: application/json +content-length: 513 +access-control-allow-origin: * +access-control-allow-methods: GET, POST, PUT, PATCH, DELETE +access-control-allow-headers: Authorization, Content-Type, Accept-Encoding, Cache-Control, DNT +cache-control: private, must-revalidate\r\n"; const SAMPLE_JSON_RESPONSE = '{"key":"value","object":{"key":"value"},"array":[1,2,3,4]}'; const SAMPLE_CSV_HEADER = "HTTP/1.1 200 OK @@ -268,6 +277,17 @@ function testJsonResponseParse() $this->assertEquals(1, $response->body->array[0]); } + function testJsonResponseParseLowercaseHeaders() + { + $req = Request::init(); + $response = new Response(self::SAMPLE_JSON_RESPONSE, self::SAMPLE_JSON_HEADER_LOWERCASE, $req); + + $this->assertEquals("value", $response->body->key); + $this->assertEquals("value", $response->body->object->key); + $this->assertIsArray( $response->body->array); + $this->assertEquals(1, $response->body->array[0]); + } + function testXMLResponseParse() { $req = Request::init()->sendsAndExpects(Mime::XML); From 29ce9851f667da0d3c420878bdac9daf247b5477 Mon Sep 17 00:00:00 2001 From: atymic Date: Tue, 7 Jan 2020 20:53:51 +1100 Subject: [PATCH 2/3] fix: allow case insensitive header access --- src/Httpful/Response/Headers.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Httpful/Response/Headers.php b/src/Httpful/Response/Headers.php index b900294..8e8a8cd 100644 --- a/src/Httpful/Response/Headers.php +++ b/src/Httpful/Response/Headers.php @@ -46,7 +46,7 @@ public static function fromString($string) */ public function offsetExists($offset) { - return isset($this->headers[$offset]); + return $this->getCaseInsensitive($offset) !== null; } /** @@ -55,9 +55,7 @@ public function offsetExists($offset) */ public function offsetGet($offset) { - if (isset($this->headers[$offset])) { - return $this->headers[$offset]; - } + return $this->getCaseInsensitive($offset); } /** @@ -95,4 +93,14 @@ public function toArray() return $this->headers; } + private function getCaseInsensitive(string $key) + { + foreach ($this->headers as $header => $value) { + if (strtolower($key) === strtolower($header)) { + return $value; + } + } + + return null; + } } From fd05a5c852a0e15d40c89ad54ce472af7d3e789e Mon Sep 17 00:00:00 2001 From: atymic Date: Tue, 7 Jan 2020 20:59:05 +1100 Subject: [PATCH 3/3] docs: update changelog --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 290f7bd..1f2cede 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ Httpful highly encourages sending in pull requests. When submitting a pull requ # Changelog +## 0.3.1 + + - FIX [PR #286](https://github.com/nategood/httpful/pull/286) Fixed header case sensitivity + ## 0.3.0 - REFACTOR Dropped support for dead versions of PHP. Updated the PHPUnit tests.