Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/TrackerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Ramsey\Uuid\UuidFactory;
use Symfony\Component\HttpFoundation\Request;

use function in_array;
use function is_array;
use function is_int;

class TrackerRequest
Expand All @@ -17,7 +19,7 @@ public function __construct(
protected TrackerHeader $header,
) {}

public function userId(int|string|null $id): static
public function userId(int|string|null $id = null): static
{
$id ??= $this->getUserId();

Expand Down Expand Up @@ -89,6 +91,8 @@ public function getRequest(): Request
protected function set(string $key, array|int|string|null $value): static
{
if (! is_int($value) && ! $value) {
$this->request->headers->remove($key);

return $this;
}

Expand All @@ -103,6 +107,25 @@ protected function set(string $key, array|int|string|null $value): static

protected function get(string $key): array|string|null
{
return $this->request->headers->get($key);
return $this->resolve(
$this->request->headers->get($key)
);
}

protected function resolve(array|int|string|null $value): array|string|null
{
if (is_array($value)) {
return $value;
}

if (is_int($value)) {
return (string) $value;
}

if (in_array($value, ['', 'null', '[]', '{}', '-'], true)) {
return null;
}

return $value;
}
}
14 changes: 12 additions & 2 deletions tests/Unit/Request/IpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
expect($telemetry->getIp())->toBe('203.0.113.10');
});

test('Else HTTP_X_REAL_IP (non-standard header name checked by the class)', function () {
test('HTTP_X_REAL_IP (non-standard header name checked by the class)', function () {
$header = new TrackerHeader;

$request = makeRequest();
Expand All @@ -24,7 +24,7 @@
expect($telemetry->getIp())->toBe('198.51.100.20');
});

test('Else client ip (REMOTE_ADDR)', function () {
test('client ip (REMOTE_ADDR)', function () {
$header = new TrackerHeader;

$request = makeRequest([], ['REMOTE_ADDR' => '192.0.2.30']);
Expand All @@ -43,6 +43,16 @@
expect($request->headers->get($header->ip))->toBe('127.0.0.1');
});

test('telemetry header is empty', function (string $value) {
$header = new TrackerHeader;

$request = makeRequest([$header->ip => $value]);
$telemetry = new TrackerRequest($request, $header);
$telemetry->ip();

expect($request->headers->get($header->ip))->toBe('127.0.0.1');
})->with(['', '-']);

test('ip() with value overrides and sets header', function () {
$header = new TrackerHeader;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@

expect($request->headers->get($header->traceId))->toBe('manual-id');
});

test('telemetry header is empty', function (string $value) {
$header = new TrackerHeader;

$request = makeRequest([$header->traceId => $value]);
$telemetry = new TrackerRequest($request, $header);
$telemetry->traceId();

$generated = $request->headers->get($header->traceId);

expect(Uuid::isValid($generated))->toBeTrue()
->and(Uuid::fromString($generated)->getVersion())->toBe(7);
})->with(['', '-']);
10 changes: 10 additions & 0 deletions tests/Unit/Request/UserIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@
$telemetry = new TrackerRequest($request, $header);
expect($telemetry->getUserId())->toBeNull();
});

test('telemetry header is empty', function (string $value) {
$header = new TrackerHeader;

$request = makeRequest([$header->userId => $value]);
$telemetry = new TrackerRequest($request, $header);
$telemetry->userId();

expect($request->headers->get($header->userId))->toBeNull();
})->with(['', '-']);