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