mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 23:30:50 +08:00 
			
		
		
		
	Feature/another look at logging (#303)
* #212 - hacked websockets proxy together * faffing around * #212 hacking away :( * #212 websockets proxy middleware working * #212 map when for webockets working * #212 some test refactor * #212 temp commit * #212 websockets proxy working, tests passing...need to do some tidying and write docs * #212 more code coverage * #212 docs for websockets * #212 updated readme * #212 tidying up after websockets refactoring * #212 tidying up after websockets refactoring * #212 tidying up after websockets refactoring * changing logging levels and logging like ms reccomends with structured data rather than strings * more faffing * more fafin * #287 ocelot logger now only takes strings as it did take params then just turned them to strings, misleading, unit tests for logger and diagnosticlogger * #287 errors now logged as they happen * #287 more detail for logs requested in issue * #287 tidy up * #287 renamed * #287 always log context id * #287 fixed me being an idiot * #287 removed crap websockets unit test that isnt a unit test * #287 removed crap websockets unit test that isnt a unit test
This commit is contained in:
		@@ -72,7 +72,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        private void ThenTheLoggerIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _logger
 | 
			
		||||
                .Verify(x => x.LogDebug(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
 | 
			
		||||
                .Verify(x => x.LogDebug(It.IsAny<string>()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenClaimsToThingsAreReturned()
 | 
			
		||||
 
 | 
			
		||||
@@ -124,7 +124,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoggerIsCalledCorrectly(string message)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(x => x.LogError(message), Times.Once);
 | 
			
		||||
            _logger.Verify(x => x.LogWarning(message), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,3 @@
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Errors
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
@@ -15,18 +13,18 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Errors;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder.Middleware;
 | 
			
		||||
    using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
    public class ExceptionHandlerMiddlewareTests
 | 
			
		||||
    {
 | 
			
		||||
        bool _shouldThrowAnException = false;
 | 
			
		||||
        private Mock<IOcelotConfigurationProvider> _provider;
 | 
			
		||||
        private Mock<IRequestScopedDataRepository> _repo;
 | 
			
		||||
        bool _shouldThrowAnException;
 | 
			
		||||
        private readonly Mock<IOcelotConfigurationProvider> _provider;
 | 
			
		||||
        private readonly Mock<IRequestScopedDataRepository> _repo;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _loggerFactory;
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private ExceptionHandlerMiddleware _middleware;
 | 
			
		||||
        private DownstreamContext _downstreamContext;
 | 
			
		||||
        private readonly ExceptionHandlerMiddleware _middleware;
 | 
			
		||||
        private readonly DownstreamContext _downstreamContext;
 | 
			
		||||
        private OcelotRequestDelegate _next;
 | 
			
		||||
 | 
			
		||||
        public ExceptionHandlerMiddlewareTests()
 | 
			
		||||
@@ -59,7 +57,7 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
                .And(_ => GivenTheConfigurationIs(config))
 | 
			
		||||
                .When(_ => WhenICallTheMiddleware())
 | 
			
		||||
                .Then(_ => ThenTheResponseIsOk())
 | 
			
		||||
                .And(_ => TheRequestIdIsNotSet())
 | 
			
		||||
                .And(_ => TheAspDotnetRequestIdIsSet())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -89,7 +87,7 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void ShouldNotSetRequestId()
 | 
			
		||||
        public void ShouldSetAspDotNetRequestId()
 | 
			
		||||
        {
 | 
			
		||||
            var config = new OcelotConfiguration(null, null, null, null);
 | 
			
		||||
 | 
			
		||||
@@ -97,7 +95,7 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
                .And(_ => GivenTheConfigurationIs(config))
 | 
			
		||||
                .When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
 | 
			
		||||
                .Then(_ => ThenTheResponseIsOk())
 | 
			
		||||
                .And(_ => TheRequestIdIsNotSet())
 | 
			
		||||
                .And(_ => TheAspDotnetRequestIdIsSet())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -146,29 +144,19 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
 | 
			
		||||
        private void GivenTheConfigReturnsError()
 | 
			
		||||
        {
 | 
			
		||||
            var config = new OcelotConfiguration(null, null, null, null);
 | 
			
		||||
 | 
			
		||||
            var response = new Ocelot.Responses.ErrorResponse<IOcelotConfiguration>(new FakeError());
 | 
			
		||||
            var response = new Responses.ErrorResponse<IOcelotConfiguration>(new FakeError());
 | 
			
		||||
            _provider
 | 
			
		||||
                .Setup(x => x.Get()).ReturnsAsync(response);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public class FakeError : Error
 | 
			
		||||
        {
 | 
			
		||||
            public FakeError() 
 | 
			
		||||
                : base("meh", OcelotErrorCode.CannotAddDataError)
 | 
			
		||||
            {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void TheRequestIdIsSet(string key, string value)
 | 
			
		||||
        {
 | 
			
		||||
            _repo.Verify(x => x.Add<string>(key, value), Times.Once);
 | 
			
		||||
            _repo.Verify(x => x.Add(key, value), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheConfigurationIs(IOcelotConfiguration config)
 | 
			
		||||
        {
 | 
			
		||||
            var response = new Ocelot.Responses.OkResponse<IOcelotConfiguration>(config);
 | 
			
		||||
            var response = new Responses.OkResponse<IOcelotConfiguration>(config);
 | 
			
		||||
            _provider
 | 
			
		||||
                .Setup(x => x.Get()).ReturnsAsync(response);
 | 
			
		||||
        }
 | 
			
		||||
@@ -193,9 +181,17 @@ namespace Ocelot.UnitTests.Errors
 | 
			
		||||
            _downstreamContext.HttpContext.Response.StatusCode.ShouldBe(500);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void TheRequestIdIsNotSet()
 | 
			
		||||
        private void TheAspDotnetRequestIdIsSet()
 | 
			
		||||
        {
 | 
			
		||||
            _repo.Verify(x => x.Add<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
 | 
			
		||||
            _repo.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        class FakeError : Error
 | 
			
		||||
        {
 | 
			
		||||
            internal FakeError()
 | 
			
		||||
                : base("meh", OcelotErrorCode.CannotAddDataError)
 | 
			
		||||
            {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -106,7 +106,7 @@ namespace Ocelot.UnitTests.Headers
 | 
			
		||||
 | 
			
		||||
        private void ThenTheErrorIsLogged()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(x => x.LogError("Unable to add header to response Trace-Id: {TraceId}"), Times.Once);
 | 
			
		||||
            _logger.Verify(x => x.LogWarning("Unable to add header to response Trace-Id: {TraceId}"), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheHeaderIsNotAdded(string key)
 | 
			
		||||
@@ -145,4 +145,4 @@ namespace Ocelot.UnitTests.Headers
 | 
			
		||||
            _addHeaders = addHeaders;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										81
									
								
								test/Ocelot.UnitTests/Logging/AspDotNetLoggerTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								test/Ocelot.UnitTests/Logging/AspDotNetLoggerTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,81 @@
 | 
			
		||||
namespace Ocelot.UnitTests.Logging
 | 
			
		||||
{
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Microsoft.Extensions.Logging;
 | 
			
		||||
    using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
    using System;
 | 
			
		||||
 | 
			
		||||
    public class AspDotNetLoggerTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<ILogger<object>> _coreLogger;
 | 
			
		||||
        private readonly AspDotNetLogger _logger; 
 | 
			
		||||
        private Mock<IRequestScopedDataRepository> _repo;
 | 
			
		||||
        private readonly string _b;
 | 
			
		||||
        private readonly string _a;
 | 
			
		||||
        private readonly Exception _ex;
 | 
			
		||||
 | 
			
		||||
        public AspDotNetLoggerTests()
 | 
			
		||||
        {
 | 
			
		||||
            _a = "tom";
 | 
			
		||||
            _b = "laura";
 | 
			
		||||
            _ex = new Exception("oh no");
 | 
			
		||||
            _coreLogger = new Mock<ILogger<object>>();
 | 
			
		||||
            _repo = new Mock<IRequestScopedDataRepository>();
 | 
			
		||||
            _logger = new AspDotNetLogger(_coreLogger.Object, _repo.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_trace()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogTrace($"a message from {_a} to {_b}");
 | 
			
		||||
 | 
			
		||||
            ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Trace);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_info()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogInformation($"a message from {_a} to {_b}");
 | 
			
		||||
 | 
			
		||||
            ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Information);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_warning()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogWarning($"a message from {_a} to {_b}");
 | 
			
		||||
 | 
			
		||||
            ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Warning);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_error()
 | 
			
		||||
        {
 | 
			
		||||
            
 | 
			
		||||
            _logger.LogError($"a message from {_a} to {_b}", _ex);
 | 
			
		||||
 | 
			
		||||
            ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura, exception: System.Exception: oh no", LogLevel.Error);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_critical()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogCritical($"a message from {_a} to {_b}", _ex);
 | 
			
		||||
 | 
			
		||||
            ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura, exception: System.Exception: oh no", LogLevel.Critical);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenLevelIsLogged(string expected, LogLevel expectedLogLevel)
 | 
			
		||||
        {
 | 
			
		||||
            _coreLogger.Verify(
 | 
			
		||||
                x => x.Log(
 | 
			
		||||
                    expectedLogLevel,
 | 
			
		||||
                    It.IsAny<EventId>(),
 | 
			
		||||
                    It.Is<object>(o => o.ToString() == expected), 
 | 
			
		||||
                    It.IsAny<Exception>(),
 | 
			
		||||
                    It.IsAny<Func<object, Exception, string>>()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										145
									
								
								test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,145 @@
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Moq;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Butterfly.Client.Tracing;
 | 
			
		||||
using Ocelot.Requester;
 | 
			
		||||
using Xunit;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Logging
 | 
			
		||||
{
 | 
			
		||||
    public class OcelotDiagnosticListenerTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly OcelotDiagnosticListener _listener;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private readonly Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private IServiceTracer _tracer;
 | 
			
		||||
        private DownstreamContext _downstreamContext;
 | 
			
		||||
        private string _name;
 | 
			
		||||
        private Exception _exception;
 | 
			
		||||
 | 
			
		||||
        public OcelotDiagnosticListenerTests()
 | 
			
		||||
        {
 | 
			
		||||
            _factory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _tracer = new FakeServiceTracer();
 | 
			
		||||
            _factory.Setup(x => x.CreateLogger<OcelotDiagnosticListener>()).Returns(_logger.Object);
 | 
			
		||||
            _listener = new OcelotDiagnosticListener(_factory.Object, _tracer);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_trace_ocelot_middleware_started()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .When(_ => WhenOcelotMiddlewareStartedCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"Ocelot.MiddlewareStarted: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_trace_ocelot_middleware_finished()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .When(_ => WhenOcelotMiddlewareFinishedCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"Ocelot.MiddlewareFinished: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_trace_ocelot_middleware_exception()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .And(_ => GivenAException(new Exception("oh no")))
 | 
			
		||||
                .When(_ => WhenOcelotMiddlewareExceptionCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"Ocelot.MiddlewareException: {_name}; {_exception.Message};"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
       [Fact]
 | 
			
		||||
        public void should_trace_middleware_started()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .When(_ => WhenMiddlewareStartedCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"MiddlewareStarting: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_trace_middleware_finished()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .When(_ => WhenMiddlewareFinishedCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"MiddlewareFinished: {_name}; {_downstreamContext.HttpContext.Response.StatusCode}"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_trace_middleware_exception()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenAMiddlewareName())
 | 
			
		||||
                .And(_ => GivenAContext())
 | 
			
		||||
                .And(_ => GivenAException(new Exception("oh no")))
 | 
			
		||||
                .When(_ => WhenMiddlewareExceptionCalled())
 | 
			
		||||
                .Then(_ => ThenTheLogIs($"MiddlewareException: {_name}; {_exception.Message};"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAException(Exception exception)
 | 
			
		||||
        {
 | 
			
		||||
            _exception = exception;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenOcelotMiddlewareStartedCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OcelotMiddlewareStarted(_downstreamContext, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenOcelotMiddlewareFinishedCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OcelotMiddlewareFinished(_downstreamContext, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenOcelotMiddlewareExceptionCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OcelotMiddlewareException(_exception, _downstreamContext, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenMiddlewareStartedCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OnMiddlewareStarting(_downstreamContext.HttpContext, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenMiddlewareFinishedCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OnMiddlewareFinished(_downstreamContext.HttpContext, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenMiddlewareExceptionCalled()
 | 
			
		||||
        {
 | 
			
		||||
            _listener.OnMiddlewareException(_exception, _name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAContext()
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamContext = new DownstreamContext(new DefaultHttpContext());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAMiddlewareName()
 | 
			
		||||
        {
 | 
			
		||||
            _name = "name";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLogIs(string expected)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(
 | 
			
		||||
                x => x.LogTrace(expected));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										75
									
								
								test/Ocelot.UnitTests/Middleware/OcelotMiddlewareTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								test/Ocelot.UnitTests/Middleware/OcelotMiddlewareTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,75 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.UnitTests.Responder;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class OcelotMiddlewareTests
 | 
			
		||||
    {
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private FakeMiddleware _middleware;
 | 
			
		||||
        private List<Error> _errors;
 | 
			
		||||
 | 
			
		||||
        public OcelotMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _errors = new List<Error>();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _middleware = new FakeMiddleware(_logger.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_error()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => GivenAnError(new AnyError()))
 | 
			
		||||
                .When(x => WhenISetTheError())
 | 
			
		||||
                .Then(x => ThenTheErrorIsLogged(1))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_errors()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => GivenAnError(new AnyError()))
 | 
			
		||||
                .And(x => GivenAnError(new AnyError()))
 | 
			
		||||
                .When(x => WhenISetTheErrors())
 | 
			
		||||
                .Then(x => ThenTheErrorIsLogged(2))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenISetTheErrors()
 | 
			
		||||
        {
 | 
			
		||||
            _middleware.SetPipelineError(new DownstreamContext(new DefaultHttpContext()), _errors);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheErrorIsLogged(int times)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(x => x.LogWarning("blahh"), Times.Exactly(times));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenISetTheError()
 | 
			
		||||
        {
 | 
			
		||||
            _middleware.SetPipelineError(new DownstreamContext(new DefaultHttpContext()), _errors[0]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAnError(Error error)
 | 
			
		||||
        {
 | 
			
		||||
            _errors.Add(error);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class FakeMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        public FakeMiddleware(IOcelotLogger logger) 
 | 
			
		||||
            : base(logger)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -57,4 +57,8 @@
 | 
			
		||||
    <PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <Folder Include="WebSockets\" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
 | 
			
		||||
</Project>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
{
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Microsoft.Extensions.Primitives;
 | 
			
		||||
    using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
@@ -21,6 +18,7 @@ namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using Ocelot.Request.Middleware;
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
    public class ReRouteRequestIdMiddlewareTests
 | 
			
		||||
    {
 | 
			
		||||
@@ -142,6 +140,30 @@ namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_update_if_global_request_id_is_same_as_re_route_request_id()
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
 | 
			
		||||
                new ReRouteBuilder()
 | 
			
		||||
                    .WithDownstreamReRoute(new DownstreamReRouteBuilder()
 | 
			
		||||
                        .WithDownstreamPathTemplate("any old string")
 | 
			
		||||
                        .WithRequestIdKey("LSRequestId")
 | 
			
		||||
                        .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                        .Build())
 | 
			
		||||
                    .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            var requestId = "alreadyset";
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
                .And(x => GivenTheRequestIdWasSetGlobally())
 | 
			
		||||
                .And(x => x.GivenTheRequestIdIsAddedToTheRequest("LSRequestId", requestId))
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheTraceIdIs(requestId))
 | 
			
		||||
                .And(x => ThenTheRequestIdIsNotUpdated())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenICallTheMiddleware()
 | 
			
		||||
        {
 | 
			
		||||
            _middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
 | 
			
		||||
@@ -159,12 +181,17 @@ namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
 | 
			
		||||
        private void ThenTheRequestIdIsSaved()
 | 
			
		||||
        {
 | 
			
		||||
            _repo.Verify(x => x.Add<string>("RequestId", _value), Times.Once);
 | 
			
		||||
            _repo.Verify(x => x.Add("RequestId", _value), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheRequestIdIsUpdated()
 | 
			
		||||
        {
 | 
			
		||||
            _repo.Verify(x => x.Update<string>("RequestId", _value), Times.Once);
 | 
			
		||||
            _repo.Verify(x => x.Update("RequestId", _value), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheRequestIdIsNotUpdated()
 | 
			
		||||
        {
 | 
			
		||||
            _repo.Verify(x => x.Update("RequestId", _value), Times.Never);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
 | 
			
		||||
@@ -182,15 +209,13 @@ namespace Ocelot.UnitTests.RequestId
 | 
			
		||||
 | 
			
		||||
        private void ThenTheTraceIdIsAnything()
 | 
			
		||||
        {
 | 
			
		||||
            StringValues value;
 | 
			
		||||
            _downstreamContext.HttpContext.Response.Headers.TryGetValue("LSRequestId", out value);
 | 
			
		||||
            _downstreamContext.HttpContext.Response.Headers.TryGetValue("LSRequestId", out var value);
 | 
			
		||||
            value.First().ShouldNotBeNullOrEmpty();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheTraceIdIs(string expected)
 | 
			
		||||
        {
 | 
			
		||||
            StringValues value;
 | 
			
		||||
            _downstreamContext.HttpContext.Response.Headers.TryGetValue("LSRequestId", out value);
 | 
			
		||||
            _downstreamContext.HttpContext.Response.Headers.TryGetValue("LSRequestId", out var value);
 | 
			
		||||
            value.First().ShouldBe(expected);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -51,7 +51,7 @@ namespace Ocelot.UnitTests.Responder
 | 
			
		||||
        public void should_return_any_errors()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenTheHttpResponseMessageIs(new HttpResponseMessage()))
 | 
			
		||||
                .And(x => x.GivenThereArePipelineErrors(new UnableToFindDownstreamRouteError()))
 | 
			
		||||
                .And(x => x.GivenThereArePipelineErrors(new UnableToFindDownstreamRouteError("/path", "GET")))
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenThereAreNoErrors())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
 
 | 
			
		||||
@@ -142,12 +142,12 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
        private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(
 | 
			
		||||
                x => x.LogError(
 | 
			
		||||
                x => x.LogWarning(
 | 
			
		||||
                    "Unable to use service Address: http://localhost and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
 | 
			
		||||
                Times.Once);
 | 
			
		||||
 | 
			
		||||
            _logger.Verify(
 | 
			
		||||
                x => x.LogError(
 | 
			
		||||
                x => x.LogWarning(
 | 
			
		||||
                    "Unable to use service Address: http://localhost and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
 | 
			
		||||
                Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
@@ -155,12 +155,12 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
        private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(
 | 
			
		||||
                x => x.LogError(
 | 
			
		||||
                x => x.LogWarning(
 | 
			
		||||
                    "Unable to use service Address: localhost and Port: -1 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
 | 
			
		||||
                Times.Once);
 | 
			
		||||
 | 
			
		||||
            _logger.Verify(
 | 
			
		||||
                x => x.LogError(
 | 
			
		||||
                x => x.LogWarning(
 | 
			
		||||
                    "Unable to use service Address: localhost and Port: 0 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
 | 
			
		||||
                Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user