mirror of https://github.com/abpframework/abp.git
6 changed files with 125 additions and 13 deletions
@ -0,0 +1,17 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
using Volo.Abp.Tracing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.CorrelationIdProvider; |
|||
|
|||
[Route("api/correlation")] |
|||
public class CorrelationIdProviderController : AbpController |
|||
{ |
|||
[HttpGet] |
|||
[Route("get")] |
|||
public string Get() |
|||
{ |
|||
return this.HttpContext.RequestServices.GetRequiredService<ICorrelationIdProvider>().Get(); |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.CorrelationIdProvider; |
|||
|
|||
public class CorrelationIdProvider_Tests : AspNetCoreMvcTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Test() |
|||
{ |
|||
// Test AbpCorrelationIdMiddleware without X-Correlation-Id header
|
|||
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/correlation/404")) |
|||
{ |
|||
var response = await Client.SendAsync(requestMessage); |
|||
response.StatusCode.ShouldBe(HttpStatusCode.NotFound); |
|||
|
|||
response.Headers.ShouldContain(x => x.Key == "X-Correlation-Id" && x.Value.First() != null); |
|||
} |
|||
|
|||
var correlationId = Guid.NewGuid().ToString("N"); |
|||
|
|||
// Test AbpCorrelationIdMiddleware
|
|||
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/correlation/404")) |
|||
{ |
|||
requestMessage.Headers.Add("X-Correlation-Id", correlationId); |
|||
var response = await Client.SendAsync(requestMessage); |
|||
response.StatusCode.ShouldBe(HttpStatusCode.NotFound); |
|||
|
|||
response.Headers.ShouldContain(x => x.Key == "X-Correlation-Id" && x.Value.First() == correlationId); |
|||
} |
|||
|
|||
// Test AspNetCoreCorrelationIdProvider
|
|||
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, "/api/correlation/get")) |
|||
{ |
|||
requestMessage.Headers.Add("X-Correlation-Id", correlationId); |
|||
var response = await Client.SendAsync(requestMessage); |
|||
response.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
|
|||
(await response.Content.ReadAsStringAsync()).ShouldBe(correlationId); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Tracing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.CorrelationIdProvider; |
|||
|
|||
public class CorrelationIdProvider_Tests |
|||
{ |
|||
[Fact] |
|||
public async Task Test() |
|||
{ |
|||
using (var application = await AbpApplicationFactory.CreateAsync<IndependentEmptyModule>()) |
|||
{ |
|||
await application.InitializeAsync(); |
|||
|
|||
var correlationIdProvider = application.ServiceProvider.GetRequiredService<ICorrelationIdProvider>(); |
|||
var currentCorrelationId = correlationIdProvider.Get(); |
|||
currentCorrelationId.ShouldNotBeNull(); |
|||
|
|||
var correlationId = Guid.NewGuid().ToString("N"); |
|||
using (correlationIdProvider.Change(correlationId)) |
|||
{ |
|||
correlationIdProvider.Get().ShouldBe(correlationId); |
|||
} |
|||
|
|||
//The default correlation id always changes.
|
|||
correlationIdProvider.Get().ShouldNotBe(currentCorrelationId); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue