From b13903e8543ab9e709671e52851c3a28bac448a2 Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Thu, 6 Apr 2023 02:07:02 +0200 Subject: [PATCH] Added more tests and simplified sample --- .../Controllers/CorrelationIdController.cs | 14 ++++++++ .../Controllers/ValuesController.cs | 18 ---------- .../Net48MvcSample/Net48MvcSample.csproj | 2 +- ...orrelationIdServiceCollectionExtensions.cs | 2 +- .../HttpClientBuilderTests.cs | 34 +++++++++++++++++-- .../INet48MvcSampleApiClient.cs | 5 ++- 6 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 samples/net48/Net48MvcSample/Controllers/CorrelationIdController.cs delete mode 100644 samples/net48/Net48MvcSample/Controllers/ValuesController.cs diff --git a/samples/net48/Net48MvcSample/Controllers/CorrelationIdController.cs b/samples/net48/Net48MvcSample/Controllers/CorrelationIdController.cs new file mode 100644 index 0000000..996049b --- /dev/null +++ b/samples/net48/Net48MvcSample/Controllers/CorrelationIdController.cs @@ -0,0 +1,14 @@ +using System.Web; +using System.Web.Http; + +namespace Net48MvcSample.Controllers +{ + public class CorrelationIdController : ApiController + { + public string Get() + { + var correlationId = HttpContext.Current.Request.Headers["X-Correlation-Id"]; + return correlationId; + } + } +} diff --git a/samples/net48/Net48MvcSample/Controllers/ValuesController.cs b/samples/net48/Net48MvcSample/Controllers/ValuesController.cs deleted file mode 100644 index 45ae3aa..0000000 --- a/samples/net48/Net48MvcSample/Controllers/ValuesController.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Http; - -namespace Net48MvcSample.Controllers -{ - public class ValuesController : ApiController - { - public IEnumerable Get() - { - var correlationId = HttpContext.Current.Request.Headers["X-Correlation-Id"]; - var response = new[] { "value1", "value2", correlationId }; - return response; - } - } -} diff --git a/samples/net48/Net48MvcSample/Net48MvcSample.csproj b/samples/net48/Net48MvcSample/Net48MvcSample.csproj index 67dc484..5a11091 100644 --- a/samples/net48/Net48MvcSample/Net48MvcSample.csproj +++ b/samples/net48/Net48MvcSample/Net48MvcSample.csproj @@ -122,7 +122,7 @@ - + Global.asax diff --git a/test/CorrelationId.Net48.Tests/CorrelationIdServiceCollectionExtensions.cs b/test/CorrelationId.Net48.Tests/CorrelationIdServiceCollectionExtensions.cs index a5cd1fe..38e6338 100644 --- a/test/CorrelationId.Net48.Tests/CorrelationIdServiceCollectionExtensions.cs +++ b/test/CorrelationId.Net48.Tests/CorrelationIdServiceCollectionExtensions.cs @@ -8,7 +8,7 @@ namespace CorrelationId.Net48.Tests { public static class CorrelationIdServiceCollectionExtensions { - public static void AddCorrelationIdToHttpClientBuilder(this ServiceCollection serviceCollection, IHttpClientBuilder httpClientBuilder) + public static void UseCorrelationIdMiddleware(this ServiceCollection serviceCollection, IHttpClientBuilder httpClientBuilder) { serviceCollection.AddTransient(x => { diff --git a/test/CorrelationId.Net48.Tests/HttpClientBuilderTests.cs b/test/CorrelationId.Net48.Tests/HttpClientBuilderTests.cs index 16f95e8..20a848f 100644 --- a/test/CorrelationId.Net48.Tests/HttpClientBuilderTests.cs +++ b/test/CorrelationId.Net48.Tests/HttpClientBuilderTests.cs @@ -37,7 +37,7 @@ public HttpClientBuilderTests(ITestOutputHelper testOutputHelper) new Uri(net48MvcSampleBaseUrl); }); - serviceCollection.AddCorrelationIdToHttpClientBuilder(httpClientBuilder); + serviceCollection.UseCorrelationIdMiddleware(httpClientBuilder); var serviceProvider = serviceCollection.BuildServiceProvider(); _net48MvcSampleApiClient = serviceProvider.GetService(); } @@ -52,8 +52,36 @@ public async Task CallTo_Net48Service_ShouldSetCorrelationId() Assert.NotNull(response.Content); _testOutputHelper.WriteLine(string.Join(", ", response.Content)); - Assert.Equal(3, response.Content.Count()); - Assert.NotEmpty(response.Content.Last()); + Assert.NotNull(response.Content); + Assert.True(Guid.TryParse(response.Content.Replace("\"", string.Empty), out _)); + } + + [Fact] + public async Task CallTo_Net48Service_MultipleCalls_ShouldSetDifferentCorrelationId() + { + var responseTask = _net48MvcSampleApiClient.GetAsync(); + var response2Task = _net48MvcSampleApiClient.GetAsync(); + Task.WaitAll(responseTask, response2Task); + + var response = await responseTask; + Assert.True(response.IsSuccessStatusCode, response.Error?.Content); + + var response2 = await response2Task; + Assert.True(response2.IsSuccessStatusCode, response2.Error?.Content); + + Assert.NotNull(response.Content); + Assert.NotNull(response2.Content); + _testOutputHelper.WriteLine(string.Join(", ", response.Content)); + _testOutputHelper.WriteLine(string.Join(", ", response2.Content)); + + Assert.NotNull(response.Content); + Assert.True(Guid.TryParse(response.Content.Replace("\"", string.Empty), out _)); + + Assert.NotNull(response2.Content); + Assert.True(Guid.TryParse(response2.Content.Replace("\"", string.Empty), out _)); + + Assert.NotEqual(response, response2); + } } } \ No newline at end of file diff --git a/test/CorrelationId.Net48.Tests/INet48MvcSampleApiClient.cs b/test/CorrelationId.Net48.Tests/INet48MvcSampleApiClient.cs index 88083cf..3e3b0cc 100644 --- a/test/CorrelationId.Net48.Tests/INet48MvcSampleApiClient.cs +++ b/test/CorrelationId.Net48.Tests/INet48MvcSampleApiClient.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Threading.Tasks; using Refit; @@ -6,7 +5,7 @@ namespace CorrelationId.Net48.Tests { public interface INet48MvcSampleApiClient { - [Get("/api/values")] - Task>> GetAsync(); + [Get("/api/correlationid")] + Task> GetAsync(); } } \ No newline at end of file