From d10f1f945dc9afd5cb233e7bf9f47befb714db54 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 27 Sep 2022 14:51:41 -0700 Subject: [PATCH] Unit test to validate spans in http redirect (#3699) --- .../HttpClientTests.Basic.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs index 3b49cac03..3272e8f5c 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.cs @@ -39,7 +39,16 @@ namespace OpenTelemetry.Instrumentation.Http.Tests this.serverLifeTime = TestHttpServer.RunServer( (ctx) => { - ctx.Response.StatusCode = 200; + if (ctx.Request.Url.PathAndQuery.Contains("redirect")) + { + ctx.Response.RedirectLocation = "/"; + ctx.Response.StatusCode = 302; + } + else + { + ctx.Response.StatusCode = 200; + } + ctx.Response.OutputStream.Close(); }, out var host, @@ -317,6 +326,28 @@ namespace OpenTelemetry.Instrumentation.Http.Tests Assert.Equal(4, processor.Invocations.Count); // SetParentProvider/OnShutdown/Dispose/OnStart called. } + [Fact] + public async Task HttpClientRedirectTest() + { + var processor = new Mock>(); + using (Sdk.CreateTracerProviderBuilder() + .AddHttpClientInstrumentation() + .AddProcessor(processor.Object) + .Build()) + { + using var c = new HttpClient(); + await c.GetAsync($"{this.url}redirect"); + } + + Assert.Equal(7, processor.Invocations.Count); // SetParentProvider/OnStart/OnEnd/OnStart/OnEnd/OnShutdown/Dispose called. + + var firstActivity = (Activity)processor.Invocations[2].Arguments[0]; // First OnEnd + Assert.Contains(firstActivity.TagObjects, t => t.Key == "http.status_code" && (int)t.Value == 302); + + var secondActivity = (Activity)processor.Invocations[4].Arguments[0]; // Second OnEnd + Assert.Contains(secondActivity.TagObjects, t => t.Key == "http.status_code" && (int)t.Value == 200); + } + [Fact] public async void RequestNotCollectedWhenInstrumentationFilterApplied() {