Laravel TestCase不发送授权标头(JWT令牌) [英] Laravel TestCase not sending Authorization headers (JWT Token)

查看:100
本文介绍了Laravel TestCase不发送授权标头(JWT令牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

我们正在编写单元测试以测试JWT令牌的创建和无效,并且每次我们尝试使JWTAuth :: invalidate令牌时,都会从JWTException返回无法从请求解析令牌"错误.

We are writing unit tests to test the creation and invalidation of JWT tokens and receiving a "The token could not be parsed from the request" error back from a JWTException every time we try to JWTAuth::invalidate the token.

说明

在我们的控制器内部,要创建用户令牌,我们要传递用户电子邮件地址,然后返回JWT令牌.

Inside our controller, to create a user token, we are passing through the user email address and then returning the JWT token.

然后,我们通过使用invalidateToken方法使令牌无效并通过发送Authorization标头传递令牌来破坏令牌.

Afterwards, we are destroying the token by invalidating it using invalidateToken method and passing through the token by sending an Authorization header.

public function invalidateToken()
{
    try {
        JWTAuth::invalidate(JWTAuth::getToken());
        return Response::json(['status' => 'success'], 200);
    } catch (JWTException $e) {
        return Response::json(['status' => 'error', 'message' => $e->getMessage()], 401);
    }
}

这可以通过使用Postman和PHPStorms Restful客户端来完美地实现.

This works perfectly by using Postman as well as PHPStorms Restful client.

当我们尝试为此方法编写测试时,我们面临错误响应和无法从请求中解析令牌"错误消息.

When we try to write a test for this method, we are faced with an error response and a "The token could not be parsed from the request" error message.

我们的测试如下:

public function testInvalidateToken()
{
    $createUserToken = $this->call('POST', '/token/create', ['email' => 'test@test.com']);
    $user = $this->parseJson($createUserToken);

    $response = $this->call('POST', '/token/invalidate',
        [/* params */], [/* cookies */], [/* files */], ['HTTP_Authorization' => 'Bearer '.$user->token]);

    // var_dump($user->token);
    // die();

    $data = $this->parseJson($response);
    $this->assertEquals($data->status, 'success');
    $this->assertEquals($data->status, '200');
}

我们正在使用Laravel 5.1(在服务器参数之前将cookie作为参数)

We are using Laravel 5.1 (which has the cookies as a parameter before the server parameter)

PHP版本5.6.10

PHP version 5.6.10

PHPUnit 3.7.28

PHPUnit 3.7.28

*编辑* 已更新为PHPUnit 4.7.6,仍然无法通过Authorization标头发送.

* EDIT * Updated to PHPUnit 4.7.6 and still failing to send through Authorization header.

*解决方案*
在我的控制器__construct方法中,我有几行代码正在运行并解决了我的问题.

* SOLUTION *
In my controller __construct method, I have these few lines of code that is running and sorted out my issue.

if ((\App::environment() == 'testing') && array_key_exists("HTTP_Authorization",  Request::server())) {
    JWTAuth::setRequest(\Route::getCurrentRequest());
}

推荐答案

在Laravel 4.2上编写测试时,我们遇到了类似的问题,特别是在测试环境中运行应用程序时,我们添加了以下几行:

We faced a similar issue when writing tests on Laravel 4.2, exceptionally we added this lines when running the application on the testing environment:

 // This will only work when unit testing
        if ((\App::environment() == 'testing') && array_key_exists("HTTP_Authorization",  LRequest::server())) {
            $headers['Authorization'] = LRequest::server()["HTTP_Authorization"];
        }

这篇关于Laravel TestCase不发送授权标头(JWT令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆