加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:38:14 所属栏目:大数据 来源:网络整理
导读:我已经看到很多关于如何在响应上设置标头的示例,但我找不到检查响应标头的方法. 例如,在测试用例中,我有: public function testGetJson(){ $response = $this-action('GET','LocationTypeController@index',null,array('Accept' = 'application/json')); $t
我已经看到很多关于如何在响应上设置标头的示例,但我找不到检查响应标头的方法.

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

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'));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读