|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Tests\HttpClient; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sentry\Agent\Transport\AgentClient; |
| 9 | +use Sentry\Event; |
| 10 | +use Sentry\HttpClient\Request; |
| 11 | +use Sentry\Options; |
| 12 | +use Sentry\Serializer\PayloadSerializer; |
| 13 | + |
| 14 | +final class AgentClientTest extends TestCase |
| 15 | +{ |
| 16 | + use TestAgent; |
| 17 | + |
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + if ($this->agentProcess !== null) { |
| 21 | + $this->stopTestAgent(); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public function testClientHandsOffEnvelopeToLocalAgent(): void |
| 26 | + { |
| 27 | + $this->startTestAgent(); |
| 28 | + |
| 29 | + $envelope = $this->createEnvelope('http://public@example.com/1', 'Hello from agent client test!'); |
| 30 | + |
| 31 | + $request = new Request(); |
| 32 | + $request->setStringBody($envelope); |
| 33 | + |
| 34 | + $client = new AgentClient('127.0.0.1', $this->agentPort); |
| 35 | + $response = $client->sendRequest($request, new Options()); |
| 36 | + |
| 37 | + $this->waitForEnvelopeCount(1); |
| 38 | + $agentOutput = $this->stopTestAgent(); |
| 39 | + |
| 40 | + $this->assertSame(202, $response->getStatusCode()); |
| 41 | + $this->assertSame('', $response->getError()); |
| 42 | + $this->assertCount(1, $agentOutput['messages']); |
| 43 | + $this->assertStringContainsString('Hello from agent client test!', $agentOutput['messages'][0]); |
| 44 | + $this->assertStringContainsString('"type":"event"', $agentOutput['messages'][0]); |
| 45 | + } |
| 46 | + |
| 47 | + public function testClientReturnsAcceptedWhenLocalAgentIsUnavailable(): void |
| 48 | + { |
| 49 | + $envelope = $this->createEnvelope('http://public@example.com/1', 'Hello from unavailable agent test!'); |
| 50 | + |
| 51 | + $request = new Request(); |
| 52 | + $request->setStringBody($envelope); |
| 53 | + |
| 54 | + $client = new AgentClient('127.0.0.1', 65001); |
| 55 | + |
| 56 | + set_error_handler(static function (): bool { |
| 57 | + return true; |
| 58 | + }); |
| 59 | + |
| 60 | + try { |
| 61 | + $response = $client->sendRequest($request, new Options()); |
| 62 | + } finally { |
| 63 | + restore_error_handler(); |
| 64 | + } |
| 65 | + |
| 66 | + $this->assertSame(202, $response->getStatusCode()); |
| 67 | + $this->assertSame('', $response->getError()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testClientReturnsErrorWhenBodyIsEmpty(): void |
| 71 | + { |
| 72 | + $client = new AgentClient(); |
| 73 | + $response = $client->sendRequest(new Request(), new Options()); |
| 74 | + |
| 75 | + $this->assertSame(400, $response->getStatusCode()); |
| 76 | + $this->assertTrue($response->hasError()); |
| 77 | + $this->assertSame('Request body is empty', $response->getError()); |
| 78 | + } |
| 79 | + |
| 80 | + private function createEnvelope(string $dsn, string $message): string |
| 81 | + { |
| 82 | + $options = new Options(['dsn' => $dsn]); |
| 83 | + |
| 84 | + $event = Event::createEvent(); |
| 85 | + $event->setMessage($message); |
| 86 | + |
| 87 | + $serializer = new PayloadSerializer($options); |
| 88 | + |
| 89 | + return $serializer->serialize($event); |
| 90 | + } |
| 91 | +} |
0 commit comments