hacked together load balancing reroutes in fileconfig (#211)

* hacked together load balancing reroutes in fileconfig

* some renaming and refactoring

* more renames

* hacked away the old config json

* test for issue 213

* renamed key

* dont share ports

* oops

* updated docs

* mvoed docs around

* port being used
This commit is contained in:
Tom Pallister
2018-01-31 20:34:55 +00:00
committed by GitHub
parent f572d1b0ca
commit 3ac9b3bd87
440 changed files with 29740 additions and 28464 deletions

View File

@ -1,103 +1,103 @@
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;
public class DownstreamUrlCreatorMiddlewareTests : ServerHostedMiddlewareTest
{
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
private readonly Mock<IUrlBuilder> _urlBuilder;
private Response<DownstreamRoute> _downstreamRoute;
private OkResponse<DownstreamPath> _downstreamPath;
private HttpRequestMessage _downstreamRequest;
public DownstreamUrlCreatorMiddlewareTests()
{
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
_urlBuilder = new Mock<IUrlBuilder>();
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
ScopedRepository
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
GivenTheTestServerIsConfigured();
}
[Fact]
public void should_replace_scheme_and_path()
{
this.Given(x => x.GivenTheDownStreamRouteIs(
new DownstreamRoute(
new List<PlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamPathTemplate("any old string")
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithDownstreamScheme("https")
.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();
}
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
{
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
services.AddLogging();
services.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
services.AddSingleton(ScopedRepository.Object);
services.AddSingleton(_urlBuilder.Object);
}
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
{
app.UseDownstreamUrlCreatorMiddleware();
}
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
{
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
ScopedRepository
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
.Returns(_downstreamRoute);
}
private void GivenTheDownstreamRequestUriIs(string uri)
{
_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)
{
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
}
}
}
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;
public class DownstreamUrlCreatorMiddlewareTests : ServerHostedMiddlewareTest
{
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
private readonly Mock<IUrlBuilder> _urlBuilder;
private Response<DownstreamRoute> _downstreamRoute;
private OkResponse<DownstreamPath> _downstreamPath;
private HttpRequestMessage _downstreamRequest;
public DownstreamUrlCreatorMiddlewareTests()
{
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
_urlBuilder = new Mock<IUrlBuilder>();
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
ScopedRepository
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
GivenTheTestServerIsConfigured();
}
[Fact]
public void should_replace_scheme_and_path()
{
this.Given(x => x.GivenTheDownStreamRouteIs(
new DownstreamRoute(
new List<PlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamPathTemplate("any old string")
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithDownstreamScheme("https")
.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();
}
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
{
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
services.AddLogging();
services.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
services.AddSingleton(ScopedRepository.Object);
services.AddSingleton(_urlBuilder.Object);
}
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
{
app.UseDownstreamUrlCreatorMiddleware();
}
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
{
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
ScopedRepository
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
.Returns(_downstreamRoute);
}
private void GivenTheDownstreamRequestUriIs(string uri)
{
_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)
{
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
}
}
}

View File

@ -1,122 +1,122 @@
using System;
using Ocelot.DownstreamUrlCreator;
using Ocelot.Responses;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.DownstreamUrlCreator
{
public class UrlBuilderTests
{
private readonly IUrlBuilder _urlBuilder;
private string _dsPath;
private string _dsScheme;
private string _dsHost;
private int _dsPort;
private Response<DownstreamUrl> _result;
public UrlBuilderTests()
{
_urlBuilder = new UrlBuilder();
}
[Fact]
public void should_return_error_when_downstream_path_is_null()
{
this.Given(x => x.GivenADownstreamPath(null))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamPathNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_return_error_when_downstream_scheme_is_null()
{
this.Given(x => x.GivenADownstreamScheme(null))
.And(x => x.GivenADownstreamPath("test"))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamSchemeNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_return_error_when_downstream_host_is_null()
{
this.Given(x => x.GivenADownstreamScheme(null))
.And(x => x.GivenADownstreamPath("test"))
.And(x => x.GivenADownstreamScheme("test"))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamHostNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_not_use_port_if_zero()
{
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
.And(x => x.GivenADownstreamScheme("http"))
.And(x => x.GivenADownstreamHost("127.0.0.1"))
.And(x => x.GivenADownstreamPort(0))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1/api/products/1"))
.And(x => x.ThenTheUrlIsWellFormed())
.BDDfy();
}
[Fact]
public void should_build_well_formed_uri()
{
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
.And(x => x.GivenADownstreamScheme("http"))
.And(x => x.GivenADownstreamHost("127.0.0.1"))
.And(x => x.GivenADownstreamPort(5000))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1:5000/api/products/1"))
.And(x => x.ThenTheUrlIsWellFormed())
.BDDfy();
}
private void ThenThereIsAnErrorOfType<T>()
{
_result.Errors[0].ShouldBeOfType<T>();
}
private void GivenADownstreamPath(string dsPath)
{
_dsPath = dsPath;
}
private void GivenADownstreamScheme(string dsScheme)
{
_dsScheme = dsScheme;
}
private void GivenADownstreamHost(string dsHost)
{
_dsHost = dsHost;
}
private void GivenADownstreamPort(int dsPort)
{
_dsPort = dsPort;
}
private void WhenIBuildTheUrl()
{
_result = _urlBuilder.Build(_dsPath, _dsScheme, new HostAndPort(_dsHost, _dsPort));
}
private void ThenTheUrlIsReturned(string expected)
{
_result.Data.Value.ShouldBe(expected);
}
private void ThenTheUrlIsWellFormed()
{
Uri.IsWellFormedUriString(_result.Data.Value, UriKind.Absolute).ShouldBeTrue();
}
}
}
using System;
using Ocelot.DownstreamUrlCreator;
using Ocelot.Responses;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.DownstreamUrlCreator
{
public class UrlBuilderTests
{
private readonly IUrlBuilder _urlBuilder;
private string _dsPath;
private string _dsScheme;
private string _dsHost;
private int _dsPort;
private Response<DownstreamUrl> _result;
public UrlBuilderTests()
{
_urlBuilder = new UrlBuilder();
}
[Fact]
public void should_return_error_when_downstream_path_is_null()
{
this.Given(x => x.GivenADownstreamPath(null))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamPathNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_return_error_when_downstream_scheme_is_null()
{
this.Given(x => x.GivenADownstreamScheme(null))
.And(x => x.GivenADownstreamPath("test"))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamSchemeNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_return_error_when_downstream_host_is_null()
{
this.Given(x => x.GivenADownstreamScheme(null))
.And(x => x.GivenADownstreamPath("test"))
.And(x => x.GivenADownstreamScheme("test"))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamHostNullOrEmptyError>())
.BDDfy();
}
[Fact]
public void should_not_use_port_if_zero()
{
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
.And(x => x.GivenADownstreamScheme("http"))
.And(x => x.GivenADownstreamHost("127.0.0.1"))
.And(x => x.GivenADownstreamPort(0))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1/api/products/1"))
.And(x => x.ThenTheUrlIsWellFormed())
.BDDfy();
}
[Fact]
public void should_build_well_formed_uri()
{
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
.And(x => x.GivenADownstreamScheme("http"))
.And(x => x.GivenADownstreamHost("127.0.0.1"))
.And(x => x.GivenADownstreamPort(5000))
.When(x => x.WhenIBuildTheUrl())
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1:5000/api/products/1"))
.And(x => x.ThenTheUrlIsWellFormed())
.BDDfy();
}
private void ThenThereIsAnErrorOfType<T>()
{
_result.Errors[0].ShouldBeOfType<T>();
}
private void GivenADownstreamPath(string dsPath)
{
_dsPath = dsPath;
}
private void GivenADownstreamScheme(string dsScheme)
{
_dsScheme = dsScheme;
}
private void GivenADownstreamHost(string dsHost)
{
_dsHost = dsHost;
}
private void GivenADownstreamPort(int dsPort)
{
_dsPort = dsPort;
}
private void WhenIBuildTheUrl()
{
_result = _urlBuilder.Build(_dsPath, _dsScheme, new ServiceHostAndPort(_dsHost, _dsPort));
}
private void ThenTheUrlIsReturned(string expected)
{
_result.Data.Value.ShouldBe(expected);
}
private void ThenTheUrlIsWellFormed()
{
Uri.IsWellFormedUriString(_result.Data.Value, UriKind.Absolute).ShouldBeTrue();
}
}
}

View File

@ -1,183 +1,183 @@
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()
.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()
.WithDownstreamPathTemplate("/")
.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()
.WithDownstreamPathTemplate("api")
.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()
.WithDownstreamPathTemplate("api/")
.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()
.WithDownstreamPathTemplate("api/product/products/")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/variants")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/variants/{variantId}")
.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()
.WithDownstreamPathTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")
.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.DownstreamPathTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
}
private void ThenTheDownstreamUrlPathIsReturned(string expected)
{
_result.Data.Value.ShouldBe(expected);
}
}
}
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()
.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()
.WithDownstreamPathTemplate("/")
.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()
.WithDownstreamPathTemplate("api")
.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()
.WithDownstreamPathTemplate("api/")
.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()
.WithDownstreamPathTemplate("api/product/products/")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/variants")
.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()
.WithDownstreamPathTemplate("productservice/products/{productId}/variants/{variantId}")
.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()
.WithDownstreamPathTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")
.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.DownstreamPathTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
}
private void ThenTheDownstreamUrlPathIsReturned(string expected)
{
_result.Data.Value.ShouldBe(expected);
}
}
}