Skip to content

Commit

Permalink
Add ActivityPropagationHandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Nov 10, 2020
1 parent 5f850e2 commit ff6e65f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/ReverseProxy/Telemetry/ActivityPropagationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
Expand Down
31 changes: 31 additions & 0 deletions test/ReverseProxy.Tests/Common/MockHttpHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.ReverseProxy.Common.Tests
{
internal class MockHttpHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _func;

public MockHttpHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func)
{
_func = func ?? throw new ArgumentNullException(nameof(func));
}

public static HttpMessageInvoker CreateClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func)
{
var handler = new MockHttpHandler(func);
return new HttpMessageInvoker(handler);
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return _func(request, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ private void VerifyValidAbstractConfig(ConfigurationData validConfig, X509Certif
Assert.Equal(validConfig.Clusters["cluster1"].SessionAffinity.Settings, abstractCluster1.SessionAffinity.Settings);
Assert.Same(certificate, abstractCluster1.HttpClient.ClientCertificate);
Assert.Equal(validConfig.Clusters["cluster1"].HttpClient.MaxConnectionsPerServer, abstractCluster1.HttpClient.MaxConnectionsPerServer);
Assert.Equal(validConfig.Clusters["cluster1"].HttpClient.PropagateActivityContext, abstractCluster1.HttpClient.PropagateActivityContext);
Assert.Equal(SslProtocols.Tls11 | SslProtocols.Tls12, abstractCluster1.HttpClient.SslProtocols);
Assert.Equal(validConfig.Clusters["cluster1"].HttpClient.DangerousAcceptAnyServerCertificate, abstractCluster1.HttpClient.DangerousAcceptAnyServerCertificate);
Assert.Equal(validConfig.Clusters["cluster1"].HttpRequest.RequestTimeout, abstractCluster1.HttpRequest.RequestTimeout);
Expand Down
21 changes: 0 additions & 21 deletions test/ReverseProxy.Tests/Service/Proxy/HttpProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,27 +1606,6 @@ private static string StreamToString(Stream stream)
return reader.ReadToEnd();
}

private class MockHttpHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func;

private MockHttpHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func)
{
this.func = func ?? throw new ArgumentNullException(nameof(func));
}

public static HttpMessageInvoker CreateClient(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> func)
{
var handler = new MockHttpHandler(func);
return new HttpMessageInvoker(handler);
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return func(request, cancellationToken);
}
}

private class DuplexStream : Stream
{
public DuplexStream(Stream readStream, Stream writeStream)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ReverseProxy.Common.Tests;
using Microsoft.ReverseProxy.Telemetry;
using Xunit;

namespace Microsoft.ReverseProxy.Service.Proxy.Tests
{
public class ActivityPropagationHandlerTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task SendAsync_CurrentActivitySet_RequestHeadersSet(bool useW3CFormat)
{
const string TraceStateString = "CustomTraceStateString";
string expectedId = null;

var invoker = new HttpMessageInvoker(new ActivityPropagationHandler(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));
}
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();
}
}
}

0 comments on commit ff6e65f

Please sign in to comment.