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

标签 php laravel

我看过很多关于如何在响应中设置 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/

相关文章:

php - 如何在显示/显示 div 时包含特定的 php 文件 :"block"

javascript - 使用ajax mysql php上传图片

php - Dingo API - 如何在 url 中添加版本号?

php - Laravel 或 PHP 唯一具有多个条件

php - Laravel 中的 Cron 作业

php - Laravel - 超过锁定等待超时

php - 带有 "WHERE... IN"查询的 PDO

php - 我的自定义邮件通知未发送,而其他所有通知均已发送

php - 需要、选择好的数据库驱动程序的建议

laravel - “Where like”子句使用 2 列的串联值与 Eloquent