Skip to content

HeaderPropagation: reworded registration exception for clarity #12636

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
{
var message =
$"The {nameof(HeaderPropagationValues)}.{nameof(HeaderPropagationValues.Headers)} property has not been " +
$"initialized. Register the header propagation middleware by adding 'app.{nameof(HeaderPropagationApplicationBuilderExtensions.UseHeaderPropagation)}() " +
$"in the 'Configure(...)' method.";
$"initialized. Register the header propagation middleware by adding 'app.{nameof(HeaderPropagationApplicationBuilderExtensions.UseHeaderPropagation)}()' " +
$"in the 'Configure(...)' method. Header propagation can only be used within the context of an HTTP request.";
throw new InvalidOperationException(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,33 @@ public async Task HeaderPropagation_WithoutMiddleware_Throws()
Assert.IsType<InvalidOperationException>(captured);
Assert.Equal(
"The HeaderPropagationValues.Headers property has not been initialized. Register the header propagation middleware " +
"by adding 'app.UseHeaderPropagation() in the 'Configure(...)' method.",
"by adding 'app.UseHeaderPropagation()' in the 'Configure(...)' method. Header propagation can only be used within " +
"the context of an HTTP request.",
captured.Message);
}

[Fact]
public async Task HeaderPropagation_OutsideOfIncomingRequest_Throws()
{
// Arrange
var services = new ServiceCollection();
services.AddHttpClient("test").AddHeaderPropagation();
services.AddHeaderPropagation(options =>
{
options.Headers.Add("X-TraceId");
});
var serviceProvider = services.BuildServiceProvider();

// Act & Assert
var client = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient("test");
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/"));
Assert.Equal(
"The HeaderPropagationValues.Headers property has not been initialized. Register the header propagation middleware " +
"by adding 'app.UseHeaderPropagation()' in the 'Configure(...)' method. Header propagation can only be used within " +
"the context of an HTTP request.",
exception.Message);
}

[Fact]
public async Task HeaderInRequest_AddCorrectValue()
{
Expand Down