|
| 1 | +using Chats.BE.Services.RequestTracing; |
| 2 | +using Chats.DB; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Logging.Abstractions; |
| 6 | + |
| 7 | +namespace Chats.BE.UnitTest.Services.RequestTracing; |
| 8 | + |
| 9 | +public sealed class RequestTracePersistServiceTests |
| 10 | +{ |
| 11 | + private static ServiceProvider CreateServiceProvider(string dbName) |
| 12 | + { |
| 13 | + ServiceCollection services = new(); |
| 14 | + services.AddDbContext<ChatsDB>(o => o.UseInMemoryDatabase(dbName)); |
| 15 | + return services.BuildServiceProvider(); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public async Task PersistSingleAsync_InMemoryBranch_UpdatesByLogIdWithoutCandidateQuery() |
| 20 | + { |
| 21 | + ServiceProvider sp = CreateServiceProvider(nameof(PersistSingleAsync_InMemoryBranch_UpdatesByLogIdWithoutCandidateQuery)); |
| 22 | + RequestTracePersistService service = new( |
| 23 | + new RequestTraceQueue(), |
| 24 | + sp.GetRequiredService<IServiceScopeFactory>(), |
| 25 | + NullLogger<RequestTracePersistService>.Instance); |
| 26 | + |
| 27 | + Guid logId = Guid.CreateVersion7(); |
| 28 | + DateTime startedAt = DateTime.UtcNow; |
| 29 | + |
| 30 | + await service.PersistSingleAsync(new RequestTraceRequestHeaderWriteModel |
| 31 | + { |
| 32 | + LogId = logId, |
| 33 | + StartedAt = startedAt, |
| 34 | + Direction = RequestTraceDirection.Inbound, |
| 35 | + Source = "127.0.0.1", |
| 36 | + UserId = 1, |
| 37 | + TraceId = "trace-1", |
| 38 | + Method = "GET", |
| 39 | + Url = "/v1/test", |
| 40 | + RequestContentType = "application/json", |
| 41 | + RequestHeaders = "x-a: 1" |
| 42 | + }, CancellationToken.None); |
| 43 | + |
| 44 | + await service.PersistSingleAsync(new RequestTraceRequestBodyWriteModel |
| 45 | + { |
| 46 | + LogId = logId, |
| 47 | + StartedAt = startedAt.AddMinutes(1), |
| 48 | + Direction = RequestTraceDirection.Outbound, |
| 49 | + Source = "different-source", |
| 50 | + UserId = 2, |
| 51 | + TraceId = "trace-2", |
| 52 | + Method = "POST", |
| 53 | + Url = "/mismatch", |
| 54 | + RequestBodyAt = startedAt.AddSeconds(1), |
| 55 | + RequestContentType = "text/plain", |
| 56 | + RawRequestBodyBytes = 11, |
| 57 | + IsRequestBodyTruncated = true, |
| 58 | + RequestBody = "hello world", |
| 59 | + RequestBodyRaw = [1, 2, 3] |
| 60 | + }, CancellationToken.None); |
| 61 | + |
| 62 | + await service.PersistSingleAsync(new RequestTraceResponseHeaderWriteModel |
| 63 | + { |
| 64 | + LogId = logId, |
| 65 | + StartedAt = startedAt.AddMinutes(2), |
| 66 | + Direction = RequestTraceDirection.Outbound, |
| 67 | + Source = "different-source-2", |
| 68 | + UserId = 3, |
| 69 | + TraceId = "trace-3", |
| 70 | + Method = "PATCH", |
| 71 | + Url = "/mismatch-2", |
| 72 | + ResponseHeaderAt = startedAt.AddSeconds(2), |
| 73 | + ResponseContentType = "application/json", |
| 74 | + StatusCode = 200, |
| 75 | + ResponseHeaders = "x-b: 2" |
| 76 | + }, CancellationToken.None); |
| 77 | + |
| 78 | + await service.PersistSingleAsync(new RequestTraceResponseBodyWriteModel |
| 79 | + { |
| 80 | + LogId = logId, |
| 81 | + StartedAt = startedAt.AddMinutes(3), |
| 82 | + Direction = RequestTraceDirection.Outbound, |
| 83 | + Source = "different-source-3", |
| 84 | + UserId = 4, |
| 85 | + TraceId = "trace-4", |
| 86 | + Method = "DELETE", |
| 87 | + Url = "/mismatch-3", |
| 88 | + ResponseBodyAt = startedAt.AddSeconds(3), |
| 89 | + ResponseContentType = "application/json", |
| 90 | + StatusCode = 201, |
| 91 | + RawResponseBodyBytes = 7, |
| 92 | + IsResponseBodyTruncated = true, |
| 93 | + ResponseBody = "resp", |
| 94 | + ResponseBodyRaw = [4, 5, 6] |
| 95 | + }, CancellationToken.None); |
| 96 | + |
| 97 | + await service.PersistSingleAsync(new RequestTraceExceptionWriteModel |
| 98 | + { |
| 99 | + LogId = logId, |
| 100 | + StartedAt = startedAt.AddMinutes(4), |
| 101 | + Direction = RequestTraceDirection.Outbound, |
| 102 | + Source = "different-source-4", |
| 103 | + UserId = 5, |
| 104 | + TraceId = "trace-5", |
| 105 | + Method = "PUT", |
| 106 | + Url = "/mismatch-4", |
| 107 | + ExceptionAt = startedAt.AddSeconds(4), |
| 108 | + ResponseContentType = "application/problem+json", |
| 109 | + StatusCode = 500, |
| 110 | + ErrorType = "TestException", |
| 111 | + ErrorMessage = "boom" |
| 112 | + }, CancellationToken.None); |
| 113 | + |
| 114 | + using IServiceScope scope = sp.CreateScope(); |
| 115 | + ChatsDB db = scope.ServiceProvider.GetRequiredService<ChatsDB>(); |
| 116 | + RequestTrace trace = await db.RequestTraces.Include(x => x.RequestTracePayload).SingleAsync(x => x.Id == logId); |
| 117 | + |
| 118 | + Assert.Equal("GET", trace.Method); |
| 119 | + Assert.Equal("/v1/test", trace.Url); |
| 120 | + Assert.Equal("text/plain", trace.RequestContentType); |
| 121 | + Assert.Equal(11, trace.RawRequestBodyBytes); |
| 122 | + Assert.True(trace.IsRequestBodyTruncated); |
| 123 | + |
| 124 | + Assert.Equal("application/json", trace.ResponseContentType); |
| 125 | + Assert.Equal((short)201, trace.StatusCode); |
| 126 | + Assert.Equal(7, trace.RawResponseBodyBytes); |
| 127 | + Assert.True(trace.IsResponseBodyTruncated); |
| 128 | + |
| 129 | + Assert.Equal("TestException", trace.ErrorType); |
| 130 | + Assert.Equal("boom", trace.ErrorMessage); |
| 131 | + |
| 132 | + Assert.NotNull(trace.RequestTracePayload); |
| 133 | + Assert.Equal("x-a: 1", trace.RequestTracePayload!.RequestHeaders); |
| 134 | + Assert.Equal("x-b: 2", trace.RequestTracePayload.ResponseHeaders); |
| 135 | + Assert.Equal("hello world", trace.RequestTracePayload.RequestBody); |
| 136 | + Assert.Equal("resp", trace.RequestTracePayload.ResponseBody); |
| 137 | + Assert.Equal(new byte[] { 1, 2, 3 }, trace.RequestTracePayload.RequestBodyRaw); |
| 138 | + Assert.Equal(new byte[] { 4, 5, 6 }, trace.RequestTracePayload.ResponseBodyRaw); |
| 139 | + } |
| 140 | +} |
0 commit comments