php – 如何检查laravel 4中的响应头以进行单元测试?

前端之家收集整理的这篇文章主要介绍了php – 如何检查laravel 4中的响应头以进行单元测试?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经看到很多关于如何在响应上设置标头的示例,但我找不到检查响应标头的方法.

例如,在测试用例中,我有:

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',array('Accept' => 'text/xml'));
    $this->assertResponseStatus(200);
    //some code here to test that the response content-type is 'text/xml'
}

我如何测试内容类型标题是’application / json’还是任何其他内容类型?也许我误会了什么?

我有的控制器可以使用Accept标头进行内容否定,我想确保响应中的内容类型是正确的.

谢谢!

在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',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'));
}
原文链接:https://www.f2er.com/laravel/133666.html

猜你在找的Laravel相关文章