Skip to content

Commit c23b9fe

Browse files
alefranzrynowak
authored andcommitted
HeaderPropagation: reworded registration exception for clarity (#12636)
* HeaderPropagation: reworded registration exception for clarity * feedback
1 parent a1e77a2 commit c23b9fe

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Middleware/HeaderPropagation/src/HeaderPropagationMessageHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
4848
{
4949
var message =
5050
$"The {nameof(HeaderPropagationValues)}.{nameof(HeaderPropagationValues.Headers)} property has not been " +
51-
$"initialized. Register the header propagation middleware by adding 'app.{nameof(HeaderPropagationApplicationBuilderExtensions.UseHeaderPropagation)}() " +
52-
$"in the 'Configure(...)' method.";
51+
$"initialized. Register the header propagation middleware by adding 'app.{nameof(HeaderPropagationApplicationBuilderExtensions.UseHeaderPropagation)}()' " +
52+
$"in the 'Configure(...)' method. Header propagation can only be used within the context of an HTTP request.";
5353
throw new InvalidOperationException(message);
5454
}
5555

src/Middleware/HeaderPropagation/test/HeaderPropagationIntegrationTest.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,33 @@ public async Task HeaderPropagation_WithoutMiddleware_Throws()
6464
Assert.IsType<InvalidOperationException>(captured);
6565
Assert.Equal(
6666
"The HeaderPropagationValues.Headers property has not been initialized. Register the header propagation middleware " +
67-
"by adding 'app.UseHeaderPropagation() in the 'Configure(...)' method.",
67+
"by adding 'app.UseHeaderPropagation()' in the 'Configure(...)' method. Header propagation can only be used within " +
68+
"the context of an HTTP request.",
6869
captured.Message);
6970
}
7071

72+
[Fact]
73+
public async Task HeaderPropagation_OutsideOfIncomingRequest_Throws()
74+
{
75+
// Arrange
76+
var services = new ServiceCollection();
77+
services.AddHttpClient("test").AddHeaderPropagation();
78+
services.AddHeaderPropagation(options =>
79+
{
80+
options.Headers.Add("X-TraceId");
81+
});
82+
var serviceProvider = services.BuildServiceProvider();
83+
84+
// Act & Assert
85+
var client = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient("test");
86+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/"));
87+
Assert.Equal(
88+
"The HeaderPropagationValues.Headers property has not been initialized. Register the header propagation middleware " +
89+
"by adding 'app.UseHeaderPropagation()' in the 'Configure(...)' method. Header propagation can only be used within " +
90+
"the context of an HTTP request.",
91+
exception.Message);
92+
}
93+
7194
[Fact]
7295
public async Task HeaderInRequest_AddCorrectValue()
7396
{

0 commit comments

Comments
 (0)