Skip to content

Commit 4a3eb41

Browse files
committed
Update Http::macro to handle correlation ID
1 parent bbd9b63 commit 4a3eb41

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ This package provides a robust foundation for building Laravel-based microservic
2323
- **Configurable Middleware Aliases & Groups:**
2424
Easily enable, disable, or rename middleware via configuration, and use convenient groups like `microservice.auth` group for full authentication and authorization in one step.
2525

26-
- **HTTP Client Macros:**
27-
Pre-configured HTTP clients for communicating with your API Gateway or other services.
26+
- **HTTP Client Macros:**
27+
Pre-configured HTTP clients for communicating with your API Gateway or other services. When a request is available, these macros automatically forward the current correlation ID header.
2828

2929
- **Ready-to-publish Configuration:**
3030
All settings are customizable via a single config file, making it easy to adapt the package to your environment.

src/Providers/MicroserviceServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,49 @@ public function boot(Router $router): void
8080

8181
// HTTP
8282
Http::macro('apiGateway', function () {
83+
// Correlation ID
84+
$header = config('microservice.correlation.header');
85+
$correlation = app()->bound('request') ? request()->header($header) : null;
86+
8387
return Http::acceptJson()
88+
->withHeaders($correlation ? [$header => $correlation] : [])
8489
->baseUrl(config('services.api_gateway.url'))
8590
->timeout(5)
8691
->retry(2, 100);
8792
});
8893

8994
Http::macro('apiGatewayDirect', function () {
95+
// Correlation ID
96+
$header = config('microservice.correlation.header');
97+
$correlation = app()->bound('request') ? request()->header($header) : null;
98+
9099
return Http::acceptJson()
100+
->withHeaders($correlation ? [$header => $correlation] : [])
91101
->baseUrl(config('services.api_gateway.url'))
92102
->timeout(5);
93103
});
94104

95105
Http::macro('apiGatewayWithToken', function (string $token) {
106+
// Correlation ID
107+
$header = config('microservice.correlation.header');
108+
$correlation = app()->bound('request') ? request()->header($header) : null;
109+
96110
return Http::acceptJson()
97111
->withToken($token)
112+
->withHeaders($correlation ? [$header => $correlation] : [])
98113
->baseUrl(config('services.api_gateway.url'))
99114
->timeout(5)
100115
->retry(2, 100);
101116
});
102117

103118
Http::macro('apiGatewayDirectWithToken', function (string $token) {
119+
// Correlation ID
120+
$header = config('microservice.correlation.header');
121+
$correlation = app()->bound('request') ? request()->header($header) : null;
122+
104123
return Http::acceptJson()
105124
->withToken($token)
125+
->withHeaders($correlation ? [$header => $correlation] : [])
106126
->baseUrl(config('services.api_gateway.url'))
107127
->timeout(5);
108128
});

tests/Http/HttpMacrosTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Tests\Http;
4+
5+
use Illuminate\Support\Facades\Http;
6+
use Illuminate\Support\Facades\Route;
7+
use Orchestra\Testbench\TestCase;
8+
9+
class HttpMacrosTest extends TestCase
10+
{
11+
protected function getPackageProviders($app)
12+
{
13+
return [\Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider::class];
14+
}
15+
16+
protected function getEnvironmentSetUp($app)
17+
{
18+
$app['config']->set('services.api_gateway.url', 'http://gateway.test');
19+
$app['config']->set('microservice.correlation.header', 'X-Correlation-ID');
20+
}
21+
22+
/** @test */
23+
public function macro_passes_correlation_header()
24+
{
25+
Http::fake();
26+
27+
Route::get('/macro', function () {
28+
Http::apiGateway()->get('/foo');
29+
return response()->json(['ok' => true]);
30+
});
31+
32+
$this->withHeaders(['X-Correlation-ID' => 'corr-123'])->get('/macro')->assertOk();
33+
34+
Http::assertSent(function ($request) {
35+
return $request->hasHeader('X-Correlation-ID', 'corr-123');
36+
});
37+
}
38+
39+
/** @test */
40+
public function macro_without_request_has_no_header()
41+
{
42+
Http::fake();
43+
44+
Http::apiGateway()->get('/foo');
45+
46+
Http::assertSent(function ($request) {
47+
return !$request->hasHeader('X-Correlation-ID');
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)