Skip to content

Commit 4321e2a

Browse files
committed
update TraceableHttpClientTest unit test to assert baggage header value
1 parent 990d5a4 commit 4321e2a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

tests/Tracing/HttpClient/TraceableHttpClientTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPUnit\Framework\TestCase;
99
use Psr\Log\LoggerAwareInterface;
1010
use Psr\Log\NullLogger;
11+
use Sentry\ClientInterface;
12+
use Sentry\Options;
1113
use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableResponse;
1214
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
1315
use Sentry\State\HubInterface;
@@ -68,6 +70,7 @@ public function testRequest(): void
6870
$this->assertSame('GET', $response->getInfo('http_method'));
6971
$this->assertSame('https://www.example.com/test-page', $response->getInfo('url'));
7072
$this->assertSame(['sentry-trace: ' . $transaction->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
73+
$this->assertArrayNotHasKey('baggage', $mockResponse->getRequestOptions()['normalized_headers']);
7174
$this->assertNotNull($transaction->getSpanRecorder());
7275

7376
$spans = $transaction->getSpanRecorder()->getSpans();
@@ -83,6 +86,102 @@ public function testRequest(): void
8386
$this->assertSame($expectedTags, $spans[1]->getTags());
8487
}
8588

89+
public function testRequestWithInvalidBaggage(): void
90+
{
91+
$options = new Options([
92+
'dsn' => 'http://public:[email protected]/sentry/1',
93+
'trace_propagation_targets' => ['non-matching-host.invalid'],
94+
]);
95+
$client = $this->createMock(ClientInterface::class);
96+
$client
97+
->expects($this->once())
98+
->method('getOptions')
99+
->willReturn($options);
100+
101+
$transaction = new Transaction(new TransactionContext());
102+
$transaction->initSpanRecorder();
103+
104+
$this->hub->expects($this->once())
105+
->method('getSpan')
106+
->willReturn($transaction);
107+
$this->hub->expects($this->once())
108+
->method('getClient')
109+
->willReturn($client);
110+
111+
$mockResponse = new MockResponse();
112+
$decoratedHttpClient = new MockHttpClient($mockResponse);
113+
$httpClient = new TraceableHttpClient($decoratedHttpClient, $this->hub);
114+
$response = $httpClient->request('PUT', 'https://www.example.com/test-page');
115+
116+
$this->assertInstanceOf(AbstractTraceableResponse::class, $response);
117+
$this->assertSame(200, $response->getStatusCode());
118+
$this->assertSame('PUT', $response->getInfo('http_method'));
119+
$this->assertSame('https://www.example.com/test-page', $response->getInfo('url'));
120+
$this->assertSame(['sentry-trace: ' . $transaction->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
121+
$this->assertArrayNotHasKey('baggage', $mockResponse->getRequestOptions()['normalized_headers']);
122+
$this->assertNotNull($transaction->getSpanRecorder());
123+
124+
$spans = $transaction->getSpanRecorder()->getSpans();
125+
$expectedTags = [
126+
'http.method' => 'PUT',
127+
'http.url' => 'https://www.example.com/test-page',
128+
];
129+
130+
$this->assertCount(2, $spans);
131+
$this->assertNull($spans[1]->getEndTimestamp());
132+
$this->assertSame('http.client', $spans[1]->getOp());
133+
$this->assertSame('HTTP PUT', $spans[1]->getDescription());
134+
$this->assertSame($expectedTags, $spans[1]->getTags());
135+
}
136+
137+
public function testRequestWithValidBaggage(): void
138+
{
139+
$options = new Options([
140+
'dsn' => 'http://public:[email protected]/sentry/1',
141+
'trace_propagation_targets' => ['www.example.com'],
142+
]);
143+
$client = $this->createMock(ClientInterface::class);
144+
$client
145+
->expects($this->once())
146+
->method('getOptions')
147+
->willReturn($options);
148+
149+
$transaction = new Transaction(new TransactionContext());
150+
$transaction->initSpanRecorder();
151+
152+
$this->hub->expects($this->once())
153+
->method('getSpan')
154+
->willReturn($transaction);
155+
$this->hub->expects($this->once())
156+
->method('getClient')
157+
->willReturn($client);
158+
159+
$mockResponse = new MockResponse();
160+
$decoratedHttpClient = new MockHttpClient($mockResponse);
161+
$httpClient = new TraceableHttpClient($decoratedHttpClient, $this->hub);
162+
$response = $httpClient->request('POST', 'https://www.example.com/test-page');
163+
164+
$this->assertInstanceOf(AbstractTraceableResponse::class, $response);
165+
$this->assertSame(200, $response->getStatusCode());
166+
$this->assertSame('POST', $response->getInfo('http_method'));
167+
$this->assertSame('https://www.example.com/test-page', $response->getInfo('url'));
168+
$this->assertSame(['sentry-trace: ' . $transaction->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
169+
$this->assertSame(['baggage: ' . $transaction->toBaggage()], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
170+
$this->assertNotNull($transaction->getSpanRecorder());
171+
172+
$spans = $transaction->getSpanRecorder()->getSpans();
173+
$expectedTags = [
174+
'http.method' => 'POST',
175+
'http.url' => 'https://www.example.com/test-page',
176+
];
177+
178+
$this->assertCount(2, $spans);
179+
$this->assertNull($spans[1]->getEndTimestamp());
180+
$this->assertSame('http.client', $spans[1]->getOp());
181+
$this->assertSame('HTTP POST', $spans[1]->getDescription());
182+
$this->assertSame($expectedTags, $spans[1]->getTags());
183+
}
184+
86185
public function testStream(): void
87186
{
88187
$transaction = new Transaction(new TransactionContext());

0 commit comments

Comments
 (0)