Browse Source

Implement getHeaders() and hasHeader()

Implement the remaining header methods. Use hasHeader() and
getHeaderLine internally to prevent mutation of the request
as it builds internal state where possible.
Mark Story 9 years ago
parent
commit
7e8377664a
2 changed files with 76 additions and 3 deletions
  1. 30 2
      src/Network/Request.php
  2. 46 1
      tests/TestCase/Network/RequestTest.php

+ 30 - 2
src/Network/Request.php

@@ -367,8 +367,8 @@ class Request implements ArrayAccess
             $data = $this->input();
             parse_str($data, $data);
         }
-        if ($this->env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
-            $data['_method'] = $this->env('HTTP_X_HTTP_METHOD_OVERRIDE');
+        if ($this->hasHeader('X-Http-Method-Override')) {
+            $data['_method'] = $this->getHeaderLine('X-Http-Method_Override');
             $override = true;
         }
         $this->_environment['ORIGINAL_REQUEST_METHOD'] = $method;
@@ -983,6 +983,34 @@ class Request implements ArrayAccess
      */
     public function getHeaders()
     {
+        $headers = [];
+        foreach ($this->_environment as $key => $value) {
+            $name = null;
+            if (strpos($key, 'HTTP_') === 0) {
+                $name = substr($key, 5);
+            }
+            if (strpos($key, 'CONTENT_') === 0) {
+                $name = $key;
+            }
+            if ($name !== null) {
+                $name = strtr(strtolower($name), '_', ' ');
+                $name = strtr(ucwords($name), ' ', '-');
+                $headers[$name] = (array)$value;
+            }
+        }
+        return $headers;
+    }
+
+    /**
+     * Check if a header exists in the request.
+     *
+     * @param string $name The header you want to get (case-insensitive)
+     * @return bool Whether or not the header is defined.
+     */
+    public function hasHeader($name)
+    {
+        $name = $this->normalizeHeaderName($name);
+        return array_key_exists($name, $this->_environment);
     }
 
     /**

+ 46 - 1
tests/TestCase/Network/RequestTest.php

@@ -1287,6 +1287,52 @@ class RequestTest extends TestCase
      *
      * @return void
      */
+    public function testGetHeaders()
+    {
+        $request = new Request(['environment' => [
+            'HTTP_HOST' => 'localhost',
+            'CONTENT_TYPE' => 'application/json',
+            'CONTENT_LENGTH' => 1337,
+            'HTTP_CONTENT_MD5' => 'abc123',
+            'HTTP_DOUBLE' => ['a', 'b']
+        ]]);
+        $headers = $request->getHeaders();
+        $expected = [
+            'Host' => ['localhost'],
+            'Content-Type' => ['application/json'],
+            'Content-Length' => [1337],
+            'Content-Md5' => ['abc123'],
+            'Double' => ['a', 'b'],
+        ];
+        $this->assertEquals($expected, $headers);
+    }
+
+    /**
+     * Test hasHeader
+     *
+     * @return void
+     */
+    public function testHasHeader()
+    {
+        $request = new Request(['environment' => [
+            'HTTP_HOST' => 'localhost',
+            'CONTENT_TYPE' => 'application/json',
+            'CONTENT_LENGTH' => 1337,
+            'HTTP_CONTENT_MD5' => 'abc123',
+            'HTTP_DOUBLE' => ['a', 'b']
+        ]]);
+        $this->assertTrue($request->hasHeader('Host'));
+        $this->assertTrue($request->hasHeader('Content-Type'));
+        $this->assertTrue($request->hasHeader('Content-MD5'));
+        $this->assertTrue($request->hasHeader('Double'));
+        $this->assertFalse($request->hasHeader('Authorization'));
+    }
+
+    /**
+     * Test getting headers with psr7 methods
+     *
+     * @return void
+     */
     public function testGetHeader()
     {
         $request = new Request(['environment' => [
@@ -2440,7 +2486,6 @@ class RequestTest extends TestCase
         $expected = $vars + [
             'CONTENT_TYPE' => null,
             'HTTP_CONTENT_TYPE' => null,
-            'HTTP_X_HTTP_METHOD_OVERRIDE' => null,
             'ORIGINAL_REQUEST_METHOD' => 'PUT',
         ];
         $this->assertSame($expected, $request->getServerParams());