繁体   English   中英

Laravel 5.2 单元测试集标头不起作用

[英]Laravel 5.2 unit test set headers not working

我有以下断言:

$this->call('GET', '/api/v1/ubication/suggest', [
            'term' => 'a'
        ], [], [], ['Accept' => 'application/json']);

$this->assertResponseStatus(422);

调用方法定义如下:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) {

}

我在 $server 位置传递我的标头,但我仍然在响应中得到 302 状态代码(我正在测试请求验证)而不是我期望的 422 。

我如何在测试中模拟该标题?

如果要使用 Headers 进行 GET 调用,可以使用:

$this->get($url, $headers);

示例:

$this->get('/oauth', [ 'HTTP_X_API_KEY' => 'xxxx' ]);

由于您正在发出 GET 请求,因此您的参数可以包含在$url

来源

你仍然可以使用

$this->call('GET', '/api/v1/ubication/suggest', [
            'term' => 'a'
        ], [], [], ['Accept' => 'application/json']);

它不起作用的原因是在幕后 Laravel 过滤掉了任何不以“HTTP_”开头的标头

所以你需要做的就是用 'HTTP_Accept' 替换 'Accept'

$this->call('GET', '/api/v1/ubication/suggest', [
            'term' => 'a'
        ], [], [], ['HTTP_Accept' => 'application/json']);
```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM