| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace Tests\Unit;
- use Google\Protobuf\Internal\Message;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Response;
- /**
- * protobuf请求测试
- *
- */
- abstract class ProtoJsonRequestTest extends \Tests\TestCase implements ProtoJsonRequest
- {
- protected Client $client;
- protected string $baseUrl = ''; // 修改为实际运行的服务器地址
- public $assertEquals = 200;
- public function setUp(): void
- {
- parent::setUp();
- $this->baseUrl = env('UNITTEST_URL', 'http://localhost:8000');
- $this->client = new Client([
- 'base_uri' => $this->baseUrl,
- 'timeout' => 5.0,
- 'http_errors' => false,
- 'verify' => false // 禁用 SSL 验证
- ]);
- }
- /**
- *
- * @return Message
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function protobufRequest(): Message
- {
- Log::info('准备 测试数据');
- // 1. 准备 Protobuf 请求数据
- $requestJson = $this->requestProtobufJson();
- Log::info('序列化请求数据完成', [
- 'data_length' => strlen($requestJson),
- 'request_data' => $requestJson
- ]);
- // 2. 发送真实的 HTTP 请求
- Log::info('发送 HTTP 请求');
- $response = $this->client->post('/gameapi', [
- 'body' => $requestJson,
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json'
- ]
- ]);
- Log::info('收到 HTTP 响应', [
- 'status_code' => $response->getStatusCode(),
- 'headers' => $response->getHeaders()
- ]);
- // 3. 验证 HTTP 响应状态码
- $this->assertEquals($this->assertEquals, $response->getStatusCode());
- // 4. 解析响应数据
- $responseContent = $response->getBody()->getContents();
- Log::info('获取响应内容', [
- 'content_length' => strlen($responseContent)
- ]);
- $protoResponse = new Response();
- $protoResponse->mergeFromJsonString($responseContent);
- Log::info('测试完成');
- return $protoResponse;
- }
- }
|