feat: 增加 httpclient 方法的 token 参数,添加用户登录和提现的测试用例

This commit is contained in:
lingling 2025-03-21 20:16:59 +08:00
parent a956648fa7
commit ff983a42f5
3 changed files with 91 additions and 6 deletions

View File

@ -44,15 +44,29 @@ class HttpBase
* @param string $method 请求方法GET/POST
* @param string|null $baseUri 动态设置 baseUri可选
* @param string $contentType 数据格式json/form
* @param string|null $token 认证 token可选
* @return array|false 响应的 JSON 数据或 false
*/
public static function httpclient($data, $url, $method = 'POST', $baseUri = null, $contentType = 'json')
public static function httpclient($data, $url, $method = 'POST', $baseUri = null, $contentType = 'json', $token = null)
{
$client = self::get_client($baseUri);
try {
$options = [];
// 设置请求头,支持动态传入 token
$headers = [
'Accept' => 'application/json',
];
if ($token) {
// 如果提供了 token则将其加入到 Authorization 头
$headers['Authorization'] = 'Bearer ' . $token;
}
// 添加请求头
$options['headers'] = $headers;
if (strtoupper($method) === 'GET') {
// GET 请求将数据作为查询参数
$options['query'] = $data;
@ -73,16 +87,14 @@ class HttpBase
$body = $response->getBody()->getContents();
return json_decode($body, true);
} catch (RequestException $e) {
// echo "HTTP 请求失败: " . $e->getMessage() . "\n";
// 捕获请求异常
if ($e->hasResponse()) {
$response = $e->getResponse();
$statusCode = $response->getStatusCode(); // 获取 HTTP 状态码
$body = $response->getBody()->getContents(); // 获取响应体
// echo "状态码: " . $statusCode . "\n";
// echo "响应体: " . $body . "\n";
// 这里可以根据需要打印响应信息
}
return $statusCode;
return $statusCode; // 返回状态码
}
}
}

18
tests/TestFunction.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Tests;
use App\Utils\API\Rocketgo;
use PHPUnit\Framework\TestCase;
/**
* 自用测试类
*/
class TestFunction extends TestCase
{
public function testPhone()
{
$res = HttpBase::httpclient([], '/api/text/classifyPhoneOnlineHistory', 'GET', null, 'form');
$this->assertEquals(1,$res['code']);
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Tests;
use App\Utils\API\Rocketgo;
use PHPUnit\Framework\TestCase;
/**
* 测试用户提现类
*/
class TestUserWithdraw extends TestCase
{
/**
* 存放用户登录token
*
* @var [type]
*/
private static $UserToken = null;
/**
* 测试用户登录功能
*
*/
public function testUserLogin()
{
$data = ['username' => '01930044627', 'password' => 'cCqQgG9koky^#uDFXllNUM46@jrI7KfsL77IIWwt'];
$res = HttpBase::httpclient($data, '/api/user/login', 'POST', null, 'form');
$token = $res['data']['userinfo']['token'];
$this->assertArrayHasKey('data', $res, "返回的数据应包含 'data' 键");
$this->assertArrayHasKey('userinfo', $res['data'], "返回的数据应包含 'userinfo' 键");
$this->assertArrayHasKey('token', $res['data']['userinfo'], "返回的 'userinfo' 应包含 'token' 键");
$this->assertNotEmpty($token, "token 应该是非空的");
// var_dump($res['data']['userinfo']['token']);
self::$UserToken = $token;
return $token;
}
/**
* 测试用户提款
* 依赖于 testUserLogin() 测试方法
*
* @depends testUserLogin
*/
public function testWithdrawSubmit($token)
{
$data = ['money' => '4500', 'bank_id' => '2923'];
$res = HttpBase::httpclient($data, '/api/withdraw/submit', 'POST', null, 'form',$token);
var_dump($res);
$res = HttpBase::httpclient($data, '/api/withdraw/submit', 'POST', null, 'form',$token);
var_dump($res);
$res = HttpBase::httpclient($data, '/api/withdraw/submit', 'POST', null, 'form',$token);
var_dump($res);
}
}