Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: header casing issues #286

Merged
merged 3 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
vendor
downloads
.idea/*
tests/.phpunit.result.cache
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions src/Httpful/Response/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function fromString($string)
*/
public function offsetExists($offset)
{
return isset($this->headers[$offset]);
return $this->getCaseInsensitive($offset) !== null;
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
20 changes: 20 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down