Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CorrelationIdMiddleware #23

Merged
merged 4 commits into from
Aug 29, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ No breaking changes. The only changes are to the development dependencies used f

## Changes

### 2.2.0 - 2023-08-29

- Add middleware for adding 'Correlation-ID' header to responses

### 2.1.0 - 2023-07-24

- Add support for PHP 8.1
Expand Down
34 changes: 34 additions & 0 deletions src/Middleware/CorrelationIdMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bilfeldt\RequestLogger\Middleware;

use Closure;
use Illuminate\Http\Request;

class CorrelationIdMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$correlationId = $this->getCorrelationId($request);

$request->headers->set('Correlation-ID', $correlationId, true);

$response = $next($request);

$response->headers->set('Correlation-ID', $correlationId, true); // This is available on all Response types whereas $response->header() is only available in \Illuminate\Http\Response

return $response;
}

private function getCorrelationId(Request $request): string
{
return $request->getUniqueId();
}
}
2 changes: 1 addition & 1 deletion src/Middleware/LogRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle(Request $request, Closure $next, string ...$drivers)
$response = $next($request);

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

return $response;
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/CorrelationIdMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Bilfeldt\RequestLogger\Tests\Unit;

use Bilfeldt\RequestLogger\Middleware\CorrelationIdMiddleware;
use Bilfeldt\RequestLogger\Tests\TestCase;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CorrelationIdMiddlewareTest extends TestCase
{
public function test_adds_request_header(): void
{
$request = new Request();

(new CorrelationIdMiddleware())->handle($request, function ($request) {
$this->assertTrue($request->headers->has('Correlation-ID'));
$this->assertEquals($request->getUniqueId(), $request->header('Correlation-ID'));

return new Response();
});
}

public function test_adds_response_header(): void
{
$request = new Request();

$response = (new CorrelationIdMiddleware())->handle($request, function ($request) {
return new Response();
});

$this->assertTrue($response->headers->has('Correlation-ID'));
$this->assertEquals($request->getUniqueId(), $request->header('Correlation-ID'));
}
}
Loading