public async Task DoesNotAddCorrelationIdToRequestWhenNotAvailable() { // Arrange var correlatingHttpClientHandler = new CorrelatingHttpClientHandler(); correlatingHttpClientHandler.InnerHandler = new HttpClientHandler(); var inspectingHandler = new RequestInspectingHandler(); inspectingHandler.InnerHandler = correlatingHttpClientHandler; // Act using (var client = new HttpClient(inspectingHandler)) { await client.GetAsync("https://www.nuget.org"); } // Assert var request = inspectingHandler.Requests.FirstOrDefault(); Assert.NotNull(request); Assert.False(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName)); }
public async Task AddsCorrelationIdToRequestWhenAvailable() { // Arrange var correlatingHttpClientHandler = new CorrelatingHttpClientHandler(); correlatingHttpClientHandler.InnerHandler = new HttpClientHandler(); var inspectingHandler = new RequestInspectingHandler(); inspectingHandler.InnerHandler = correlatingHttpClientHandler; var correlationId = Guid.NewGuid(); CallContext.LogicalSetData(WebApiCorrelationHandler.CallContextKey, correlationId); // Act using (var client = new HttpClient(inspectingHandler)) { await client.GetAsync("https://www.nuget.org"); } // Assert var request = inspectingHandler.Requests.FirstOrDefault(); Assert.NotNull(request); Assert.True(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName)); Assert.Equal(correlationId.ToString(), request.Headers.GetValues(WebApiCorrelationHandler.CorrelationIdHttpHeaderName).FirstOrDefault()); }