【发布时间】:2016-08-10 05:01:42
【问题描述】:
我正在尝试使用 phpunit 测试我的 Laravel API,并且我正在使用 $this->call(); 方法来执行调用并查看它们是否工作正常。
我也是用于身份验证的 JWT,因此必须通过它传递我的令牌。简单的 GET 请求很简单:
$response = $this->call('GET', 'users?token=' . $this->token);
但是当我需要为此创建新用户或任何资源时,我正在尝试这样做:
$response = $this->call('POST', 'users/?token=' . $this->token, $user);
但它给了我这样的重定向:
<!DOCTYPE html>\n
<html>\n
<head>\n
<meta charset="UTF-8" />\n
<meta http-equiv="refresh" content="1;url=http://localhost" />\n
\n
<title>Redirecting to http://localhost</title>\n
</head>\n
<body>\n
Redirecting to <a href="http://localhost">http://localhost</a>.\n
</body>\n
</html>
现在当我进行一些挖掘时,我发现了这个:
Redirect 302 on Laravel POST request
调用方法的 API 如下所示:
$response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
所以我尝试了这个:
$response = $this->call('POST', 'users/?token=' . $this->token, $user, [], [], [], ['Content-Type' => 'application/x-www-form-urlencoded']);
但我仍然收到重定向。我错过了什么?
【问题讨论】:
标签: php laravel laravel-5 phpunit forms