HeaderPropagation: reworded registration exception for clarity (#12636)

* HeaderPropagation: reworded registration exception for clarity

* feedback
This commit is contained in:
Alessio Franceschelli 2019-07-27 20:59:17 +01:00 committed by Ryan Nowak
parent a1e77a2c09
commit c23b9feb19
2 changed files with 26 additions and 3 deletions

View File

@ -48,8 +48,8 @@ namespace Microsoft.AspNetCore.HeaderPropagation
{
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);
}

View File

@ -64,10 +64,33 @@ namespace Microsoft.AspNetCore.HeaderPropagation.Tests
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()
{