Skip to content

Commit

Permalink
Set X-Request-ID to server globals (#4)
Browse files Browse the repository at this point in the history
Set X-Request-ID to server globals
  • Loading branch information
joskfg authored Jun 28, 2021
1 parent 0b51b38 commit ca39267
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/RequestId.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
12 changes: 11 additions & 1 deletion tests/RequestIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.'
);
}
}
}

0 comments on commit ca39267

Please sign in to comment.