POST 在 laravel 5 中通过 PhpUnit 使用令牌请求?
POST request in laravel 5 with token via PhpUnit?
我正在尝试使用 phpunit 测试我的 Laravel APIs,我正在使用 $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>
现在,当我进行一些挖掘时,我遇到了这个:
调用方法的 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']);
但我仍然收到重定向。我错过了什么?
为什么不尝试这样的事情呢?
$token = ‘your token’;
$method = ‘POST’;
$params = [];
$response = $this->call($method,'http://api.api.app/api/',
$params,[],[],['HTTP_Authorization' => 'Bearer ' . $token],[]);
$this->response = $response;
$this->seeStatusCode(200);
更新:
您在 Laravel 中启用了 CORS?也许这就是原因。
使用header:'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
或尝试 laravel-cors 包。
我正在尝试使用 phpunit 测试我的 Laravel APIs,我正在使用 $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>
现在,当我进行一些挖掘时,我遇到了这个:
调用方法的 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']);
但我仍然收到重定向。我错过了什么?
为什么不尝试这样的事情呢?
$token = ‘your token’;
$method = ‘POST’;
$params = [];
$response = $this->call($method,'http://api.api.app/api/',
$params,[],[],['HTTP_Authorization' => 'Bearer ' . $token],[]);
$this->response = $response;
$this->seeStatusCode(200);
更新:
您在 Laravel 中启用了 CORS?也许这就是原因。
使用header:'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
或尝试 laravel-cors 包。