【问题标题】:How to send headers with phpunit api request?如何使用 phpunit api 请求发送标头?
【发布时间】:2016-03-25 02:17:15
【问题描述】:

我正在尝试使用 phpunit 测试我用 laravel 5 编写的 Web 服务。我是这个测试框架的新手。应用程序使用 JWT 进行身份验证,该身份验证在请求的标头中传递。

我的测试看起来像这样:

$response = $this->call('POST', 'authenticate', [
    'username' => 'carparts',
    'email' => 'admin@admin.com',
    'password' => 'password'
]);

$token = 'Bearer ' . json_decode($response->getContent())->token;

$response = $this->call('POST', 'users', [
    "first_name"    => "Test",
    "last_name"     => "Test",
    "email"         => 'test@test.com',
    "password"      => "testing",
    "role"          => "1"
], [], [], [
    'Authorization' => $token
]);

dd($response->getContent());

令牌返回正常,但是当我尝试在下一个请求中使用创建新用户时,它失败说无法从请求中解析令牌。当我在中间件中执行request()->headers 进行检查时,即使这样也不会显示标题。我究竟做错了什么?如何使用 PHPUnit 在请求中传递标头?

【问题讨论】:

    标签: php testing http-headers phpunit


    【解决方案1】:

    有一个将标头转换为服务器变量的功能。尝试传递它:

    $this->transformHeadersToServerVars([ 'Authorization' => $token ])
    

    【讨论】:

      【解决方案2】:

      $server 参数是服务器变量数组,而不是 http 标头。
      将您的令牌传递为HTTP_AUTHORIZATION

      $response = $this->call('POST', 'users', [
          "first_name"    => "Test",
          "last_name"     => "Test",
          "email"         => 'test@test.com',
          "password"      => "testing",
          "role"          => "1"
      ], [], [], [
          'HTTP_AUTHORIZATION' => $token
      ]);
      

      【讨论】:

      • 还是不行。我什至尝试在我的路线中查看dd($_SERVER['HTTP_AUTHORIZATION']),但它说未定义索引。有什么想法吗?
      • 我尝试了 HTTP_Authorization,它对我有用。
      【解决方案3】:

      只是一个疯狂的猜测......

      检查您的 .htaccess,它必须包含

      RewriteCond %{HTTP:Authorization} .
      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
      

      【讨论】:

        【解决方案4】:

        我花了一天时间研究这个问题。

        当你运行测试时真正发生了什么:你运行 phpunit 命令然后每个请求并没有真正发送,它只是在当前实例中运行。也就是说调用$this->call()不会调用http请求。

        每个测试用例(即测试方法)调用运行 createApplication 方法的 setUp 方法。内核启动应用程序所需的所有方法都在方法setUp() 中运行,该方法恰好在您的测试用例之前调用:

        // Middleware
        ...
        // There is nothing related to testCase 
        
        function testCase() {
           // set header
        }
        // Action
        ...
        // You can use header here
        

        为了解决这个问题,我在测试用例之前添加了方法。我仍然无法更改标题,但我真的不在乎。要获取自定义信息,我执行以下操作:

        // in tests:
        function _setHeader() {
          global $_TESTING_X_HEADER;
          $_TESTING_X_HEADER = "some-value";
        }
        function testCase() {
          // setUp will run with $_TESTING_X_HEADER == "some-value"
          // test goes here
        }
        
        // in middleware:
        if(App::environment('testing')) {
          $h = global $_TESTING_X_HEADER;
        } else {
          $h = request()->header('x-header');
        }
        
        

        【讨论】:

          【解决方案5】:

          我正在使用 PHP 单元进行一些功能测试。

          $client->request('GET','url', [], [], ['HTTP_Authorization' => $token]);
          

          它对我有用!

          【讨论】:

            猜你喜欢
            • 2013-10-24
            • 2013-09-24
            • 2020-08-12
            • 2016-10-25
            • 2020-12-31
            • 1970-01-01
            • 2014-09-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多