mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-30 21:52:50 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using Moq;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.Requester;
|
|
using Ocelot.Responses;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.UnitTests.Requester
|
|
{
|
|
public class HttpClientBuilderTests
|
|
{
|
|
private readonly HttpClientBuilder _builder;
|
|
private readonly Mock<IDelegatingHandlerHandlerHouse> _house;
|
|
private readonly Mock<IDelegatingHandlerHandlerProvider> _provider;
|
|
private IHttpClientBuilder _builderResult;
|
|
private IHttpClient _httpClient;
|
|
private HttpResponseMessage _response;
|
|
private DownstreamReRoute _request;
|
|
|
|
public HttpClientBuilderTests()
|
|
{
|
|
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
|
_house = new Mock<IDelegatingHandlerHandlerHouse>();
|
|
_builder = new HttpClientBuilder(_house.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void should_build_http_client()
|
|
{
|
|
this.Given(x => GivenTheProviderReturns())
|
|
.And(x => GivenARequest())
|
|
.And(x => GivenTheHouseReturns())
|
|
.When(x => WhenIBuild())
|
|
.Then(x => ThenTheHttpClientShouldNotBeNull())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_delegating_handlers_in_order()
|
|
{
|
|
var fakeOne = new FakeDelegatingHandler();
|
|
var fakeTwo = new FakeDelegatingHandler();
|
|
|
|
var handlers = new List<Func<DelegatingHandler>>()
|
|
{
|
|
() => fakeOne,
|
|
() => fakeTwo
|
|
};
|
|
|
|
this.Given(x => GivenTheProviderReturns(handlers))
|
|
.And(x => GivenARequest())
|
|
.And(x => GivenTheHouseReturns())
|
|
.And(x => WhenIBuild())
|
|
.When(x => WhenICallTheClient())
|
|
.Then(x => ThenTheFakeAreHandledInOrder(fakeOne, fakeTwo))
|
|
.And(x => ThenSomethingIsReturned())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenARequest()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
|
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
|
|
|
_request = reRoute;
|
|
}
|
|
|
|
private void GivenTheHouseReturns()
|
|
{
|
|
_house
|
|
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
|
.Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
|
}
|
|
|
|
private void ThenSomethingIsReturned()
|
|
{
|
|
_response.ShouldNotBeNull();
|
|
}
|
|
|
|
private void WhenICallTheClient()
|
|
{
|
|
_response = _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://test.com")).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private void ThenTheFakeAreHandledInOrder(FakeDelegatingHandler fakeOne, FakeDelegatingHandler fakeTwo)
|
|
{
|
|
fakeOne.TimeCalled.ShouldBeGreaterThan(fakeTwo.TimeCalled);
|
|
}
|
|
|
|
private void GivenTheProviderReturns()
|
|
{
|
|
_provider
|
|
.Setup(x => x.Get())
|
|
.Returns(new List<Func<DelegatingHandler>>(){ () => new FakeDelegatingHandler()});
|
|
}
|
|
|
|
private void GivenTheProviderReturns(List<Func<DelegatingHandler>> handlers)
|
|
{
|
|
_provider
|
|
.Setup(x => x.Get())
|
|
.Returns(handlers);
|
|
}
|
|
|
|
private void WhenIBuild()
|
|
{
|
|
_httpClient = _builder.Create(_request);
|
|
}
|
|
|
|
private void ThenTheHttpClientShouldNotBeNull()
|
|
{
|
|
_httpClient.ShouldNotBeNull();
|
|
}
|
|
}
|
|
}
|