bunch of refactoring

This commit is contained in:
TomPallister
2016-10-26 21:38:44 +01:00
parent 9575adc90d
commit 367fa327b3
35 changed files with 264 additions and 157 deletions

View File

@ -56,7 +56,7 @@ namespace Ocelot.UnitTests.Authentication
[Fact]
public void happy_path()
{
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().Build())))
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().Build())))
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenNoExceptionsAreThrown())
.BDDfy();

View File

@ -58,7 +58,7 @@ namespace Ocelot.UnitTests.Authorization
[Fact]
public void happy_path()
{
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithIsAuthorised(true).Build())))
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithIsAuthorised(true).Build())))
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())

View File

@ -59,7 +59,7 @@ namespace Ocelot.UnitTests.ClaimsBuilder
[Fact]
public void happy_path()
{
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamTemplate("any old string")
.WithClaimsToClaims(new List<ClaimToThing>

View File

@ -57,7 +57,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
[Fact]
public void happy_path()
{
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("any old string").Build())))
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("any old string").Build())))
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
.BDDfy();

View File

@ -18,7 +18,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
private readonly Mock<IOcelotConfigurationProvider> _mockConfig;
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
private readonly Mock<ITemplateVariableNameAndValueFinder> _finder;
private readonly Mock<IUrlPathPlaceholderNameAndValueFinder> _finder;
private string _upstreamUrlPath;
private Response<DownstreamRoute> _result;
private List<ReRoute> _reRoutesConfig;
@ -29,7 +29,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
{
_mockConfig = new Mock<IOcelotConfigurationProvider>();
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
_finder = new Mock<ITemplateVariableNameAndValueFinder>();
_finder = new Mock<IUrlPathPlaceholderNameAndValueFinder>();
_downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object, _finder.Object);
}
@ -40,7 +40,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(
x =>
x.GivenTheTemplateVariableAndNameFinderReturns(
new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
new OkResponse<List<UrlPathPlaceholderNameAndValue>>(new List<UrlPathPlaceholderNameAndValue>())))
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
{
new ReRouteBuilder()
@ -55,7 +55,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
.When(x => x.WhenICallTheFinder())
.Then(
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamTemplate("someDownstreamPath")
.Build()
@ -71,7 +71,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(
x =>
x.GivenTheTemplateVariableAndNameFinderReturns(
new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
new OkResponse<List<UrlPathPlaceholderNameAndValue>>(new List<UrlPathPlaceholderNameAndValue>())))
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
{
new ReRouteBuilder()
@ -92,7 +92,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
.When(x => x.WhenICallTheFinder())
.Then(
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamTemplate("someDownstreamPathForAPost")
.Build()
@ -123,7 +123,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.BDDfy();
}
private void GivenTheTemplateVariableAndNameFinderReturns(Response<List<TemplateVariableNameAndValue>> response)
private void GivenTheTemplateVariableAndNameFinderReturns(Response<List<UrlPathPlaceholderNameAndValue>> response)
{
_finder
.Setup(x => x.Find(It.IsAny<string>(), It.IsAny<string>()))
@ -176,13 +176,13 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
{
_result.Data.ReRoute.DownstreamTemplate.ShouldBe(expected.ReRoute.DownstreamTemplate);
for (int i = 0; i < _result.Data.TemplateVariableNameAndValues.Count; i++)
for (int i = 0; i < _result.Data.TemplatePlaceholderNameAndValues.Count; i++)
{
_result.Data.TemplateVariableNameAndValues[i].TemplateVariableName.ShouldBe(
expected.TemplateVariableNameAndValues[i].TemplateVariableName);
_result.Data.TemplatePlaceholderNameAndValues[i].TemplateVariableName.ShouldBe(
expected.TemplatePlaceholderNameAndValues[i].TemplateVariableName);
_result.Data.TemplateVariableNameAndValues[i].TemplateVariableValue.ShouldBe(
expected.TemplateVariableNameAndValues[i].TemplateVariableValue);
_result.Data.TemplatePlaceholderNameAndValues[i].TemplateVariableValue.ShouldBe(
expected.TemplatePlaceholderNameAndValues[i].TemplateVariableValue);
}
_result.IsError.ShouldBeFalse();

View File

@ -10,14 +10,14 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
{
public class UrlPathToUrlTemplateMatcherTests
{
private readonly ITemplateVariableNameAndValueFinder _finder;
private readonly IUrlPathPlaceholderNameAndValueFinder _finder;
private string _downstreamUrlPath;
private string _downstreamPathTemplate;
private Response<List<TemplateVariableNameAndValue>> _result;
private Response<List<UrlPathPlaceholderNameAndValue>> _result;
public UrlPathToUrlTemplateMatcherTests()
{
_finder = new TemplateVariableNameAndValueFinder();
_finder = new UrlPathPlaceholderNameAndValueFinder();
}
[Fact]
@ -26,7 +26,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
this.Given(x => x.GivenIHaveAUpstreamPath(""))
.And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
.And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
.BDDfy();
}
@ -36,7 +36,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
.And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
.BDDfy();
}
@ -46,7 +46,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
.And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
.BDDfy();
}
@ -56,16 +56,16 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
.And(x => x.ThenTheTemplatesVariablesAre(new List<TemplateVariableNameAndValue>()))
.And(x => x.ThenTheTemplatesVariablesAre(new List<UrlPathPlaceholderNameAndValue>()))
.BDDfy();
}
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
new TemplateVariableNameAndValue("{productId}", "1")
new UrlPathPlaceholderNameAndValue("{productId}", "1")
};
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
@ -78,10 +78,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
};
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
@ -94,10 +94,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
};
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
@ -110,11 +110,11 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2"),
new TemplateVariableNameAndValue("{variantId}", "123")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{categoryId}", "2"),
new UrlPathPlaceholderNameAndValue("{variantId}", "123")
};
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
@ -127,10 +127,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
[Fact]
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
{
var expectedTemplates = new List<TemplateVariableNameAndValue>
var expectedTemplates = new List<UrlPathPlaceholderNameAndValue>
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{categoryId}", "2")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{categoryId}", "2")
};
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
@ -140,7 +140,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
.BDDfy();
}
private void ThenTheTemplatesVariablesAre(List<TemplateVariableNameAndValue> expectedResults)
private void ThenTheTemplatesVariablesAre(List<UrlPathPlaceholderNameAndValue> expectedResults)
{
foreach (var expectedResult in expectedResults)
{

View File

@ -20,19 +20,19 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
{
public class DownstreamUrlCreatorMiddlewareTests : IDisposable
{
private readonly Mock<IDownstreamUrlTemplateVariableReplacer> _downstreamUrlTemplateVariableReplacer;
private readonly Mock<IDownstreamUrlPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
private readonly string _url;
private readonly TestServer _server;
private readonly HttpClient _client;
private Response<DownstreamRoute> _downstreamRoute;
private HttpResponseMessage _result;
private OkResponse<string> _downstreamUrl;
private OkResponse<DownstreamUrl> _downstreamUrl;
public DownstreamUrlCreatorMiddlewareTests()
{
_url = "http://localhost:51879";
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamUrlTemplateVariableReplacer>();
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamUrlPathPlaceholderReplacer>();
_scopedRepository = new Mock<IRequestScopedDataRepository>();
var builder = new WebHostBuilder()
@ -58,7 +58,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
[Fact]
public void happy_path()
{
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("any old string").Build())))
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("any old string").Build())))
.And(x => x.TheUrlReplacerReturns("any old string"))
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
@ -67,16 +67,16 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
private void TheUrlReplacerReturns(string downstreamUrl)
{
_downstreamUrl = new OkResponse<string>(downstreamUrl);
_downstreamUrl = new OkResponse<DownstreamUrl>(new DownstreamUrl(downstreamUrl));
_downstreamUrlTemplateVariableReplacer
.Setup(x => x.ReplaceTemplateVariables(It.IsAny<DownstreamRoute>()))
.Setup(x => x.Replace(It.IsAny<string>(), It.IsAny<List<UrlPathPlaceholderNameAndValue>>()))
.Returns(_downstreamUrl);
}
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
{
_scopedRepository
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once());
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data.Value), Times.Once());
}
private void WhenICallTheMiddleware()

View File

@ -13,18 +13,18 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
public class UpstreamUrlPathTemplateVariableReplacerTests
{
private DownstreamRoute _downstreamRoute;
private Response<string> _result;
private readonly IDownstreamUrlTemplateVariableReplacer _downstreamUrlPathReplacer;
private Response<DownstreamUrl> _result;
private readonly IDownstreamUrlPathPlaceholderReplacer _downstreamUrlPathReplacer;
public UpstreamUrlPathTemplateVariableReplacerTests()
{
_downstreamUrlPathReplacer = new DownstreamUrlTemplateVariableReplacer();
_downstreamUrlPathReplacer = new DownstreamUrlPathPlaceholderReplacer();
}
[Fact]
public void can_replace_no_template_variables()
{
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().Build())))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().Build())))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
.BDDfy();
@ -33,7 +33,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_no_template_variables_with_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("/").Build())))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("/").Build())))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
.BDDfy();
@ -42,7 +42,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_no_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api").Build())))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api").Build())))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
.BDDfy();
@ -51,7 +51,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_one_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api/").Build())))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api/").Build())))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
.BDDfy();
@ -60,7 +60,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_multiple_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api/product/products/").Build())))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder().WithDownstreamTemplate("api/product/products/").Build())))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
.BDDfy();
@ -69,9 +69,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_one_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
var templateVariables = new List<UrlPathPlaceholderNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1")
new UrlPathPlaceholderNameAndValue("{productId}", "1")
};
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRouteBuilder().WithDownstreamTemplate("productservice/products/{productId}/").Build())))
@ -83,9 +83,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_one_template_variable_with_path_after()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
var templateVariables = new List<UrlPathPlaceholderNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1")
new UrlPathPlaceholderNameAndValue("{productId}", "1")
};
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRouteBuilder().WithDownstreamTemplate("productservice/products/{productId}/variants").Build())))
@ -97,10 +97,10 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_two_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
var templateVariables = new List<UrlPathPlaceholderNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{variantId}", "12")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{variantId}", "12")
};
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRouteBuilder().WithDownstreamTemplate("productservice/products/{productId}/variants/{variantId}").Build())))
@ -112,11 +112,11 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
[Fact]
public void can_replace_url_three_template_variable()
{
var templateVariables = new List<TemplateVariableNameAndValue>()
var templateVariables = new List<UrlPathPlaceholderNameAndValue>()
{
new TemplateVariableNameAndValue("{productId}", "1"),
new TemplateVariableNameAndValue("{variantId}", "12"),
new TemplateVariableNameAndValue("{categoryId}", "34")
new UrlPathPlaceholderNameAndValue("{productId}", "1"),
new UrlPathPlaceholderNameAndValue("{variantId}", "12"),
new UrlPathPlaceholderNameAndValue("{categoryId}", "34")
};
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRouteBuilder().WithDownstreamTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}").Build())))
@ -132,12 +132,12 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
private void WhenIReplaceTheTemplateVariables()
{
_result = _downstreamUrlPathReplacer.ReplaceTemplateVariables(_downstreamRoute);
_result = _downstreamUrlPathReplacer.Replace(_downstreamRoute.ReRoute.DownstreamTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
}
private void ThenTheDownstreamUrlPathIsReturned(string expected)
{
_result.Data.ShouldBe(expected);
_result.Data.Value.ShouldBe(expected);
}
}
}

View File

@ -58,7 +58,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
[Fact]
public void happy_path()
{
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamTemplate("any old string")
.WithClaimsToHeaders(new List<ClaimToThing>

View File

@ -81,6 +81,23 @@ namespace Ocelot.UnitTests.RequestBuilder
.BDDfy();
}
[Fact]
public void should_use_unvalidated_http_content_headers()
{
this.Given(x => x.GivenIHaveHttpMethod("POST"))
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
.And(x => x.GivenTheContentTypeIs("application/json; charset=utf-8"))
.When(x => x.WhenICreateARequest())
.And(x => x.ThenTheCorrectContentHeadersAreUsed(new HeaderDictionary
{
{
"Content-Type", "application/json; charset=utf-8"
}
}))
.BDDfy();
}
[Fact]
public void should_use_headers()
{

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
@ -10,6 +11,7 @@ using Ocelot.Infrastructure.RequestData;
using Ocelot.RequestBuilder;
using Ocelot.Requester;
using Ocelot.Requester.Middleware;
using Ocelot.Responder;
using Ocelot.Responses;
using TestStack.BDDfy;
using Xunit;
@ -26,16 +28,19 @@ namespace Ocelot.UnitTests.Requester
private HttpResponseMessage _result;
private OkResponse<HttpResponseMessage> _response;
private OkResponse<Request> _request;
private readonly Mock<IHttpResponder> _responder;
public HttpRequesterMiddlewareTests()
{
_url = "http://localhost:51879";
_requester = new Mock<IHttpRequester>();
_scopedRepository = new Mock<IRequestScopedDataRepository>();
_responder = new Mock<IHttpResponder>();
var builder = new WebHostBuilder()
.ConfigureServices(x =>
{
x.AddSingleton(_responder.Object);
x.AddSingleton(_requester.Object);
x.AddSingleton(_scopedRepository.Object);
})
@ -58,8 +63,9 @@ namespace Ocelot.UnitTests.Requester
{
this.Given(x => x.GivenTheRequestIs(new Request(new HttpRequestMessage(),new CookieContainer())))
.And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
.And(x => x.GivenTheResponderReturns())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
.Then(x => x.ThenTheResponderIsCalledCorrectly())
.BDDfy();
}
@ -71,10 +77,17 @@ namespace Ocelot.UnitTests.Requester
.ReturnsAsync(_response);
}
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
private void GivenTheResponderReturns()
{
_scopedRepository
.Verify(x => x.Add("Response", _response.Data), Times.Once());
_responder
.Setup(x => x.SetResponseOnHttpContext(It.IsAny<HttpContext>(), _response.Data))
.ReturnsAsync(new OkResponse());
}
private void ThenTheResponderIsCalledCorrectly()
{
_responder
.Verify(x => x.SetResponseOnHttpContext(It.IsAny<HttpContext>(), _response.Data), Times.Once());
}
private void WhenICallTheMiddleware()

View File

@ -15,7 +15,7 @@ using Xunit;
namespace Ocelot.UnitTests.Responder
{
public class HttpResponderMiddlewareTests : IDisposable
public class HttpErrorResponderMiddlewareTests : IDisposable
{
private readonly Mock<IHttpResponder> _responder;
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
@ -26,7 +26,7 @@ namespace Ocelot.UnitTests.Responder
private HttpResponseMessage _result;
private OkResponse<HttpResponseMessage> _response;
public HttpResponderMiddlewareTests()
public HttpErrorResponderMiddlewareTests()
{
_url = "http://localhost:51879";
_responder = new Mock<IHttpResponder>();
@ -47,7 +47,7 @@ namespace Ocelot.UnitTests.Responder
.UseUrls(_url)
.Configure(app =>
{
app.UseHttpResponderMiddleware();
app.UseHttpErrorResponderMiddleware();
});
_server = new TestServer(builder);
@ -60,7 +60,7 @@ namespace Ocelot.UnitTests.Responder
this.Given(x => x.GivenTheHttpResponseMessageIs(new HttpResponseMessage()))
.And(x => x.GivenThereAreNoPipelineErrors())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.TheResponderIsCalledCorrectly())
.Then(x => x.ThenThereAreNoErrors())
.BDDfy();
}
@ -71,10 +71,9 @@ namespace Ocelot.UnitTests.Responder
.Returns(new OkResponse<bool>(false));
}
private void TheResponderIsCalledCorrectly()
private void ThenThereAreNoErrors()
{
_responder
.Verify(x => x.CreateResponse(It.IsAny<HttpContext>(), _response.Data), Times.Once);
//todo a better assert?
}
private void WhenICallTheMiddleware()