Skip to content

Commit dc99410

Browse files
authored
Merge pull request #23 from bilfeldt/features/correlation-id-middleware
Add CorrelationIdMiddleware
2 parents 7d2ddba + 969da51 commit dc99410

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ No breaking changes. The only changes are to the development dependencies used f
1010

1111
## Changes
1212

13+
### 2.2.0 - 2023-08-29
14+
15+
- Add middleware for adding 'Correlation-ID' header to responses
16+
1317
### 2.1.0 - 2023-07-24
1418

1519
- Add support for PHP 8.1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bilfeldt\RequestLogger\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class CorrelationIdMiddleware
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle(Request $request, Closure $next)
18+
{
19+
$correlationId = $this->getCorrelationId($request);
20+
21+
$request->headers->set('Correlation-ID', $correlationId, true);
22+
23+
$response = $next($request);
24+
25+
$response->headers->set('Correlation-ID', $correlationId, true); // This is available on all Response types whereas $response->header() is only available in \Illuminate\Http\Response
26+
27+
return $response;
28+
}
29+
30+
private function getCorrelationId(Request $request): string
31+
{
32+
return $request->getUniqueId();
33+
}
34+
}

src/Middleware/LogRequestMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function handle(Request $request, Closure $next, string ...$drivers)
3030
$response = $next($request);
3131

3232
if ($header = config('request-logger.header')) {
33-
$response->headers->set($header, $requestId, true); // This is available on all Response types whereas $request->header() is only available in \Illuminate\Http\Response
33+
$response->headers->set($header, $requestId, true); // This is available on all Response types whereas $response->header() is only available in \Illuminate\Http\Response
3434
}
3535

3636
return $response;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Bilfeldt\RequestLogger\Tests\Unit;
4+
5+
use Bilfeldt\RequestLogger\Middleware\CorrelationIdMiddleware;
6+
use Bilfeldt\RequestLogger\Tests\TestCase;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\Response;
9+
10+
class CorrelationIdMiddlewareTest extends TestCase
11+
{
12+
public function test_adds_request_header(): void
13+
{
14+
$request = new Request();
15+
16+
(new CorrelationIdMiddleware())->handle($request, function ($request) {
17+
$this->assertTrue($request->headers->has('Correlation-ID'));
18+
$this->assertEquals($request->getUniqueId(), $request->header('Correlation-ID'));
19+
20+
return new Response();
21+
});
22+
}
23+
24+
public function test_adds_response_header(): void
25+
{
26+
$request = new Request();
27+
28+
$response = (new CorrelationIdMiddleware())->handle($request, function ($request) {
29+
return new Response();
30+
});
31+
32+
$this->assertTrue($response->headers->has('Correlation-ID'));
33+
$this->assertEquals($request->getUniqueId(), $request->header('Correlation-ID'));
34+
}
35+
}

0 commit comments

Comments
 (0)