|
|
@@ -0,0 +1,90 @@
|
|
|
+<?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 ProtoRequest
|
|
|
+{
|
|
|
+
|
|
|
+ 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 请求数据
|
|
|
+ $requestData = $this->create_request_protobuf();
|
|
|
+ $name = substr(get_class($requestData), 17);
|
|
|
+ $requestDatabin = $requestData->serializeToString();
|
|
|
+ Log::info('序列化请求数据完成', [
|
|
|
+ 'data_length' => strlen($requestDatabin),
|
|
|
+ 'request_data' => $requestData->serializeToJsonString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 2. 发送真实的 HTTP 请求
|
|
|
+ Log::info('发送 HTTP 请求');
|
|
|
+ $response = $this->client->post('/gameapi', [
|
|
|
+ 'body' => $requestDatabin,
|
|
|
+ 'headers' => [
|
|
|
+ 'Content-Type' => 'application/x-protobuf',
|
|
|
+ 'Accept' => 'application/x-protobuf'
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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->mergeFromString($responseContent);
|
|
|
+
|
|
|
+ Log::info('测试完成');
|
|
|
+
|
|
|
+ return $protoResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|