Beispiel #1
0
 protected internal void BeforeExecuteNextMiddleware(ref TrafficCometMiddlewaresAccessor trafficCometAccessor,
                                                     string clientid, string traceId)
 {
     trafficCometAccessor.ClientId       = clientid;
     trafficCometAccessor.TraceId        = traceId;
     trafficCometAccessor.StartDateUtc   = DateTime.UtcNow;
     trafficCometAccessor.StartDateLocal = DateTime.Now.ToLocalTime();
 }
Beispiel #2
0
        public async Task Invoke(HttpContext httpContext, ITrafficCometMiddlewaresAccessor trafficCometAccessor,
                                 IDynamicBodyReader dynamicBodyReader, IOptionsSnapshot <RequestReadMiddlewareConfig> config)
        {
            if (trafficCometAccessor == null)
            {
                throw new ArgumentNullException(nameof(trafficCometAccessor));
            }

            if (dynamicBodyReader == null)
            {
                throw new ArgumentNullException(nameof(dynamicBodyReader));
            }

            TrafficCometMiddlewaresAccessor internalTrafficCometAccessor = (TrafficCometMiddlewaresAccessor)trafficCometAccessor;

            internalTrafficCometAccessor.IgnoreRequest = !internalTrafficCometAccessor.IgnoreWholeRequest &&
                                                         config.IgnoreRequest(httpContext.Request.Path);

            if (!internalTrafficCometAccessor.IgnoreRequest)
            {
                MemoryStream originalRequestBody = new MemoryStream();
                await httpContext.Request.Body.CopyToAsync(originalRequestBody);

                originalRequestBody.Position = 0L;

                try
                {
                    httpContext.Request.EnableRewind();
                    using (var buffer = new MemoryStream())
                    {
                        await originalRequestBody.CopyToAsync(buffer);

                        originalRequestBody.Position = 0L;

                        var bodyCompressed = httpContext.RequestBodyIsCompressed(out string compressionType);
                        internalTrafficCometAccessor.RequestBody = dynamicBodyReader
                                                                   .ReadBody(buffer, bodyCompressed, compressionType);
                    }
                }
                catch (Exception ex)
                {
                    if (Logger != null && Logger.IsEnabled(LogLevel.Error))
                    {
                        Logger.LogError(new EventId(90, $"TrafficComet.{nameof(RequestReadMiddleware)}"), ex, ex.ToString());
                    }
                }
                finally
                {
                    httpContext.Request.Body = originalRequestBody;
                }
                await Next(httpContext);
            }
            else
            {
                await Next(httpContext);
            }
        }
Beispiel #3
0
        public async Task ThrowException_RollbackOryginalRequestBody()
        {
            var accessor = new TrafficCometMiddlewaresAccessor();
            var requestReadMiddleware = RequestReadMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext);
            await requestReadMiddleware.Invoke(httpContext, accessor, IStringBodyReaderMockFactory.CreateMockObjectWithException());

            Assert.IsNull(accessor.RequestBody);
            Assert.NotNull(httpContext.Request.Body);
            Assert.AreEqual(httpContext.Request.Body.Position, 0L);
        }
Beispiel #4
0
        public async Task RequestBodyReadSuccessfully()
        {
            var accessor   = new TrafficCometMiddlewaresAccessor();
            var bodyReader = IStringBodyReaderMockFactory.CreateMockObject(MockStaticData.RequestBody);

            var requestReadMiddleware = RequestReadMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext);
            await requestReadMiddleware.Invoke(httpContext, accessor, bodyReader);

            Assert.AreEqual(accessor.RequestBody, MockStaticData.RequestBody);
            Assert.NotNull(httpContext.Request.Body);
            Assert.AreEqual(httpContext.Request.Body.Position, 0L);
        }
        public void ThrowException_NullClientIdGenerator()
        {
            var accessor  = new TrafficCometMiddlewaresAccessor();
            var logWriter = ITrafficLogWriterMockFactory.CreateMockObject();

            var requestReadMiddleware = TrafficCometMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext);
            var trafficLogFactory     = ITrafficLogFactoryMockFactory.CreateMockObject(MockStaticData.MockLog);
            var traceIdGenerator      = ITraceIdGeneratorMockFactory.CreateObjectMock(MockStaticData.TraceId);

            Assert.ThrowsAsync <ArgumentNullException>(() =>
                                                       requestReadMiddleware.Invoke(httpContext, accessor,
                                                                                    logWriter, trafficLogFactory, traceIdGenerator, null));
        }
        internal async Task RunMiddlewareWithConfiguration(TrafficCometMiddlewareConfig config, string requestPath = null)
        {
            var accessor  = new TrafficCometMiddlewaresAccessor();
            var logWriter = ITrafficLogWriterMockFactory.CreateMockObject();

            var trafficCometMiddleware = TrafficCometMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext, config, requestPath);
            var trafficLogFactory      = ITrafficLogFactoryMockFactory.CreateMockObject(MockStaticData.MockLog);
            var traceIdGenerator       = ITraceIdGeneratorMockFactory.CreateObjectMock(MockStaticData.TraceId);
            var clientIdGenerator      = IClientUniqueIdGeneratorMockFactory.CreateMockObject(MockStaticData.UserUniqueId);

            await trafficCometMiddleware.Invoke(httpContext, accessor, logWriter, trafficLogFactory, traceIdGenerator, clientIdGenerator);

            Assert.True(accessor.IgnoreWholeRequest);
        }
Beispiel #7
0
        internal async Task RunMiddlewareWithConfiguration(RequestReadMiddlewareConfig config)
        {
            var accessor   = new TrafficCometMiddlewaresAccessor();
            var bodyReader = IStringBodyReaderMockFactory.CreateMockObject(MockStaticData.RequestBody);

            var requestReadMiddleware = RequestReadMiddlewareMockFactory.CreateMockObject(
                out HttpContext httpContext, config);

            await requestReadMiddleware.Invoke(httpContext, accessor, bodyReader);

            Assert.IsNull(accessor.RequestBody);
            Assert.NotNull(httpContext.Request.Body);
            Assert.AreEqual(httpContext.Request.Body.Position, 0L);
        }
Beispiel #8
0
        public async Task StopLoggingFileRequests()
        {
            var accessor   = new TrafficCometMiddlewaresAccessor();
            var bodyReader = IStringBodyReaderMockFactory.CreateMockObject(MockStaticData.RequestBody);

            var middlewareConfig = new RequestReadMiddlewareConfig
            {
                StartLoggingFileRequest = false
            };

            var requestReadMiddleware = RequestReadMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext,
                                                                                          middlewareConfig, "/bootstrap.css");

            await requestReadMiddleware.Invoke(httpContext, accessor, bodyReader);

            Assert.IsNull(accessor.RequestBody);
            Assert.NotNull(httpContext.Request.Body);
            Assert.AreEqual(httpContext.Request.Body.Position, 0L);
        }
        public async Task SuccessfullyRun()
        {
            var accessor  = new TrafficCometMiddlewaresAccessor();
            var logWriter = ITrafficLogWriterMockFactory.CreateMockObject();

            var trafficCometMiddleware = TrafficCometMiddlewareMockFactory.CreateMockObject(out HttpContext httpContext);
            var trafficLogFactory      = ITrafficLogFactoryMockFactory.CreateMockObject(MockStaticData.MockLog);
            var traceIdGenerator       = ITraceIdGeneratorMockFactory.CreateObjectMock(MockStaticData.TraceId);
            var clientIdGenerator      = IClientUniqueIdGeneratorMockFactory.CreateMockObject(MockStaticData.UserUniqueId);

            await trafficCometMiddleware.Invoke(httpContext, accessor, logWriter, trafficLogFactory, traceIdGenerator, clientIdGenerator);

            Assert.Null(accessor.ResponseBody);
            Assert.Null(accessor.RequestBody);
            Assert.False(accessor.IgnoreWholeRequest);
            Assert.AreEqual(accessor.TraceId, MockStaticData.TraceId);
            Assert.AreEqual(accessor.ClientUniqueId, MockStaticData.UserUniqueId);
            Assert.NotNull(httpContext.Response.Body);
            Assert.AreEqual(httpContext.Response.Body.Position, 0L);
            Assert.NotNull(httpContext.Request.Body);
            Assert.AreEqual(httpContext.Request.Body.Position, 0L);
        }
Beispiel #10
0
        public async Task Invoke(HttpContext httpContext, ITrafficCometMiddlewaresAccessor trafficCometAccessor,
                                 ITrafficLogWriter logWriter, ITrafficLogFactory logFactory, ITraceIdGenerator traceIdGenerator,
                                 IClientIdGenerator clientUniqueIdGenerator, IOptionsSnapshot <TrafficCometMiddlewareConfig> config)
        {
            if (trafficCometAccessor == null)
            {
                throw new ArgumentNullException(nameof(trafficCometAccessor));
            }

            if (logWriter == null)
            {
                throw new ArgumentNullException(nameof(logWriter));
            }

            if (logFactory == null)
            {
                throw new ArgumentNullException(nameof(logFactory));
            }

            if (traceIdGenerator == null)
            {
                throw new ArgumentNullException(nameof(traceIdGenerator));
            }

            if (clientUniqueIdGenerator == null)
            {
                throw new ArgumentNullException(nameof(clientUniqueIdGenerator));
            }

            TrafficCometMiddlewaresAccessor internalTrafficCometAccessor = (TrafficCometMiddlewaresAccessor)trafficCometAccessor;

            internalTrafficCometAccessor.InitContextValues();
            internalTrafficCometAccessor.ApplicationId      = config.Value.ApplicationId;
            internalTrafficCometAccessor.IgnoreWholeRequest = config.IgnoreRequest(httpContext.Request.Path);

            var clientIdReaded = clientUniqueIdGenerator.TryGenerateClientId(out string clientId);
            var traceIdReaded  = traceIdGenerator.TryGenerateTraceId(out string traceId);

            if (!internalTrafficCometAccessor.IgnoreWholeRequest && clientIdReaded && traceIdReaded)
            {
                BeforeExecuteNextMiddleware(ref internalTrafficCometAccessor, clientId, traceId);
                await Next(httpContext);

                try
                {
                    AfterExecutedNextMiddleware(ref internalTrafficCometAccessor);
                    var ignoreRequest  = internalTrafficCometAccessor.IgnoreRequest;
                    var ignoreResponse = internalTrafficCometAccessor.IgnoreResponse;

                    var log = logFactory.Create();
                    if (log != null)
                    {
                        _ = logWriter.SaveLog(log);
                    }
                }
                catch (Exception ex)
                {
                    if (Logger != null && Logger.IsEnabled(LogLevel.Error))
                    {
                        Logger.LogError(new EventId(92, $"TrafficComet.{nameof(TrafficCometMiddleware)}"), ex, ex.ToString());
                    }
                }
            }
            else
            {
                await Next(httpContext);
            }
        }
Beispiel #11
0
 protected internal void AfterExecutedNextMiddleware(ref TrafficCometMiddlewaresAccessor trafficCometAccessor)
 {
     trafficCometAccessor.EndDateUtc   = DateTime.UtcNow;
     trafficCometAccessor.EndDateLocal = DateTime.Now.ToLocalTime();
 }