| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace Tests\Unit;
- use Tests\TestCase;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Log;
- use Uraus\App\ResponseHelloAreYouOk;
- class Protobuf2ControllerTest extends TestCase
- {
- private Client $client;
- private string $baseUrl = ''; // 修改为实际运行的服务器地址
- 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 验证
- ]);
- Log::info('测试开始,设置客户端配置', [
- 'base_uri' => $this->baseUrl
- ]);
- }
- /**
- * 测试 Hello 模块的 AreYouOk 请求
- */
- public function testHelloAreYouOk()
- {
- Log::info('准备 Hello/AreYouOk 测试数据');
- // 1. 准备 Protobuf 请求数据
- $areYouOk = new ResponseHelloAreYouOk();
- $areYouOk->setMsg("你好" . uniqid());
- $areYouOk->setTimes(time());
- $requestData = $areYouOk->serializeToString();
- Log::info('序列化请求数据完成', [
- 'data_length' => strlen($requestData),
- 'request_data' => $areYouOk->serializeToJsonString()
- ]);
- // 2. 发送真实的 HTTP 请求
- Log::info('发送 HTTP 请求');
- $response = $this->client->post('/api/protobuf/HelloAreYouOk', [
- 'body' => $requestData,
- 'headers' => [
- 'Content-Type' => 'application/x-protobuf',
- 'Accept' => 'application/x-protobuf'
- ]
- ]);
- Log::info('收到 HTTP 响应', [
- 'status_code' => $response->getStatusCode(),
- 'headers' => $response->getHeaders()
- ]);
- // 3. 验证 HTTP 响应状态码
- $this->assertEquals(200, $response->getStatusCode());
- // 4. 解析响应数据
- $responseContent = $response->getBody()->getContents();
- Log::info('获取响应内容', [
- 'content_length' => strlen($responseContent)
- ]);
- $protoResponse = new ResponseHelloAreYouOk();
- $protoResponse->mergeFromString($responseContent);
- dump($protoResponse->serializeToJsonString());
- Log::info('测试完成');
- }
- }
|