diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs b/test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs index 96717c4..c149ddb 100644 --- a/test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs +++ b/test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs @@ -4,4 +4,9 @@ public static class Constants { public const string AppName = "test-web"; public const string IntegrationEventIdHeaderName = "X-IntegrationEvent-Id"; -} + + public static class LogTemplates + { + public const string HandledIntegratonEvent = "Handled integration event {@event}."; + } +} \ No newline at end of file diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs b/test/Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs index 08ebc6b..b77a339 100644 --- a/test/Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs +++ b/test/Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs @@ -1,30 +1,22 @@ using Cnblogs.Architecture.Ddd.EventBus.Abstractions; using Cnblogs.Architecture.TestIntegrationEvents; +using static Cnblogs.Architecture.IntegrationTestProject.Constants; namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers; public class TestIntegrationEventHandler : IIntegrationEventHandler { - private readonly IHttpContextAccessor _httpContextAccessor; private readonly ILogger _logger; - public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor, ILogger logger) + public TestIntegrationEventHandler(ILogger logger) { - _httpContextAccessor = httpContextAccessor; _logger = logger; } public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken) { - var context = _httpContextAccessor.HttpContext; - context?.Response.OnStarting(() => - { - context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString()); - return Task.CompletedTask; - }); - - _logger.LogInformation("Handled integration event {event}.", notification); + _logger.LogInformation(LogTemplates.HandledIntegratonEvent, notification); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs index 5ba5c67..240ded6 100644 --- a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs +++ b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs @@ -17,7 +17,6 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddCnblogsApiVersioning(); builder.Services.AddSwaggerGen(); -builder.Services.AddHttpContextAccessor(); var app = builder.Build(); diff --git a/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj b/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj index df6a342..1646f25 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj +++ b/test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj @@ -1,5 +1,6 @@  + @@ -21,6 +22,12 @@ + + + + + + diff --git a/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs b/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs index 26a0ccd..5f1241a 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs @@ -6,7 +6,12 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Sinks.InMemory; +using Serilog.Sinks.InMemory.Assertions; using Xunit.Abstractions; +using static Cnblogs.Architecture.IntegrationTestProject.Constants; namespace Cnblogs.Architecture.IntegrationTests; @@ -24,15 +29,15 @@ public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync() { // Arrange var builder = WebApplication.CreateBuilder(); + builder.Logging.AddSerilog(logger => logger.WriteTo.InMemory().WriteTo.Console()); builder.Services - .AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly) - .AddHttpContextAccessor(); + .AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly); builder.WebHost.UseTestServer(); var app = builder.Build(); app.Subscribe(); await app.StartAsync(); var client = app.GetTestClient(); - var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!"); + var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, $"Hello World! {Guid.NewGuid()}"); // Act var subscriptions = await client.GetFromJsonAsync("/dapr/subscribe"); @@ -42,7 +47,8 @@ public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync() // Assert response.Should().BeSuccessful(); - response.Headers.Should().ContainKey(Constants.IntegrationEventIdHeaderName) - .WhoseValue.First().Should().Be(@event.Id.ToString()); + InMemorySink.Instance + .Should().HaveMessage(LogTemplates.HandledIntegratonEvent).Appearing().Once() + .WithProperty("event").HavingADestructuredObject().WithProperty("Id").WithValue(@event.Id); } } \ No newline at end of file