mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-01 22:22:51 +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
101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
using Moq;
|
|
using Ocelot.Logging;
|
|
using Ocelot.Requester;
|
|
using Ocelot.Responses;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.Middleware;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
using Shouldly;
|
|
|
|
namespace Ocelot.UnitTests.Requester
|
|
{
|
|
public class HttpClientHttpRequesterTest
|
|
{
|
|
private readonly Mock<IHttpClientCache> _cacheHandlers;
|
|
private Mock<IDelegatingHandlerHandlerHouse> _house;
|
|
private Mock<IDelegatingHandlerHandlerProvider> _provider;
|
|
private Response<HttpResponseMessage> _response;
|
|
private readonly HttpClientHttpRequester _httpClientRequester;
|
|
private DownstreamContext _request;
|
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
|
private Mock<IOcelotLogger> _logger;
|
|
|
|
public HttpClientHttpRequesterTest()
|
|
{
|
|
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
|
_provider.Setup(x => x.Get()).Returns(new List<Func<DelegatingHandler>>());
|
|
_house = new Mock<IDelegatingHandlerHandlerHouse>();
|
|
_house.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
|
_logger = new Mock<IOcelotLogger>();
|
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
|
_loggerFactory
|
|
.Setup(x => x.CreateLogger<HttpClientHttpRequester>())
|
|
.Returns(_logger.Object);
|
|
_cacheHandlers = new Mock<IHttpClientCache>();
|
|
_httpClientRequester = new HttpClientHttpRequester(_loggerFactory.Object, _cacheHandlers.Object, _house.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_request_correctly()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
|
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
|
|
|
var context = new DownstreamContext(new DefaultHttpContext())
|
|
{
|
|
DownstreamReRoute = reRoute,
|
|
DownstreamRequest = new HttpRequestMessage() { RequestUri = new Uri("http://www.bbc.co.uk") },
|
|
};
|
|
|
|
this.Given(x=>x.GivenTheRequestIs(context))
|
|
.When(x=>x.WhenIGetResponse())
|
|
.Then(x => x.ThenTheResponseIsCalledCorrectly())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_request_unable_to_complete_request()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
|
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
|
|
|
var context = new DownstreamContext(new DefaultHttpContext())
|
|
{
|
|
DownstreamReRoute = reRoute,
|
|
DownstreamRequest = new HttpRequestMessage() { RequestUri = new Uri("http://localhost:60080") },
|
|
};
|
|
|
|
this.Given(x => x.GivenTheRequestIs(context))
|
|
.When(x => x.WhenIGetResponse())
|
|
.Then(x => x.ThenTheResponseIsCalledError())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenTheRequestIs(DownstreamContext request)
|
|
{
|
|
_request = request;
|
|
}
|
|
|
|
private void WhenIGetResponse()
|
|
{
|
|
_response = _httpClientRequester.GetResponse(_request).Result;
|
|
}
|
|
|
|
private void ThenTheResponseIsCalledCorrectly()
|
|
{
|
|
_response.IsError.ShouldBeFalse();
|
|
}
|
|
|
|
private void ThenTheResponseIsCalledError()
|
|
{
|
|
_response.IsError.ShouldBeTrue();
|
|
}
|
|
}
|
|
}
|