public async Task SendAsync_CurrentActivitySet_RequestHeadersSet(bool useW3CFormat, ActivityContextHeaders activityContextHeaders)
        {
            const string TraceStateString = "CustomTraceStateString";
            string       expectedId       = null;

            var invoker = new HttpMessageInvoker(new ActivityPropagationHandler(activityContextHeaders, new MockHttpHandler(
                                                                                    (HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                var headers = request.Headers;

                Assert.True(headers.TryGetValues(useW3CFormat ? "traceparent" : "Request-Id", out var values));
                Assert.Equal(expectedId, Assert.Single(values));

                if (useW3CFormat)
                {
                    Assert.True(headers.TryGetValues("tracestate", out values));
                    Assert.Equal(TraceStateString, Assert.Single(values));
                }

                if (activityContextHeaders.HasFlag(ActivityContextHeaders.Baggage))
                {
                    Assert.True(headers.TryGetValues("Baggage", out values));
                    Assert.Equal("foo=bar", Assert.Single(values));
                }

                if (activityContextHeaders.HasFlag(ActivityContextHeaders.CorrelationContext))
                {
                    Assert.True(headers.TryGetValues("Correlation-Context", out values));
                    Assert.Equal("foo=bar", Assert.Single(values));
                }

                return(Task.FromResult <HttpResponseMessage>(null));
            })));

            var activity = new Activity("CustomOperation");

            if (useW3CFormat)
            {
                activity.SetIdFormat(ActivityIdFormat.W3C);
                activity.TraceStateString = TraceStateString;
                activity.SetParentId("00-01234567890123456789012345678901-0123456789012345-01");
            }
            else
            {
                activity.SetIdFormat(ActivityIdFormat.Hierarchical);
                activity.SetParentId("|root");
            }

            activity.AddBaggage("foo", "bar");

            activity.Start();
            expectedId = activity.Id;

            await invoker.SendAsync(new HttpRequestMessage(), CancellationToken.None);

            activity.Stop();
        }
        private void InjectHeaders(Activity currentActivity, HttpRequestMessage request)
        {
            if (currentActivity.IdFormat == ActivityIdFormat.W3C)
            {
                request.Headers.Remove(TraceParentHeaderName);
                request.Headers.Remove(TraceStateHeaderName);

                request.Headers.TryAddWithoutValidation(TraceParentHeaderName, currentActivity.Id);
                if (currentActivity.TraceStateString != null)
                {
                    request.Headers.TryAddWithoutValidation(TraceStateHeaderName, currentActivity.TraceStateString);
                }
            }
            else
            {
                request.Headers.Remove(RequestIdHeaderName);
                request.Headers.TryAddWithoutValidation(RequestIdHeaderName, currentActivity.Id);
            }

            // we expect baggage to be empty or contain a few items
            using (var e = currentActivity.Baggage.GetEnumerator())
            {
                if (e.MoveNext())
                {
                    var baggage = new List <string>();
                    do
                    {
                        var item = e.Current;
                        baggage.Add(new NameValueHeaderValue(Uri.EscapeDataString(item.Key), Uri.EscapeDataString(item.Value ?? string.Empty)).ToString());
                    }while (e.MoveNext());
                    if (_activityContextHeaders.HasFlag(ActivityContextHeaders.Baggage))
                    {
                        request.Headers.TryAddWithoutValidation(BaggageHeaderName, baggage);
                    }
                    if (_activityContextHeaders.HasFlag(ActivityContextHeaders.CorrelationContext))
                    {
                        request.Headers.TryAddWithoutValidation(CorrelationContextHeaderName, baggage);
                    }
                }
            }
        }