mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-26 23:22: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
106 lines
4.5 KiB
C#
106 lines
4.5 KiB
C#
using Ocelot.Middleware;
|
|
|
|
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.DownstreamRouteFinder;
|
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
using Ocelot.DownstreamUrlCreator;
|
|
using Ocelot.DownstreamUrlCreator.Middleware;
|
|
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|
using Ocelot.Infrastructure.RequestData;
|
|
using Ocelot.Logging;
|
|
using Ocelot.Responses;
|
|
using Ocelot.Values;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
using Shouldly;
|
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
public class DownstreamUrlCreatorMiddlewareTests
|
|
{
|
|
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
|
|
private readonly Mock<IUrlBuilder> _urlBuilder;
|
|
private OkResponse<DownstreamPath> _downstreamPath;
|
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
|
private Mock<IOcelotLogger> _logger;
|
|
private DownstreamUrlCreatorMiddleware _middleware;
|
|
private DownstreamContext _downstreamContext;
|
|
private OcelotRequestDelegate _next;
|
|
|
|
public DownstreamUrlCreatorMiddlewareTests()
|
|
{
|
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
|
_logger = new Mock<IOcelotLogger>();
|
|
_loggerFactory.Setup(x => x.CreateLogger<DownstreamUrlCreatorMiddleware>()).Returns(_logger.Object);
|
|
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
|
|
_urlBuilder = new Mock<IUrlBuilder>();
|
|
_downstreamContext.DownstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
|
_next = async context => {
|
|
//do nothing
|
|
};
|
|
}
|
|
|
|
[Fact]
|
|
public void should_replace_scheme_and_path()
|
|
{
|
|
var downstreamReRoute = new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("any old string")
|
|
.WithUpstreamHttpMethod(new List<string> {"Get"})
|
|
.WithDownstreamScheme("https")
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheDownStreamRouteIs(
|
|
new DownstreamRoute(
|
|
new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(downstreamReRoute)
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.And(x => x.GivenTheDownstreamRequestUriIs("http://my.url/abc?q=123"))
|
|
.And(x => x.GivenTheUrlReplacerWillReturn("/api/products/1"))
|
|
.When(x => x.WhenICallTheMiddleware())
|
|
.Then(x => x.ThenTheDownstreamRequestUriIs("https://my.url:80/api/products/1?q=123"))
|
|
.BDDfy();
|
|
}
|
|
|
|
private void WhenICallTheMiddleware()
|
|
{
|
|
_middleware = new DownstreamUrlCreatorMiddleware(_next, _loggerFactory.Object, _downstreamUrlTemplateVariableReplacer.Object, _urlBuilder.Object);
|
|
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
|
{
|
|
_downstreamContext.TemplatePlaceholderNameAndValues = downstreamRoute.TemplatePlaceholderNameAndValues;
|
|
_downstreamContext.DownstreamReRoute = downstreamRoute.ReRoute.DownstreamReRoute[0];
|
|
}
|
|
|
|
private void GivenTheDownstreamRequestUriIs(string uri)
|
|
{
|
|
_downstreamContext.DownstreamRequest.RequestUri = new Uri(uri);
|
|
}
|
|
|
|
private void GivenTheUrlReplacerWillReturn(string path)
|
|
{
|
|
_downstreamPath = new OkResponse<DownstreamPath>(new DownstreamPath(path));
|
|
_downstreamUrlTemplateVariableReplacer
|
|
.Setup(x => x.Replace(It.IsAny<PathTemplate>(), It.IsAny<List<PlaceholderNameAndValue>>()))
|
|
.Returns(_downstreamPath);
|
|
}
|
|
|
|
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
|
{
|
|
_downstreamContext.DownstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
|
}
|
|
}
|
|
}
|