Skip to content

Instantly share code, notes, and snippets.

@jm42
Last active August 29, 2015 14:08
Show Gist options
  • Save jm42/7097a52af8664b5cbd28 to your computer and use it in GitHub Desktop.
Save jm42/7097a52af8664b5cbd28 to your computer and use it in GitHub Desktop.
HTTP Headers
<?php
class Headers implements ArrayAccess {
private static $headers = array(
'Accept',
'Accept-Charset',
'Accept-Language',
'Accept-Encoding',
'Authorization',
'Cache-Control',
'Connection',
'Cookie',
'Content-Length',
'Content-Type',
'Content-MD5',
'User-Agent',
'Referer',
'Expect',
'Host',
'If-Match',
'If-None-Match',
'If-Modified-Since',
'If-Unmodified-Since',
'If-Range',
'X-Requested-With',
'X-Forwarded-Proto',
'X-Forwarded-For',
'X-HTTP-Method-Override',
);
private $server;
public function __construct($server) {
$this->server = $server;
}
public function offsetExists($offset) {
return isset($this->server[$this->normalize($offset)]);
}
public function offsetGet($offset) {
return $this->server[$this->normalize($offset)];
}
public function offsetSet($offset, $value) {
$this->server[$this->normalize($offset)] = $value;
}
public function offsetUnset($offset) {
unset($this->server[$this->normalize($offset)]);
}
private function normalize($offset) {
if (!in_array($offset, static::$headers, true)) {
throw new InvalidArgumentException(
"Invalid header, $offset given"
);
}
if (strpos($offset, 'Content-') === 0) {
return 'CONTENT_' . strtoupper(substr($offset, 8));
}
return 'HTTP_' . strtoupper(strtr($offset, '-', '_'));
}
}
<?php
class HeadersTest extends \PHPUnit_Framework_TestCase {
private $server = array(
'REQUEST_URI' => '/',
'REQUEST_METHOD' => 'GET',
'HTTP_HOST' => 'localhost',
'HTTP_USER_AGENT' => 'Mozilla/5.0',
'HTTP_ACCEPT' => 'text/html;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_CACHE_CONTROL' => 'max-age=0',
);
public function getInvalidHeader() {
return array(
[new stdClass],
[function() {}],
);
}
public function getInexistentHeader() {
return array(
['Content-Ravioli'],
);
}
public function testConstructorWithArray() {
new Headers($this->server);
}
public function testConstructorWithArrayAccess() {
new Headers(new ArrayObject($this->server));
}
public function testExists() {
$headers = new Headers($this->server);
$this->assertTrue(isset($headers['Host']));
$this->assertFalse(isset($headers['Referer']));
}
public function testGet() {
$headers = new Headers($this->server);
$this->assertEquals('text/html;q=0.9,*/*;q=0.8', $headers['Accept']);
$this->assertEquals('en-US,en;q=0.5', $headers['Accept-Language']);
}
/**
* @dataProvider getInvalidHeader
* @expectedException PHPUnit_Framework_Error
*/
public function testGetWithInvalidOffset($invalid) {
$headers = new Headers($this->server);
$headers[$invalid];
}
/**
* @dataProvider getInexistentHeader
* @expectedException InvalidArgumentException
*/
public function testGetWithInexistentOffset($inexistent) {
$headers = new Headers($this->server);
$headers[$inexistent];
}
public function testSet() {
$headers = new Headers($this->server);
$headers['Content-Type'] = 'text/html';
$this->assertEquals('text/html', $headers['Content-Type']);
}
public function testUnset() {
$headers = new Headers($this->server);
$headers['Content-Type'] = 'text/html';
unset($headers['Accept-Language']);
unset($headers['Content-Type']);
$this->assertFalse(isset($headers['Accept-Language']));
$this->assertFalse(isset($headers['Content-Type']));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment