/** * Gets or sets HTTP headers oo the request. All headers * are included immediately after the HTTP protocol definition during * transmission. This method provides a simple array or key/value * interface to the headers. * * @param mixed $key Key or array of key/value pairs to set * @param string $value Value to set to the supplied key * @return mixed */ public function headers($key = NULL, $value = NULL) { if ($key instanceof HTTP_Header) { // Act a setter, replace all headers $this->_header = $key; return $this; } if (is_array($key)) { // Act as a setter, replace all headers $this->_header->exchangeArray($key); return $this; } if ($this->_header->count() === 0 and $this->is_initial()) { // Lazy load the request headers $this->_header = HTTP::request_headers(); } if ($key === NULL) { // Act as a getter, return all headers return $this->_header; } elseif ($value === NULL) { // Act as a getter, single header return $this->_header->offsetExists($key) ? $this->_header->offsetGet($key) : NULL; } // Act as a setter for a single header $this->_header[$key] = $value; return $this; }
/** * Tests HTTP::request_headers() * * HTTP::request_headers relies on the $_SERVER superglobal if the function * `apache_request_headers` or the PECL `http` extension are not available. * * The test feeds the $_SERVER superglobal with the test cases' datasets * and then restores the $_SERVER superglobal so that it does not affect * other tests. * * @test * @dataProvider provider_request_headers * @param array $server_globals globals to feed $_SERVER * @param array $expected_headers Expected, cleaned HTTP headers */ public function test_request_headers(array $server_globals, array $expected_headers) { // save the $_SERVER super-global into temporary local var $tmp_server = $_SERVER; $_SERVER = array_replace_recursive($_SERVER, $server_globals); $headers = HTTP::request_headers(); $actual_headers = array_intersect_key($headers->getArrayCopy(), $expected_headers); $this->assertSame($expected_headers, $actual_headers); // revert the super-global to its previous state $_SERVER = $tmp_server; }