我正试图在我的Laravel 5应用程序中编写测试,专门用于测试web服务。身份验证使用JWT令牌。我已经成功地编写并运行了一个测试,该测试验证在身份验证时返回的令牌。
<?php
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('authenticate', [
'username' => 'carparts',
'email' => 'admin@admin.com',
'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();就像一种魅力。我面临的问题是如何在其他请求中使用这里返回的令牌,因为很明显,所有其他请求都需要一个令牌才能继续进行,所以在测试每个API调用之前,我是要验证和获取一个新令牌,还是有一种解决方法?
我已经可以这么做了
<?php
$I = new ApiTester($scenario);
$I->wantTo('see a list of all users');
$I->haveHttpHeader('Authorization', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImNhcnBhcnRzIiwic3ViIjoiMSIsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdDo4MDAwXC9hcGlcL2F1dGhlbnRpY2F0ZSIsImlhdCI6IjE0NDY2NDA0ODYiLCJleHAiOiIxNDQ2NjQ0MDg2IiwibmJmIjoiMTQ0NjY0MDQ4NiIsImp0aSI6ImZmYTNkZjc4Yzg5YjdmNDNhYThkZTRmZTViZWI4YjI3In0.9UBZgEz3hHTEMlK5hPzYRV1DsAI3TSSHSZxV0FcBLio');
$I->sendGET('/users');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();但这并不是很有效,原因很明显,我很难编码令牌。任何帮助都是非常感谢的。
发布于 2015-11-09 16:02:58
您可以通过调用$I->grabDataFromJsonResponse()从json获取令牌。示例假设您的响应类似于:
{
"status": "ok",
"token": "xxxxxxxx"
}那么你的测试结果将如下所示。警告未经测试的代码。
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('authenticate', [
'username' => 'carparts',
'email' => 'admin@admin.com',
'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$token = $I->grabDataFromJsonResponse('token');
$I->deleteHeader('Authorization'); /* Needed with old version of codeception. */
$I->amBearerAuthenticated($token);
$I->sendGET('/users');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();发布于 2019-04-08 10:21:17
您可以在位于ApiTester目录中的_support类中定义通用方法,然后可以在所有测试类中达到它。
我就是这么做的
class ApiTester extends \Codeception\Actor
{
use _generated\ApiTesterActions;
private $userToken = null;
public function getUserToken()
{
if ($this->userToken) {
return $this->userToken;
}
$this->generateUserToken();
return $this->userToken;
}
private function generateUserToken()
{
$email = 'testUser@test.com';
$password = '123456789';
$I = $this;
$encoder = new BCryptPasswordEncoder(12);
$I->haveInDatabase('users', [
'email' => $email,
'status' => UserConstants::USER_STATUS_VERIFIED,
'password' => $encoder->encodePassword($password, 'dasdsa'), // salt is not important for Bcrypt
'created_at' => '2019-01-01 20:20:20',
'updated_at' => '2019-01-01 20:20:20',
]);
$userId = $I->grabFromDatabase('users', 'id', [
'email' => $email
]);
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST('/api/v1/auth/login', ['username' => $email, 'password' => $password]);
$response = $I->grabResponse();
$responseArray = json_decode($response, true);
$this->userToken = $responseArray['token'];
}
}在我的测试类中,我像这样使用它
class UserControllerCest
{
/**
* @param ApiTester $I
*/
public function testIndexAction(ApiTester $I)
{
$token = $I->getUserToken();
//we should login first
$I->haveHttpHeader('Content-Type', 'application/json');
$I->amBearerAuthenticated($token);
$I->sendGET('/api/v1/posts');
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
}
}https://stackoverflow.com/questions/33603968
复制相似问题