我已经看到很多关于如何在响应上设置标头的示例,但我找不到检查响应标头的方法.
例如,在测试用例中,我有:
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'
}
Run Code Online (Sandbox Code Playgroud)
我如何测试内容类型标题是'application/json'还是任何其他内容类型?也许我误会了什么?
我有的控制器可以使用Accept标头进行内容否定,我想确保响应中的内容类型是正确的.
谢谢!
big*_*dan 10
在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'));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4293 次 |
| 最近记录: |