簡體   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