Ocelot/test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs
geffzhang ef3c4f614a Monitoring (#219)
* feat:  use Https://github.com/ButterflyAPM to monitor each API request monitoring metrics

* feat: using DiagnosticSource and Butterfly.OpenTracing

* refactor:refactor Ocelot tracing, merge code into OcelotDiagnosticListener

* refactor: move OcelotHttpTracingHandler to Requester

* fix: Requester\HttpClientBuilder.cs(10,14): error CS0234: The type or namespace name 'Tracing' does not exist in the namespace

* feat: add test should_set_up_tracing

* feat : Remove extraneous code

* feat: remove unused DiagnosticSource diagnostic

* fix : test UseTracing

* add test should_call_scoped_data_repository_QosProviderError

* add test should_return_any_errors

* add test HttpClientHttpRequesterTest

*  it should keep it can not be deleted
2018-02-12 18:33:23 +00:00

82 lines
2.8 KiB
C#

namespace Ocelot.UnitTests.Requester
{
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Logging;
using Ocelot.Requester;
using Ocelot.Requester.Middleware;
using Ocelot.Requester.QoS;
using Ocelot.Responses;
using TestStack.BDDfy;
using Xunit;
public class HttpRequesterMiddlewareTests : ServerHostedMiddlewareTest
{
private readonly Mock<IHttpRequester> _requester;
private OkResponse<HttpResponseMessage> _response;
private OkResponse<Ocelot.Request.Request> _request;
public HttpRequesterMiddlewareTests()
{
_requester = new Mock<IHttpRequester>();
GivenTheTestServerIsConfigured();
}
[Fact]
public void should_call_scoped_data_repository_correctly()
{
this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider(), false, false,false)))
.And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
.And(x => x.GivenTheScopedRepoReturns())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheScopedRepoIsCalledCorrectly())
.BDDfy();
}
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
{
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
services.AddLogging();
services.AddSingleton(_requester.Object);
services.AddSingleton(ScopedRepository.Object);
}
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
{
app.UseHttpRequesterMiddleware();
}
private void GivenTheRequestIs(Ocelot.Request.Request request)
{
_request = new OkResponse<Ocelot.Request.Request>(request);
ScopedRepository
.Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
.Returns(_request);
}
private void GivenTheRequesterReturns(HttpResponseMessage response)
{
_response = new OkResponse<HttpResponseMessage>(response);
_requester
.Setup(x => x.GetResponse(It.IsAny<Ocelot.Request.Request>()))
.ReturnsAsync(_response);
}
private void GivenTheScopedRepoReturns()
{
ScopedRepository
.Setup(x => x.Add(It.IsAny<string>(), _response.Data))
.Returns(new OkResponse());
}
private void ThenTheScopedRepoIsCalledCorrectly()
{
ScopedRepository
.Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
}
}
}