public async Task StatusCode400LogWarning()
        {
            var highestSeverityLevel = LogSeverityLevel.None;
            var mockLogger           = new Mock <ISyncLogger>();

            mockLogger.Setup(logger =>
                             logger.LogSync(
                                 It.IsAny <LogRecord>()))
            .Callback((LogRecord lr) =>
            {
                if (lr.SeverityLevel > highestSeverityLevel)
                {
                    highestSeverityLevel = lr.SeverityLevel;
                }
            })
            .Verifiable();
            FulcrumApplication.Setup.SynchronousFastLogger = mockLogger.Object;
            const string url          = "https://v-mock.org/v2/smoke-testing-company/ver";
            var          innerHandler = new ReturnResponseWithPresetStatusCode(async ctx => await Task.CompletedTask, 400);
            var          options      = new NexusLinkMiddlewareOptions();

            options.Features.LogRequestAndResponse.Enabled = true;
            var outerHandler = new Pipe.NexusLinkMiddleware(innerHandler.InvokeAsync, options);
            var context      = new DefaultHttpContext();

            context.SetRequest(url);

            await outerHandler.InvokeAsync(context);

            Assert.AreEqual(LogSeverityLevel.Warning, highestSeverityLevel);
        }
예제 #2
0
        public async Task BatchLogs()
        {
            _logCounter = 0;
            var mockLogger = new Mock <ISyncLogger>();

            mockLogger.Setup(logger =>
                             logger.LogSync(
                                 It.IsAny <LogRecord>()))
            .Callback((LogRecord lr) =>
            {
                Assert.IsTrue(FulcrumApplication.Context.IsInBatchLogger);
                Interlocked.Increment(ref _logCounter);
            })
            .Verifiable();
            FulcrumApplication.Setup.SynchronousFastLogger     = new BatchLogger(mockLogger.Object);
            FulcrumApplication.Setup.LogSeverityLevelThreshold = LogSeverityLevel.Information;

            var doLogging = new LogFiveTimesHandler(async c => await Task.CompletedTask);
            var options   = new NexusLinkMiddlewareOptions();

            options.Features.BatchLog.Enabled   = true;
            options.Features.BatchLog.Threshold = LogSeverityLevel.Warning;
            var handler = new Pipe.NexusLinkMiddleware(doLogging.InvokeAsync, options);
            var context = new DefaultHttpContext();

            Assert.IsFalse(FulcrumApplication.Context.IsInBatchLogger);
            await handler.InvokeAsync(context);

            Assert.IsFalse(FulcrumApplication.Context.IsInBatchLogger);
            mockLogger.Verify();
        }
        public async Task SaveClientTenantOldPrefixAcceptsFalsePositives()
        {
            var options = new NexusLinkMiddlewareOptions();

            options.Features.SaveClientTenant.Enabled = true;
            options.Features.SaveClientTenant.RegexForFindingTenantInUrl = SaveClientTenantOptions.LegacyVersion;
            var handler = new Pipe.NexusLinkMiddleware(ctx =>
            {
                _foundClientTenant = FulcrumApplication.Context.ClientTenant;
                return(Task.CompletedTask);
            }, options);

            _foundClientTenant = null;
            var url     = "https://ver-fulcrum-fundamentals.azurewebsites.net/api/v1/false/positive/ServiceMetas/ServiceHealth";
            var context = new DefaultHttpContext();

            context.SetRequest(url);
            await handler.InvokeAsync(context);

            Assert.IsNotNull(_foundClientTenant);
        }
        public async Task SavedConfigurationHandlesNoCorrelationId()
        {
            // Setup a mocked logger as the FullLogger so that we have full control
            var mockLogger = new Mock <ISyncLogger>();

            FulcrumApplication.Setup.SynchronousFastLogger = mockLogger.Object;
            mockLogger.Setup(x => x.LogSync(It.IsAny <LogRecord>())).Verifiable();

            // The expected correlation id propagated as a request header
            const string corrId = "CorrelationId";

            var leverConfig = new Mock <ILeverServiceConfiguration>();
            var options     = new NexusLinkMiddlewareOptions();

            options.Features.SaveClientTenant.Enabled = true;
            options.Features.SaveClientTenant.RegexForFindingTenantInUrl  = SaveClientTenantOptions.LegacyVersion;
            options.Features.SaveTenantConfiguration.Enabled              = true;
            options.Features.SaveTenantConfiguration.ServiceConfiguration = leverConfig.Object;
            options.Features.SaveCorrelationId.Enabled = true;

            var handler = new Pipe.NexusLinkMiddleware(ctx =>
            {
                _foundCorrelationId = FulcrumApplication.Context.CorrelationId;
                return(Task.CompletedTask);
            }, options);
            var url = "https://v-mock.org/v2/smoke-testing-company/ver/";

            // Simulate an incoming request
            var context = new DefaultHttpContext();

            context.SetRequest(url);
            context.Request.Headers.Add("X-Correlation-ID", corrId);
            await handler.InvokeAsync(context);

            // Check that LogAsync has NOT been invoked
            mockLogger.VerifyNoOtherCalls();

            Assert.AreEqual(corrId, _foundCorrelationId,
                            "When SaveConfiguration is run before SaveCorrelationId, we still expect X-Correlation-ID header to be handled");
        }
        public async Task SaveClientTenantNewPrefixSuccess()
        {
            var options = new NexusLinkMiddlewareOptions();

            options.Features.SaveClientTenant.Enabled = true;
            options.Features.SaveClientTenant.RegexForFindingTenantInUrl = SaveClientTenantOptions.ApiVersionTenant;
            var handler = new Pipe.NexusLinkMiddleware(context =>
            {
                _foundClientTenant = FulcrumApplication.Context.ClientTenant;
                return(Task.CompletedTask);
            }, options);

            foreach (var entry in new Dictionary <Tenant, string>
            {
                { new Tenant("smoke-testing-company", "ver"), "https://v-mock.org/api/v2/Tenant/smoke-testing-company/ver/" },
                {
                    new Tenant("smoke-testing-company", "local"),
                    "https://v-mock.org/api/v-pa1/Tenant/smoke-testing-company/local/"
                },
                {
                    new Tenant("fulcrum", "prd"),
                    "https://prd-fulcrum-fundamentals.azurewebsites.net/api/v1/Tenant/fulcrum/prd/ServiceMetas/ServiceHealth"
                },
                {
                    new Tenant("fulcrum", "ver"),
                    "https://ver-fulcrum-fundamentals.azurewebsites.net/api/v1/Tenant/fulcrum/ver/ServiceMetas/ServiceHealth"
                }
            })
            {
                _foundClientTenant = null;
                var expectedTenant = entry.Key;
                var context        = new DefaultHttpContext();
                context.SetRequest(entry.Value);
                await handler.InvokeAsync(context);

                Assert.AreEqual(expectedTenant, _foundClientTenant,
                                $"Could not find tenant '{expectedTenant}' from url '{entry.Value}'. Found {_foundClientTenant}");
            }
        }
        public async Task SaveConfigurationFail()
        {
            var options = new NexusLinkMiddlewareOptions();

            options.Features.SaveClientTenant.Enabled = true;
            options.Features.SaveClientTenant.RegexForFindingTenantInUrl = SaveClientTenantOptions.LegacyVersion;
            var handler = new Pipe.NexusLinkMiddleware(ctx => Task.CompletedTask, options);

            FulcrumApplication.Context.ClientTenant = null;
            foreach (var url in new[]
            {
                "http://gooogle.com/",
                "https://anywhere.org/api/v1/eels/"
            })
            {
                var context = new DefaultHttpContext();
                context.SetRequest(url);
                await handler.InvokeAsync(context);

                Assert.IsNull(FulcrumApplication.Context.ClientTenant);
            }
        }