mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 17:42:50 +08:00
removed fake service tracer (#511)
* removed fake service tracer * removed comments
This commit is contained in:
parent
a5bb74a2ea
commit
049731b43b
@ -1,24 +1,26 @@
|
|||||||
using Butterfly.Client.Tracing;
|
namespace Ocelot.Configuration.Creator
|
||||||
using Ocelot.Configuration.File;
|
{
|
||||||
using Ocelot.Requester;
|
using System;
|
||||||
|
using Butterfly.Client.Tracing;
|
||||||
namespace Ocelot.Configuration.Creator
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
{
|
using Ocelot.Configuration.File;
|
||||||
public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator
|
using Ocelot.Requester;
|
||||||
{
|
|
||||||
private readonly IServiceTracer _tracer;
|
public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator
|
||||||
|
{
|
||||||
public HttpHandlerOptionsCreator(IServiceTracer tracer)
|
private readonly IServiceTracer _tracer;
|
||||||
{
|
|
||||||
_tracer = tracer;
|
public HttpHandlerOptionsCreator(IServiceProvider services)
|
||||||
}
|
{
|
||||||
|
_tracer = services.GetService<IServiceTracer>();
|
||||||
public HttpHandlerOptions Create(FileHttpHandlerOptions options)
|
}
|
||||||
{
|
|
||||||
var useTracing = _tracer.GetType() != typeof(FakeServiceTracer) && options.UseTracing;
|
public HttpHandlerOptions Create(FileHttpHandlerOptions options)
|
||||||
|
{
|
||||||
return new HttpHandlerOptions(options.AllowAutoRedirect,
|
var useTracing = _tracer!= null && options.UseTracing;
|
||||||
options.UseCookieContainer, useTracing, options.UseProxy);
|
|
||||||
}
|
return new HttpHandlerOptions(options.AllowAutoRedirect,
|
||||||
}
|
options.UseCookieContainer, useTracing, options.UseProxy);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -158,8 +158,6 @@ namespace Ocelot.DependencyInjection
|
|||||||
_services.TryAddSingleton<IResponseAggregator, SimpleJsonResponseAggregator>();
|
_services.TryAddSingleton<IResponseAggregator, SimpleJsonResponseAggregator>();
|
||||||
_services.AddSingleton<ITracingHandlerFactory, TracingHandlerFactory>();
|
_services.AddSingleton<ITracingHandlerFactory, TracingHandlerFactory>();
|
||||||
|
|
||||||
// We add this here so that we can always inject something into the factory for IoC..
|
|
||||||
_services.AddSingleton<IServiceTracer, FakeServiceTracer>();
|
|
||||||
_services.TryAddSingleton<IFileConfigurationPollerOptions, InMemoryFileConfigurationPollerOptions>();
|
_services.TryAddSingleton<IFileConfigurationPollerOptions, InMemoryFileConfigurationPollerOptions>();
|
||||||
_services.TryAddSingleton<IAddHeadersToResponse, AddHeadersToResponse>();
|
_services.TryAddSingleton<IAddHeadersToResponse, AddHeadersToResponse>();
|
||||||
_services.TryAddSingleton<IPlaceholders, Placeholders>();
|
_services.TryAddSingleton<IPlaceholders, Placeholders>();
|
||||||
@ -237,8 +235,6 @@ namespace Ocelot.DependencyInjection
|
|||||||
|
|
||||||
public IOcelotBuilder AddOpenTracing(Action<ButterflyOptions> settings)
|
public IOcelotBuilder AddOpenTracing(Action<ButterflyOptions> settings)
|
||||||
{
|
{
|
||||||
// Earlier we add FakeServiceTracer and need to remove it here before we add butterfly
|
|
||||||
_services.RemoveAll<IServiceTracer>();
|
|
||||||
_services.AddButterfly(settings);
|
_services.AddButterfly(settings);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -1,96 +1,95 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DiagnosticAdapter;
|
using Microsoft.Extensions.DiagnosticAdapter;
|
||||||
using Butterfly.Client.AspNetCore;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Butterfly.OpenTracing;
|
using Butterfly.Client.AspNetCore;
|
||||||
using Ocelot.Middleware;
|
using Butterfly.OpenTracing;
|
||||||
using Butterfly.Client.Tracing;
|
using Ocelot.Middleware;
|
||||||
using System.Linq;
|
using Butterfly.Client.Tracing;
|
||||||
using System.Collections.Generic;
|
using System.Linq;
|
||||||
using Ocelot.Infrastructure.Extensions;
|
using System.Collections.Generic;
|
||||||
using Ocelot.Requester;
|
using Ocelot.Infrastructure.Extensions;
|
||||||
|
using Ocelot.Requester;
|
||||||
namespace Ocelot.Logging
|
|
||||||
{
|
namespace Ocelot.Logging
|
||||||
public class OcelotDiagnosticListener
|
{
|
||||||
{
|
public class OcelotDiagnosticListener
|
||||||
private readonly IServiceTracer _tracer;
|
{
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IServiceTracer _tracer;
|
||||||
|
private readonly IOcelotLogger _logger;
|
||||||
public OcelotDiagnosticListener(IOcelotLoggerFactory factory, IServiceTracer tracer)
|
|
||||||
{
|
public OcelotDiagnosticListener(IOcelotLoggerFactory factory, IServiceProvider services)
|
||||||
_tracer = tracer;
|
{
|
||||||
_logger = factory.CreateLogger<OcelotDiagnosticListener>();
|
_tracer = services.GetService<IServiceTracer>();
|
||||||
}
|
_logger = factory.CreateLogger<OcelotDiagnosticListener>();
|
||||||
|
}
|
||||||
[DiagnosticName("Ocelot.MiddlewareException")]
|
|
||||||
public virtual void OcelotMiddlewareException(Exception exception, DownstreamContext context, string name)
|
[DiagnosticName("Ocelot.MiddlewareException")]
|
||||||
{
|
public virtual void OcelotMiddlewareException(Exception exception, DownstreamContext context, string name)
|
||||||
_logger.LogTrace($"Ocelot.MiddlewareException: {name}; {exception.Message};");
|
{
|
||||||
Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
_logger.LogTrace($"Ocelot.MiddlewareException: {name}; {exception.Message};");
|
||||||
}
|
Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
||||||
|
}
|
||||||
[DiagnosticName("Ocelot.MiddlewareStarted")]
|
|
||||||
public virtual void OcelotMiddlewareStarted(DownstreamContext context, string name)
|
[DiagnosticName("Ocelot.MiddlewareStarted")]
|
||||||
{
|
public virtual void OcelotMiddlewareStarted(DownstreamContext context, string name)
|
||||||
_logger.LogTrace($"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
{
|
||||||
Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
_logger.LogTrace($"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
||||||
}
|
Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}");
|
||||||
|
}
|
||||||
[DiagnosticName("Ocelot.MiddlewareFinished")]
|
|
||||||
public virtual void OcelotMiddlewareFinished(DownstreamContext context, string name)
|
[DiagnosticName("Ocelot.MiddlewareFinished")]
|
||||||
{
|
public virtual void OcelotMiddlewareFinished(DownstreamContext context, string name)
|
||||||
_logger.LogTrace($"Ocelot.MiddlewareFinished: {name}; {context.HttpContext.Request.Path}");
|
{
|
||||||
Event(context.HttpContext, $"OcelotMiddlewareFinished: {name}; {context.HttpContext.Request.Path}");
|
_logger.LogTrace($"Ocelot.MiddlewareFinished: {name}; {context.HttpContext.Request.Path}");
|
||||||
}
|
Event(context.HttpContext, $"OcelotMiddlewareFinished: {name}; {context.HttpContext.Request.Path}");
|
||||||
|
}
|
||||||
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting")]
|
|
||||||
public virtual void OnMiddlewareStarting(HttpContext httpContext, string name)
|
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting")]
|
||||||
{
|
public virtual void OnMiddlewareStarting(HttpContext httpContext, string name)
|
||||||
_logger.LogTrace($"MiddlewareStarting: {name}; {httpContext.Request.Path}");
|
{
|
||||||
Event(httpContext, $"MiddlewareStarting: {name}; {httpContext.Request.Path}");
|
_logger.LogTrace($"MiddlewareStarting: {name}; {httpContext.Request.Path}");
|
||||||
}
|
Event(httpContext, $"MiddlewareStarting: {name}; {httpContext.Request.Path}");
|
||||||
|
}
|
||||||
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")]
|
|
||||||
public virtual void OnMiddlewareException(Exception exception, string name)
|
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")]
|
||||||
{
|
public virtual void OnMiddlewareException(Exception exception, string name)
|
||||||
_logger.LogTrace($"MiddlewareException: {name}; {exception.Message};");
|
{
|
||||||
}
|
_logger.LogTrace($"MiddlewareException: {name}; {exception.Message};");
|
||||||
|
}
|
||||||
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished")]
|
|
||||||
public virtual void OnMiddlewareFinished(HttpContext httpContext, string name)
|
[DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished")]
|
||||||
{
|
public virtual void OnMiddlewareFinished(HttpContext httpContext, string name)
|
||||||
_logger.LogTrace($"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}");
|
{
|
||||||
Event(httpContext, $"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}");
|
_logger.LogTrace($"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}");
|
||||||
}
|
Event(httpContext, $"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}");
|
||||||
|
}
|
||||||
private void Event(HttpContext httpContext, string @event)
|
|
||||||
{
|
private void Event(HttpContext httpContext, string @event)
|
||||||
// todo - if the user isnt using tracing the code gets here and will blow up on
|
{
|
||||||
// _tracer.Tracer.TryExtract. We already use the fake tracer for another scenario
|
// todo - if the user isnt using tracing the code gets here and will blow up on
|
||||||
// so sticking it here as well..I guess we need a factory for this but cba to do it at
|
// _tracer.Tracer.TryExtract..
|
||||||
// the moment
|
if(_tracer == null)
|
||||||
if(_tracer.GetType() == typeof(FakeServiceTracer))
|
{
|
||||||
{
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
var span = httpContext.GetSpan();
|
||||||
var span = httpContext.GetSpan();
|
|
||||||
|
if(span == null)
|
||||||
if(span == null)
|
{
|
||||||
{
|
var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");
|
||||||
var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}");
|
if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
|
||||||
if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(),
|
c => c.Select(x => new KeyValuePair<string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
|
||||||
c => c.Select(x => new KeyValuePair<string, string>(x.Key, x.Value.GetValue())).GetEnumerator()))
|
{
|
||||||
{
|
spanBuilder.AsChildOf(spanContext);
|
||||||
spanBuilder.AsChildOf(spanContext);
|
}
|
||||||
}
|
|
||||||
|
span = _tracer.Start(spanBuilder);
|
||||||
span = _tracer.Start(spanBuilder);
|
httpContext.SetSpan(span);
|
||||||
httpContext.SetSpan(span);
|
}
|
||||||
}
|
|
||||||
|
span?.Log(LogField.CreateNew().Event(@event));
|
||||||
span?.Log(LogField.CreateNew().Event(@event));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
using Butterfly.Client.Tracing;
|
|
||||||
using Butterfly.OpenTracing;
|
|
||||||
|
|
||||||
namespace Ocelot.Requester
|
|
||||||
{
|
|
||||||
public class FakeServiceTracer : IServiceTracer
|
|
||||||
{
|
|
||||||
public ITracer Tracer { get; }
|
|
||||||
public string ServiceName { get; }
|
|
||||||
public string Environment { get; }
|
|
||||||
public string Identity { get; }
|
|
||||||
public ISpan Start(ISpanBuilder spanBuilder)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +1,21 @@
|
|||||||
using Butterfly.Client.Tracing;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
|
|
||||||
namespace Ocelot.Requester
|
namespace Ocelot.Requester
|
||||||
{
|
{
|
||||||
|
using System;
|
||||||
|
using Butterfly.Client.Tracing;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
public class TracingHandlerFactory : ITracingHandlerFactory
|
public class TracingHandlerFactory : ITracingHandlerFactory
|
||||||
{
|
{
|
||||||
private readonly IServiceTracer _tracer;
|
private readonly IServiceTracer _tracer;
|
||||||
private readonly IRequestScopedDataRepository _repo;
|
private readonly IRequestScopedDataRepository _repo;
|
||||||
|
|
||||||
public TracingHandlerFactory(
|
public TracingHandlerFactory(
|
||||||
IServiceTracer tracer,
|
IServiceProvider services,
|
||||||
IRequestScopedDataRepository repo)
|
IRequestScopedDataRepository repo)
|
||||||
{
|
{
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
_tracer = tracer;
|
_tracer = services.GetService<IServiceTracer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITracingHandler Get()
|
public ITracingHandler Get()
|
||||||
|
@ -1,175 +1,180 @@
|
|||||||
using System;
|
using System;
|
||||||
using Butterfly.Client.Tracing;
|
using Butterfly.Client.Tracing;
|
||||||
using Butterfly.OpenTracing;
|
using Butterfly.OpenTracing;
|
||||||
using Ocelot.Configuration;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Ocelot.Configuration.Creator;
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.Creator;
|
||||||
using Ocelot.Requester;
|
using Ocelot.Configuration.File;
|
||||||
using Shouldly;
|
using Ocelot.Requester;
|
||||||
using TestStack.BDDfy;
|
using Shouldly;
|
||||||
using Xunit;
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
namespace Ocelot.UnitTests.Configuration
|
|
||||||
{
|
namespace Ocelot.UnitTests.Configuration
|
||||||
public class HttpHandlerOptionsCreatorTests
|
{
|
||||||
{
|
public class HttpHandlerOptionsCreatorTests
|
||||||
private IHttpHandlerOptionsCreator _httpHandlerOptionsCreator;
|
{
|
||||||
private FileReRoute _fileReRoute;
|
private IHttpHandlerOptionsCreator _httpHandlerOptionsCreator;
|
||||||
private HttpHandlerOptions _httpHandlerOptions;
|
private FileReRoute _fileReRoute;
|
||||||
private IServiceTracer _serviceTracer;
|
private HttpHandlerOptions _httpHandlerOptions;
|
||||||
|
private IServiceProvider _serviceProvider;
|
||||||
public HttpHandlerOptionsCreatorTests()
|
private IServiceCollection _serviceCollection;
|
||||||
{
|
|
||||||
_serviceTracer = new FakeServiceTracer();
|
public HttpHandlerOptionsCreatorTests()
|
||||||
_httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceTracer);
|
{
|
||||||
}
|
_serviceCollection = new ServiceCollection();
|
||||||
|
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
||||||
[Fact]
|
_httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceProvider);
|
||||||
public void should_not_use_tracing_if_fake_tracer_registered()
|
|
||||||
{
|
|
||||||
var fileReRoute = new FileReRoute
|
|
||||||
{
|
|
||||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
|
||||||
{
|
|
||||||
UseTracing = true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
|
||||||
|
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_tracing_if_real_tracer_registered()
|
|
||||||
{
|
|
||||||
var fileReRoute = new FileReRoute
|
|
||||||
{
|
|
||||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
|
||||||
{
|
|
||||||
UseTracing = true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, true, true);
|
|
||||||
|
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
|
||||||
.And(x => GivenARealTracer())
|
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default()
|
|
||||||
{
|
|
||||||
var fileReRoute = new FileReRoute();
|
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
|
||||||
|
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_create_options_with_specified_useCookie_and_allowAutoRedirect()
|
|
||||||
{
|
|
||||||
var fileReRoute = new FileReRoute
|
|
||||||
{
|
|
||||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
|
||||||
{
|
|
||||||
AllowAutoRedirect = false,
|
|
||||||
UseCookieContainer = false,
|
|
||||||
UseTracing = false
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
|
||||||
|
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_options_with_useproxy_true_as_default()
|
public void should_not_use_tracing_if_fake_tracer_registered()
|
||||||
{
|
{
|
||||||
var fileReRoute = new FileReRoute
|
var fileReRoute = new FileReRoute
|
||||||
{
|
{
|
||||||
HttpHandlerOptions = new FileHttpHandlerOptions()
|
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||||
};
|
{
|
||||||
|
UseTracing = true
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
}
|
||||||
|
};
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
|
||||||
.BDDfy();
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_options_with_specified_useproxy()
|
public void should_use_tracing_if_real_tracer_registered()
|
||||||
{
|
{
|
||||||
var fileReRoute = new FileReRoute
|
var fileReRoute = new FileReRoute
|
||||||
{
|
{
|
||||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||||
{
|
{
|
||||||
UseProxy = false
|
UseTracing = true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var expectedOptions = new HttpHandlerOptions(false, false, false, false);
|
var expectedOptions = new HttpHandlerOptions(false, false, true, true);
|
||||||
|
|
||||||
this.Given(x => GivenTheFollowing(fileReRoute))
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
.When(x => WhenICreateHttpHandlerOptions())
|
.And(x => GivenARealTracer())
|
||||||
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
.BDDfy();
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
}
|
.BDDfy();
|
||||||
|
}
|
||||||
private void GivenTheFollowing(FileReRoute fileReRoute)
|
|
||||||
{
|
[Fact]
|
||||||
_fileReRoute = fileReRoute;
|
public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default()
|
||||||
}
|
{
|
||||||
|
var fileReRoute = new FileReRoute();
|
||||||
private void WhenICreateHttpHandlerOptions()
|
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
||||||
{
|
|
||||||
_httpHandlerOptions = _httpHandlerOptionsCreator.Create(_fileReRoute.HttpHandlerOptions);
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
}
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
private void ThenTheFollowingOptionsReturned(HttpHandlerOptions expected)
|
.BDDfy();
|
||||||
{
|
}
|
||||||
_httpHandlerOptions.ShouldNotBeNull();
|
|
||||||
_httpHandlerOptions.AllowAutoRedirect.ShouldBe(expected.AllowAutoRedirect);
|
[Fact]
|
||||||
_httpHandlerOptions.UseCookieContainer.ShouldBe(expected.UseCookieContainer);
|
public void should_create_options_with_specified_useCookie_and_allowAutoRedirect()
|
||||||
|
{
|
||||||
|
var fileReRoute = new FileReRoute
|
||||||
|
{
|
||||||
|
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||||
|
{
|
||||||
|
AllowAutoRedirect = false,
|
||||||
|
UseCookieContainer = false,
|
||||||
|
UseTracing = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
||||||
|
|
||||||
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_create_options_with_useproxy_true_as_default()
|
||||||
|
{
|
||||||
|
var fileReRoute = new FileReRoute
|
||||||
|
{
|
||||||
|
HttpHandlerOptions = new FileHttpHandlerOptions()
|
||||||
|
};
|
||||||
|
|
||||||
|
var expectedOptions = new HttpHandlerOptions(false, false, false, true);
|
||||||
|
|
||||||
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_create_options_with_specified_useproxy()
|
||||||
|
{
|
||||||
|
var fileReRoute = new FileReRoute
|
||||||
|
{
|
||||||
|
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||||
|
{
|
||||||
|
UseProxy = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var expectedOptions = new HttpHandlerOptions(false, false, false, false);
|
||||||
|
|
||||||
|
this.Given(x => GivenTheFollowing(fileReRoute))
|
||||||
|
.When(x => WhenICreateHttpHandlerOptions())
|
||||||
|
.Then(x => ThenTheFollowingOptionsReturned(expectedOptions))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheFollowing(FileReRoute fileReRoute)
|
||||||
|
{
|
||||||
|
_fileReRoute = fileReRoute;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICreateHttpHandlerOptions()
|
||||||
|
{
|
||||||
|
_httpHandlerOptions = _httpHandlerOptionsCreator.Create(_fileReRoute.HttpHandlerOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheFollowingOptionsReturned(HttpHandlerOptions expected)
|
||||||
|
{
|
||||||
|
_httpHandlerOptions.ShouldNotBeNull();
|
||||||
|
_httpHandlerOptions.AllowAutoRedirect.ShouldBe(expected.AllowAutoRedirect);
|
||||||
|
_httpHandlerOptions.UseCookieContainer.ShouldBe(expected.UseCookieContainer);
|
||||||
_httpHandlerOptions.UseTracing.ShouldBe(expected.UseTracing);
|
_httpHandlerOptions.UseTracing.ShouldBe(expected.UseTracing);
|
||||||
_httpHandlerOptions.UseProxy.ShouldBe(expected.UseProxy);
|
_httpHandlerOptions.UseProxy.ShouldBe(expected.UseProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenARealTracer()
|
private void GivenARealTracer()
|
||||||
{
|
{
|
||||||
var tracer = new RealTracer();
|
var tracer = new RealTracer();
|
||||||
_httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(tracer);
|
_serviceCollection.AddSingleton<IServiceTracer, RealTracer>();
|
||||||
}
|
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
||||||
|
_httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceProvider);
|
||||||
class RealTracer : IServiceTracer
|
}
|
||||||
{
|
|
||||||
public ITracer Tracer => throw new NotImplementedException();
|
class RealTracer : IServiceTracer
|
||||||
|
{
|
||||||
public string ServiceName => throw new NotImplementedException();
|
public ITracer Tracer => throw new NotImplementedException();
|
||||||
|
|
||||||
public string Environment => throw new NotImplementedException();
|
public string ServiceName => throw new NotImplementedException();
|
||||||
|
|
||||||
public string Identity => throw new NotImplementedException();
|
public string Environment => throw new NotImplementedException();
|
||||||
|
|
||||||
public ISpan Start(ISpanBuilder spanBuilder)
|
public string Identity => throw new NotImplementedException();
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
public ISpan Start(ISpanBuilder spanBuilder)
|
||||||
}
|
{
|
||||||
}
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -7,6 +7,7 @@ using Xunit;
|
|||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Logging
|
namespace Ocelot.UnitTests.Logging
|
||||||
{
|
{
|
||||||
@ -15,7 +16,8 @@ namespace Ocelot.UnitTests.Logging
|
|||||||
private readonly OcelotDiagnosticListener _listener;
|
private readonly OcelotDiagnosticListener _listener;
|
||||||
private Mock<IOcelotLoggerFactory> _factory;
|
private Mock<IOcelotLoggerFactory> _factory;
|
||||||
private readonly Mock<IOcelotLogger> _logger;
|
private readonly Mock<IOcelotLogger> _logger;
|
||||||
private IServiceTracer _tracer;
|
private IServiceCollection _serviceCollection;
|
||||||
|
private IServiceProvider _serviceProvider;
|
||||||
private DownstreamContext _downstreamContext;
|
private DownstreamContext _downstreamContext;
|
||||||
private string _name;
|
private string _name;
|
||||||
private Exception _exception;
|
private Exception _exception;
|
||||||
@ -24,9 +26,10 @@ namespace Ocelot.UnitTests.Logging
|
|||||||
{
|
{
|
||||||
_factory = new Mock<IOcelotLoggerFactory>();
|
_factory = new Mock<IOcelotLoggerFactory>();
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_tracer = new FakeServiceTracer();
|
_serviceCollection = new ServiceCollection();
|
||||||
|
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
||||||
_factory.Setup(x => x.CreateLogger<OcelotDiagnosticListener>()).Returns(_logger.Object);
|
_factory.Setup(x => x.CreateLogger<OcelotDiagnosticListener>()).Returns(_logger.Object);
|
||||||
_listener = new OcelotDiagnosticListener(_factory.Object, _tracer);
|
_listener = new OcelotDiagnosticListener(_factory.Object, _serviceProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
using System;
|
||||||
using Butterfly.Client.Tracing;
|
using Butterfly.Client.Tracing;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Requester;
|
using Ocelot.Requester;
|
||||||
@ -11,13 +13,18 @@ namespace Ocelot.UnitTests.Requester
|
|||||||
{
|
{
|
||||||
private TracingHandlerFactory _factory;
|
private TracingHandlerFactory _factory;
|
||||||
private Mock<IServiceTracer> _tracer;
|
private Mock<IServiceTracer> _tracer;
|
||||||
|
private IServiceCollection _serviceCollection;
|
||||||
|
private IServiceProvider _serviceProvider;
|
||||||
private Mock<IRequestScopedDataRepository> _repo;
|
private Mock<IRequestScopedDataRepository> _repo;
|
||||||
|
|
||||||
public TracingHandlerFactoryTests()
|
public TracingHandlerFactoryTests()
|
||||||
{
|
{
|
||||||
_tracer = new Mock<IServiceTracer>();
|
_tracer = new Mock<IServiceTracer>();
|
||||||
|
_serviceCollection = new ServiceCollection();
|
||||||
|
_serviceCollection.AddSingleton<IServiceTracer>(_tracer.Object);
|
||||||
|
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
||||||
_repo = new Mock<IRequestScopedDataRepository>();
|
_repo = new Mock<IRequestScopedDataRepository>();
|
||||||
_factory = new TracingHandlerFactory(_tracer.Object, _repo.Object);
|
_factory = new TracingHandlerFactory(_serviceProvider, _repo.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -27,4 +34,4 @@ namespace Ocelot.UnitTests.Requester
|
|||||||
handler.ShouldBeOfType<OcelotHttpTracingHandler>();
|
handler.ShouldBeOfType<OcelotHttpTracingHandler>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user