mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-30 00:32: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
211 lines
9.3 KiB
C#
211 lines
9.3 KiB
C#
using System.Collections.Generic;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.DownstreamRouteFinder;
|
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|
using Ocelot.Responses;
|
|
using Ocelot.Values;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
|
|
{
|
|
public class UpstreamUrlPathTemplateVariableReplacerTests
|
|
{
|
|
private DownstreamRoute _downstreamRoute;
|
|
private Response<DownstreamPath> _result;
|
|
private readonly IDownstreamPathPlaceholderReplacer _downstreamPathReplacer;
|
|
|
|
public UpstreamUrlPathTemplateVariableReplacerTests()
|
|
{
|
|
_downstreamPathReplacer = new DownstreamTemplatePathPlaceholderReplacer();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_no_template_variables()
|
|
{
|
|
this.Given(x => x.GivenThereIsAUrlMatch(
|
|
new DownstreamRoute(
|
|
new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_no_template_variables_with_slash()
|
|
{
|
|
this.Given(x => x.GivenThereIsAUrlMatch(
|
|
new DownstreamRoute(
|
|
new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("/")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_no_slash()
|
|
{
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("api")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_one_slash()
|
|
{
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("api/")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_multiple_slash()
|
|
{
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("api/product/products/")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_one_template_variable()
|
|
{
|
|
var templateVariables = new List<PlaceholderNameAndValue>()
|
|
{
|
|
new PlaceholderNameAndValue("{productId}", "1")
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("productservice/products/{productId}/")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_one_template_variable_with_path_after()
|
|
{
|
|
var templateVariables = new List<PlaceholderNameAndValue>()
|
|
{
|
|
new PlaceholderNameAndValue("{productId}", "1")
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("productservice/products/{productId}/variants")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_two_template_variable()
|
|
{
|
|
var templateVariables = new List<PlaceholderNameAndValue>()
|
|
{
|
|
new PlaceholderNameAndValue("{productId}", "1"),
|
|
new PlaceholderNameAndValue("{variantId}", "12")
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("productservice/products/{productId}/variants/{variantId}")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void can_replace_url_three_template_variable()
|
|
{
|
|
var templateVariables = new List<PlaceholderNameAndValue>()
|
|
{
|
|
new PlaceholderNameAndValue("{productId}", "1"),
|
|
new PlaceholderNameAndValue("{variantId}", "12"),
|
|
new PlaceholderNameAndValue("{categoryId}", "34")
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
|
new ReRouteBuilder()
|
|
.WithDownstreamReRoute(new DownstreamReRouteBuilder()
|
|
.WithDownstreamPathTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build())))
|
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenThereIsAUrlMatch(DownstreamRoute downstreamRoute)
|
|
{
|
|
_downstreamRoute = downstreamRoute;
|
|
}
|
|
|
|
private void WhenIReplaceTheTemplateVariables()
|
|
{
|
|
_result = _downstreamPathReplacer.Replace(_downstreamRoute.ReRoute.DownstreamReRoute[0].DownstreamPathTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
|
|
}
|
|
|
|
private void ThenTheDownstreamUrlPathIsReturned(string expected)
|
|
{
|
|
_result.Data.Value.ShouldBe(expected);
|
|
}
|
|
}
|
|
}
|