Browse Source

Start implementing getHeader() and getHeaderLine()

Mark Story 9 years ago
parent
commit
6501fd9276
2 changed files with 132 additions and 0 deletions
  1. 86 0
      src/Network/Request.php
  2. 46 0
      tests/TestCase/Network/RequestTest.php

+ 86 - 0
src/Network/Request.php

@@ -960,6 +960,92 @@ class Request implements ArrayAccess
     }
 
     /**
+     * Get all headers in the request.
+     *
+     * Returns an associative array where the header names are
+     * the keys and the values are a list of header values.
+     *
+     * While header names are not case-sensitive, getHeaders() will preserve the
+     * exact case in which headers were originally specified.
+     *
+     * @return array An associative array of headers and their values.
+     */
+    public function getHeaders()
+    {
+    }
+
+    /**
+     * Get a single header from the request.
+     *
+     * Return the header value as an array. If the header
+     * is not present an empty array will be returned.
+     *
+     * @param string $name The header you want to get (case-insensitive)
+     * @return array An associative array of headers and their values.
+     *   If the header doesn't exist, an empty array will be returned.
+     */
+    public function getHeader($name)
+    {
+        $name = str_replace('-', '_', strtoupper($name));
+        if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'])) {
+            $name = 'HTTP_' . $name;
+        }
+        if (isset($this->_environment[$name])) {
+            return (array)$this->_environment[$name];
+        }
+        return [];
+    }
+
+    /**
+     * Get a single header as a string from the request.
+     *
+     * Return an array of header values
+     *
+     * @param string $name The header you want to get (case-insensitive)
+     * @return string Header values collapsed into a comma separated string.
+     */
+    public function getHeaderLine($name)
+    {
+        $value = $this->getHeader($name);
+        return implode(', ', $value);
+    }
+
+    /**
+     * Get a modified request with the provided header.
+     *
+     * @param string $name The header name.
+     * @param string|array $value The header value
+     * @return static
+     */
+    public function withHeader($name, $value)
+    {
+    }
+
+    /**
+     * Get a modified request with the provided header.
+     *
+     * Existing header values will be retained. The provided value
+     * will be appended into the existing values.
+     *
+     * @param string $name The header name.
+     * @param string|array $value The header value
+     * @return static
+     */
+    public function withAddedHeader($name, $value)
+    {
+    }
+
+    /**
+     * Get a modified request without a provided header.
+     *
+     * @param string $name The header name.
+     * @return static
+     */
+    public function withoutHeader($name)
+    {
+    }
+
+    /**
      * Get the HTTP method used for this request.
      *
      * @return string The name of the HTTP method used.

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

@@ -1283,6 +1283,52 @@ class RequestTest extends TestCase
     }
 
     /**
+     * Test getting headers with psr7 methods
+     *
+     * @return void
+     */
+    public function testGetHeader()
+    {
+        $request = new Request(['environment' => [
+            'HTTP_HOST' => 'localhost',
+            'CONTENT_TYPE' => 'application/json',
+            'CONTENT_LENGTH' => 1337,
+            'HTTP_CONTENT_MD5' => 'abc123',
+            'HTTP_DOUBLE' => ['a', 'b']
+        ]]);
+        $this->assertEquals([], $request->getHeader('Not-there'));
+
+        $expected = [$request->env('HTTP_HOST')];
+        $this->assertEquals($expected, $request->getHeader('Host'));
+        $this->assertEquals($expected, $request->getHeader('host'));
+        $this->assertEquals($expected, $request->getHeader('HOST'));
+        $this->assertEquals(['a', 'b'], $request->getHeader('Double'));
+    }
+
+    /**
+     * Test getting headers with psr7 methods
+     *
+     * @return void
+     */
+    public function testGetHeaderLine()
+    {
+        $request = new Request(['environment' => [
+            'HTTP_HOST' => 'localhost',
+            'CONTENT_TYPE' => 'application/json',
+            'CONTENT_LENGTH' => 1337,
+            'HTTP_CONTENT_MD5' => 'abc123',
+            'HTTP_DOUBLE' => ['a', 'b']
+        ]]);
+        $this->assertEquals('', $request->getHeaderLine('Authorization'));
+
+        $expected = $request->env('CONTENT_LENGTH');
+        $this->assertEquals($expected, $request->getHeaderLine('Content-Length'));
+        $this->assertEquals($expected, $request->getHeaderLine('content-Length'));
+        $this->assertEquals($expected, $request->getHeaderLine('ConTent-LenGth'));
+        $this->assertEquals('a, b', $request->getHeaderLine('Double'));
+    }
+
+    /**
      * Test accepts() with and without parameters
      *
      * @return void