Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PhpUnit在laravel 5中使用令牌发布请求?_Php_Laravel_Laravel 5_Phpunit_Forms - Fatal编程技术网

通过PhpUnit在laravel 5中使用令牌发布请求?

通过PhpUnit在laravel 5中使用令牌发布请求?,php,laravel,laravel-5,phpunit,forms,Php,Laravel,Laravel 5,Phpunit,Forms,我正在尝试使用phpunit测试我的laravelapi,我正在使用$this->call()'XMLHttpRequest' 或者试试包装 我尝试的是Authorization而不是HTTP\u Authorization。不管怎样,我现在也试过了,但还是一样$response=$this->call('POST',users/?token='。$this->token,$user,[],[],[],[],['HTTP_Authorization'=>'Bearer'.$this->token

我正在尝试使用phpunit测试我的laravelapi,我正在使用
$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>
所以我试了一下:

$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);
更新:

你在拉威尔启用了CORS?也许这就是原因

使用标题:
'HTTP\u X\u REQUESTED\u和'=>'XMLHttpRequest'


或者试试包装

我尝试的是
Authorization
而不是
HTTP\u Authorization
。不管怎样,我现在也试过了,但还是一样<代码>$response=$this->call('POST',users/?token='。$this->token,$user,[],[],[],[],['HTTP_Authorization'=>'Bearer'.$this->token])尝试使用完整的URI,以防万一。相同。此外,我还尝试了一个测试路由,并转储了
request()->header()
,以查看所有存在的头。尽管存在
内容类型“:[“应用程序\/x-www-form-urlencoded”]
,但无法在此处看到
HTTP\u授权
标头。我的问题是,我在最后一个数组中传递了它,而不是在最后一个数组中传递。但我仍然在那里与重定向。唯一改变的是请求的标题现在已经存在。你是在严格模式下使用dingo吗?也许您还需要两个标题:
Accept:application/x.visitapeldoorn.v1+json
Content-Type:application/json
$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);