我看过很多关于如何在响应中设置 header 的示例,但我找不到检查响应 header 的方法。
例如在我的测试用例中:
public function testGetJson()
{
$response = $this->action('GET', 'LocationTypeController@index', null, array('Accept' => 'application/json'));
$this->assertResponseStatus(200);
//some code here to test that the response content-type is 'application/json'
}
public function testGetXml()
{
$response = $this->action('GET', 'LocationTypeController@index', null, array('Accept' => 'text/xml'));
$this->assertResponseStatus(200);
//some code here to test that the response content-type is 'text/xml'
}
我将如何着手测试内容类型 header 是“application/json”还是任何其他内容类型?也许我误解了什么?
我拥有的 Controller 可以使用 Accept header 进行内容否定,我想确保响应中的内容类型是正确的。
谢谢!
最佳答案
在 Symfony 和 Laravel 文档中进行了一些挖掘之后,我能够弄清楚......
public function testGetJson()
{
// Symfony interally prefixes headers with "HTTP", so
// just Accept would not work. I also had the method signature wrong...
$response = $this->action('GET', 'LocationTypeController@index',
array(), array(), array(), array('HTTP_Accept' => 'application/json'));
$this->assertResponseStatus(200);
// I just needed to access the public
// headers var (which is a Symfony ResponseHeaderBag object)
$this->assertEquals('application/json',
$response->headers->get('Content-Type'));
}
关于php - 如何检查 laravel 4 中的响应 header 以进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819493/