Skip to content

Commit

Permalink
improved http client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Feb 13, 2024
1 parent c5a0b32 commit 1ab99d3
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions tests/Instrumentation/HttpClientInstrumentationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Server\Server;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
Expand All @@ -9,17 +8,9 @@
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;

beforeEach(function () {
Server::start();
});

afterEach(function () {
Server::stop();
});

it('injects propagation headers to Http client request', function () {
Server::enqueue([
new Response(200, ['Content-Length' => 0]),
$http = Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0]),
]);

$root = Tracer::newSpan('root')->start();
Expand All @@ -36,16 +27,16 @@

$httpSpan = Arr::get($spans, count($spans) - 2);

$request = Server::received()[0];
$request = Http::recorded()->first()[0];
assert($request instanceof \Illuminate\Http\Client\Request);

expect($request)
->hasHeader('traceparent')->toBeTrue()
->getHeader('traceparent')->toBe([sprintf('00-%s-%s-01', $traceId, $httpSpan->getSpanId())]);
->header('traceparent')->toBe([sprintf('00-%s-%s-01', $traceId, $httpSpan->getSpanId())]);
});

it('create http client span', function () {
Server::enqueue([
new Response(200, ['Content-Length' => 0]),
Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0]),
]);

Http::withTrace()->get(Server::$url);
Expand All @@ -72,8 +63,8 @@
});

it('set span status to error on 4xx and 5xx status code', function () {
Server::enqueue([
new Response(500, ['Content-Length' => 0]),
Http::fake([
'*' => Http::response('', 500, ['Content-Length' => 0]),
]);

Http::withTrace()->get(Server::$url);
Expand All @@ -98,8 +89,8 @@
],
]);

Server::enqueue([
new Response(200, ['Content-Length' => 0]),
Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0]),
]);

Http::withHeaders([
Expand All @@ -123,8 +114,8 @@
],
]);

Server::enqueue([
new Response(200, ['Content-Length' => 0, 'Content-Type' => 'text/html; charset=UTF-8']),
Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0, 'Content-Type' => 'text/html; charset=UTF-8']),
]);

Http::withTrace()->get(Server::$url);
Expand All @@ -148,8 +139,8 @@
],
]);

Server::enqueue([
new Response(200, ['Content-Length' => 0]),
Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0]),
]);

Http::withHeaders(['x-foo' => 'bar'])->withTrace()->get(Server::$url);
Expand All @@ -171,8 +162,8 @@
],
]);

Server::enqueue([
new Response(200, ['Content-Length' => 0, 'Set-Cookie' => 'cookie']),
Http::fake([
'*' => Http::response('', 200, ['Content-Length' => 0, 'Set-Cookie' => 'cookie']),
]);

Http::withHeaders([
Expand Down

0 comments on commit 1ab99d3

Please sign in to comment.