Skip to content

Commit

Permalink
Added more tests and simplified sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmark committed Apr 6, 2023
1 parent 1d1baca commit b13903e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
18 changes: 0 additions & 18 deletions samples/net48/Net48MvcSample/Controllers/ValuesController.cs

This file was deleted.

2 changes: 1 addition & 1 deletion samples/net48/Net48MvcSample/Net48MvcSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\ValuesController.cs" />
<Compile Include="Controllers\CorrelationIdController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand Down
34 changes: 31 additions & 3 deletions test/CorrelationId.Net48.Tests/HttpClientBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public HttpClientBuilderTests(ITestOutputHelper testOutputHelper)
new Uri(net48MvcSampleBaseUrl);
});

serviceCollection.AddCorrelationIdToHttpClientBuilder(httpClientBuilder);
serviceCollection.UseCorrelationIdMiddleware(httpClientBuilder);
var serviceProvider = serviceCollection.BuildServiceProvider();
_net48MvcSampleApiClient = serviceProvider.GetService<INet48MvcSampleApiClient>();
}
Expand All @@ -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);

}
}
}
5 changes: 2 additions & 3 deletions test/CorrelationId.Net48.Tests/INet48MvcSampleApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Refit;

namespace CorrelationId.Net48.Tests
{
public interface INet48MvcSampleApiClient
{
[Get("/api/values")]
Task<IApiResponse<IEnumerable<string>>> GetAsync();
[Get("/api/correlationid")]
Task<IApiResponse<string>> GetAsync();
}
}

0 comments on commit b13903e

Please sign in to comment.