diff --git a/src/RequestId.php b/src/RequestId.php index e06a242..384e961 100644 --- a/src/RequestId.php +++ b/src/RequestId.php @@ -24,15 +24,19 @@ class RequestId public function handle(Request $request, \Closure $next, $guard = null) { $uuid = $request->headers->get('X-Request-ID'); + if (is_null($uuid)) { $uuid = Uuid::uuid4()->toString(); + $request->headers->set('X-Request-ID', $uuid); } + $_SERVER['HTTP_X_REQUEST_ID'] = $uuid; + $response = $next($request); $response->headers->set('X-Request-ID', $uuid); return $response; } -} \ No newline at end of file +} diff --git a/tests/RequestIdTest.php b/tests/RequestIdTest.php index 73caba8..3280066 100644 --- a/tests/RequestIdTest.php +++ b/tests/RequestIdTest.php @@ -46,6 +46,11 @@ public function testRequestIdShouldBeFilledIfDoesNotExistInRequestAndResponse() $request->header('X-Request-ID'), 'The same X-Request-ID must be set in request and response.' ); + $this->assertEquals( + $response->headers->get('X-Request-ID'), + $_SERVER['HTTP_X_REQUEST_ID'], + 'The same X-Request-ID must be set in server globals.' + ); } public function testPropagateRequestIdToResponseIfProvidedInRequest() @@ -72,5 +77,10 @@ public function testPropagateRequestIdToResponseIfProvidedInRequest() $response->headers->get('X-Request-ID'), 'The request X-Request-ID header must be set in the response.' ); + $this->assertEquals( + '09226165-364a-461a-bf5c-e859d70d907e', + $_SERVER['HTTP_X_REQUEST_ID'], + 'The same X-Request-ID must be set in server globals.' + ); } -} \ No newline at end of file +}