Test Client headers

This commit is contained in:
PJ Dietz 2014-07-25 22:39:32 -04:00
parent 5e9980f864
commit 832e849875
2 changed files with 52 additions and 0 deletions

View File

@ -51,4 +51,46 @@ class ClientTest extends \PHPUnit_Framework_TestCase
["OPTIONS"] ["OPTIONS"]
]; ];
} }
/**
* @dataProvider httpHeaderProvider
*/
public function testCheckHttpHeaders($headerKey, $headerValue)
{
$host = "localhost";
$port = 8080;
$script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("GET"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array($headerKey => $headerValue)));
$client = new Client();
$resp = $client->request($rqst);
$headers = json_decode($resp->getBody());
$this->assertEquals($headerValue, $headers->{$headerKey});
$server->stop();
}
public function httpHeaderProvider()
{
return [
["Cache-Control", "max-age=0"],
["X-Custom-Header", "custom value"],
["Accept-Charset", "utf-8"]
];
}
} }

View File

@ -0,0 +1,10 @@
<?php
// http://www.php.net/manual/en/function.getallheaders.php#84262
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) === 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
print json_encode($headers);