1

我在 Guzzle-HTTP 中的承载授权存在问题。我用它用 PHPUnit 测试我的 PHP-REST-API。

这是我的测试方法:

 public function testGetMe()
{
    $client = new Client([
        'base_uri' => $this->apiBaseURL
    ]);        
    $data = ['email' => $email, 'password' => '12345'];
    $client->post('register', [
            'form_params' => $data]
    );
    $responseJson = json_decode($response->getBody());
    $myToken = $responseJson->data->token;

    $response = $client->request('GET', 'users', [
        'headers' => [
            'Authorization'      => 'Bearer '.$myToken
        ],
        'debug' => true
    ]);
}

但是,如果我将令牌设置为硬编码,如下所示:

 public function testGetMe()
{
    $client = new Client([
        'base_uri' => $this->apiBaseURL
    ]);        
    $data = ['email' => $email, 'password' => '12345'];
    $client->post('register', [
            'form_params' => $data]
    );
    $responseJson = json_decode($response->getBody());
    $myToken = eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE0NjQ5NzExMzQsImp0aSI6IjByR3FpOW15Rm1rRGo2TU9sMVhkK3dRU3p1V0pWejM1UEhiU2dTMmg5SEU9IiwiaXNzIjoiQXBwTmFtZSIsIm5iZiI6MTQ2NDk3MTE0NCwiZXhwIjoxNDY0OTczMTQ0LCJzdWIiOiJ0ZXN0QG1haWwuZGUifQ.yA4a_S6ILCeqENm00H712g9uF5g9eSz_BmnaMDdZ2r4p5e1q88g0T09IG2WKCi1oExoBfQ8VTmKeX6ZQv0RydQ;

    $response = $client->request('GET', 'users', [
        'headers' => [
            'Authorization'      => 'Bearer '.$myToken
        ],
        'debug' => true
    ]);
}

并且还与邮递员一起工作。这与我从 REST-API 收到的令牌相同。

你有什么想法吗?

4

0 回答 0