From 99739618d7e1aa0f282099b6a0eb44787d8dbd92 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Mon, 8 Oct 2018 17:18:22 +0200 Subject: [PATCH] Store HTTP response headers in case-insensitive array --- init.php | 1 + lib/HttpClient/CurlClient.php | 2 +- lib/Util/CaseInsensitiveArray.php | 62 ++++++++++++++++++++++ tests/Stripe/HttpClient/CurlClientTest.php | 9 ++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/Util/CaseInsensitiveArray.php diff --git a/init.php b/init.php index c78718008..582b6392d 100644 --- a/init.php +++ b/init.php @@ -5,6 +5,7 @@ // Utilities require(dirname(__FILE__) . '/lib/Util/AutoPagingIterator.php'); +require(dirname(__FILE__) . '/lib/Util/CaseInsensitiveArray.php'); require(dirname(__FILE__) . '/lib/Util/LoggerInterface.php'); require(dirname(__FILE__) . '/lib/Util/DefaultLogger.php'); require(dirname(__FILE__) . '/lib/Util/RandomGenerator.php'); diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php index b25c1c14f..51eec3e18 100644 --- a/lib/HttpClient/CurlClient.php +++ b/lib/HttpClient/CurlClient.php @@ -162,7 +162,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile) } // Create a callback to capture HTTP headers for the response - $rheaders = []; + $rheaders = new Util\CaseInsensitiveArray(); $headerCallback = function ($curl, $header_line) use (&$rheaders) { // Ignore the HTTP request line (HTTP/1.1 200 OK) if (strpos($header_line, ":") === false) { diff --git a/lib/Util/CaseInsensitiveArray.php b/lib/Util/CaseInsensitiveArray.php new file mode 100644 index 000000000..02c6f26da --- /dev/null +++ b/lib/Util/CaseInsensitiveArray.php @@ -0,0 +1,62 @@ +container = array_map("strtolower", $initial_array); + } + + public function offsetSet($offset, $value) + { + $offset = static::maybeLowercase($offset); + if (is_null($offset)) { + $this->container[] = $value; + } else { + $this->container[$offset] = $value; + } + } + + public function offsetExists($offset) + { + $offset = static::maybeLowercase($offset); + return isset($this->container[$offset]); + } + + public function offsetUnset($offset) + { + $offset = static::maybeLowercase($offset); + unset($this->container[$offset]); + } + + public function offsetGet($offset) + { + $offset = static::maybeLowercase($offset); + return isset($this->container[$offset]) ? $this->container[$offset] : null; + } + + private static function maybeLowercase($v) + { + if (is_string($v)) { + return strtolower($v); + } else { + return $v; + } + } +} diff --git a/tests/Stripe/HttpClient/CurlClientTest.php b/tests/Stripe/HttpClient/CurlClientTest.php index 086e6d3ee..89cf203f2 100644 --- a/tests/Stripe/HttpClient/CurlClientTest.php +++ b/tests/Stripe/HttpClient/CurlClientTest.php @@ -225,4 +225,13 @@ public function testSleepTimeShouldAddSomeRandomness() $this->assertEquals($baseValue * 4, $this->sleepTimeMethod->invoke($curlClient, 3)); $this->assertEquals($baseValue * 8, $this->sleepTimeMethod->invoke($curlClient, 4)); } + + public function testResponseHeadersCaseInsensitive() + { + $charge = Charge::all(); + + $headers = $charge->getLastResponse()->headers; + $this->assertNotNull($headers['request-id']); + $this->assertEquals($headers['request-id'], $headers['Request-Id']); + } }