Skip to content

Commit 1641d81

Browse files
Merge pull request #18
Added handling of empty values
2 parents 263eff0 + bf080de commit 1641d81

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/TrackerRequest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Ramsey\Uuid\UuidFactory;
99
use Symfony\Component\HttpFoundation\Request;
1010

11+
use function in_array;
12+
use function is_array;
1113
use function is_int;
1214

1315
class TrackerRequest
@@ -17,7 +19,7 @@ public function __construct(
1719
protected TrackerHeader $header,
1820
) {}
1921

20-
public function userId(int|string|null $id): static
22+
public function userId(int|string|null $id = null): static
2123
{
2224
$id ??= $this->getUserId();
2325

@@ -89,6 +91,8 @@ public function getRequest(): Request
8991
protected function set(string $key, array|int|string|null $value): static
9092
{
9193
if (! is_int($value) && ! $value) {
94+
$this->request->headers->remove($key);
95+
9296
return $this;
9397
}
9498

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

104108
protected function get(string $key): array|string|null
105109
{
106-
return $this->request->headers->get($key);
110+
return $this->resolve(
111+
$this->request->headers->get($key)
112+
);
113+
}
114+
115+
protected function resolve(array|int|string|null $value): array|string|null
116+
{
117+
if (is_array($value)) {
118+
return $value;
119+
}
120+
121+
if (is_int($value)) {
122+
return (string) $value;
123+
}
124+
125+
if (in_array($value, ['', 'null', '[]', '{}', '-'], true)) {
126+
return null;
127+
}
128+
129+
return $value;
107130
}
108131
}

tests/Unit/Request/IpTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
expect($telemetry->getIp())->toBe('203.0.113.10');
1515
});
1616

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

2020
$request = makeRequest();
@@ -24,7 +24,7 @@
2424
expect($telemetry->getIp())->toBe('198.51.100.20');
2525
});
2626

27-
test('Else client ip (REMOTE_ADDR)', function () {
27+
test('client ip (REMOTE_ADDR)', function () {
2828
$header = new TrackerHeader;
2929

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

46+
test('telemetry header is empty', function (string $value) {
47+
$header = new TrackerHeader;
48+
49+
$request = makeRequest([$header->ip => $value]);
50+
$telemetry = new TrackerRequest($request, $header);
51+
$telemetry->ip();
52+
53+
expect($request->headers->get($header->ip))->toBe('127.0.0.1');
54+
})->with(['', '-']);
55+
4656
test('ip() with value overrides and sets header', function () {
4757
$header = new TrackerHeader;
4858

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,16 @@
4646

4747
expect($request->headers->get($header->traceId))->toBe('manual-id');
4848
});
49+
50+
test('telemetry header is empty', function (string $value) {
51+
$header = new TrackerHeader;
52+
53+
$request = makeRequest([$header->traceId => $value]);
54+
$telemetry = new TrackerRequest($request, $header);
55+
$telemetry->traceId();
56+
57+
$generated = $request->headers->get($header->traceId);
58+
59+
expect(Uuid::isValid($generated))->toBeTrue()
60+
->and(Uuid::fromString($generated)->getVersion())->toBe(7);
61+
})->with(['', '-']);

tests/Unit/Request/UserIdTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@
4444
$telemetry = new TrackerRequest($request, $header);
4545
expect($telemetry->getUserId())->toBeNull();
4646
});
47+
48+
test('telemetry header is empty', function (string $value) {
49+
$header = new TrackerHeader;
50+
51+
$request = makeRequest([$header->userId => $value]);
52+
$telemetry = new TrackerRequest($request, $header);
53+
$telemetry->userId();
54+
55+
expect($request->headers->get($header->userId))->toBeNull();
56+
})->with(['', '-']);

0 commit comments

Comments
 (0)