Skip to content

Commit 6d8ccf7

Browse files
committed
HeaderPropagation: reworded registration exception for clarity
1 parent 4aebd29 commit 6d8ccf7

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-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. Also, do not use an HttpClient with header propagation outside of an incoming HTTP request.";
5353
throw new InvalidOperationException(message);
5454
}
5555

src/Middleware/HeaderPropagation/test/HeaderPropagationIntegrationTest.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,35 @@ 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. Also, do not use an HttpClient with header " +
68+
"propagation outside of an incoming 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
85+
var client = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient("test");
86+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/"));
87+
88+
// Assert
89+
Assert.Equal(
90+
"The HeaderPropagationValues.Headers property has not been initialized. Register the header propagation middleware " +
91+
"by adding 'app.UseHeaderPropagation()' in the 'Configure(...)' method. Also, do not use an HttpClient with header " +
92+
"propagation outside of an incoming HTTP request.",
93+
exception.Message);
94+
}
95+
7196
[Fact]
7297
public async Task HeaderInRequest_AddCorrectValue()
7398
{

0 commit comments

Comments
 (0)