From f2f583a88cdb281d4e14fcaf5247f857b4518b12 Mon Sep 17 00:00:00 2001 From: Andrea Tosato Date: Mon, 10 Feb 2020 18:01:54 +0100 Subject: [PATCH 1/7] Add DownstreamHttpVersion property for Http/2 WebServer --- .../Configuration/Builder/DownstreamReRouteBuilder.cs | 11 ++++++++++- src/Ocelot/Configuration/Creator/ReRoutesCreator.cs | 1 + src/Ocelot/Configuration/DownstreamReRoute.cs | 6 +++++- src/Ocelot/Configuration/File/FileReRoute.cs | 1 + .../Configuration/Validator/ReRouteFluentValidator.cs | 5 +++++ src/Ocelot/Request/Mapper/RequestMapper.cs | 8 +++++++- 6 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index ed978dde..c318b949 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -1,5 +1,6 @@ using Ocelot.Configuration.Creator; using Ocelot.Values; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -42,6 +43,7 @@ namespace Ocelot.Configuration.Builder private bool _dangerousAcceptAnyServerCertificateValidator; private SecurityOptions _securityOptions; private string _downstreamHttpMethod; + private string _downstreamHttpVersion; public DownstreamReRouteBuilder() { @@ -255,6 +257,12 @@ namespace Ocelot.Configuration.Builder return this; } + public DownstreamReRouteBuilder WithHttpVersion(string httpVersion) + { + _downstreamHttpVersion = httpVersion; + return this; + } + public DownstreamReRoute Build() { return new DownstreamReRoute( @@ -290,7 +298,8 @@ namespace Ocelot.Configuration.Builder _addHeadersToUpstream, _dangerousAcceptAnyServerCertificateValidator, _securityOptions, - _downstreamHttpMethod); + _downstreamHttpMethod, + _downstreamHttpVersion); } } } diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index 1cc463ef..63f42197 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -138,6 +138,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) + .WithHttpVersion(fileReRoute.DownstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/src/Ocelot/Configuration/DownstreamReRoute.cs b/src/Ocelot/Configuration/DownstreamReRoute.cs index 1c41f648..d8a3cb74 100644 --- a/src/Ocelot/Configuration/DownstreamReRoute.cs +++ b/src/Ocelot/Configuration/DownstreamReRoute.cs @@ -1,6 +1,7 @@ namespace Ocelot.Configuration { using Creator; + using System; using System.Collections.Generic; using Values; @@ -39,7 +40,8 @@ namespace Ocelot.Configuration List addHeadersToUpstream, bool dangerousAcceptAnyServerCertificateValidator, SecurityOptions securityOptions, - string downstreamHttpMethod) + string downstreamHttpMethod, + string downstreamHttpVersion) { DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; AddHeadersToDownstream = addHeadersToDownstream; @@ -74,6 +76,7 @@ namespace Ocelot.Configuration AddHeadersToUpstream = addHeadersToUpstream; SecurityOptions = securityOptions; DownstreamHttpMethod = downstreamHttpMethod; + DownstreamHttpVersion = downstreamHttpVersion; } public string Key { get; } @@ -109,5 +112,6 @@ namespace Ocelot.Configuration public bool DangerousAcceptAnyServerCertificateValidator { get; } public SecurityOptions SecurityOptions { get; } public string DownstreamHttpMethod { get; } + public string DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/Configuration/File/FileReRoute.cs b/src/Ocelot/Configuration/File/FileReRoute.cs index 1bae31dd..b1a6cd4d 100644 --- a/src/Ocelot/Configuration/File/FileReRoute.cs +++ b/src/Ocelot/Configuration/File/FileReRoute.cs @@ -56,5 +56,6 @@ namespace Ocelot.Configuration.File public int Timeout { get; set; } public bool DangerousAcceptAnyServerCertificateValidator { get; set; } public FileSecurityOptions SecurityOptions { get; set; } + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs b/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs index 746c430c..99a5a422 100644 --- a/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs +++ b/src/Ocelot/Configuration/Validator/ReRouteFluentValidator.cs @@ -83,6 +83,11 @@ RuleForEach(reRoute => reRoute.DownstreamHostAndPorts) .SetValidator(hostAndPortValidator); }); + + When(reRoute => !string.IsNullOrEmpty(reRoute.DownstreamHttpVersion), () => + { + RuleFor(r => r.DownstreamHttpVersion).Matches("^[0-9]([.,][0-9]{1,1})?$"); + }); } private async Task IsSupportedAuthenticationProviders(FileAuthenticationOptions authenticationOptions, CancellationToken cancellationToken) diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index e050f1a6..f7ad7c38 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -20,11 +20,17 @@ { try { + if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version)) + { + version = new Version(1, 1); + } + var requestMessage = new HttpRequestMessage() { Content = await MapContent(request), Method = MapMethod(request, downstreamReRoute), - RequestUri = MapUri(request) + RequestUri = MapUri(request), + Version = version, }; MapHeaders(request, requestMessage); From 42a1395a84eb5e029e7e9d7da3ccaae44ff6b528 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Mon, 17 Feb 2020 07:45:44 +0000 Subject: [PATCH 2/7] tests for downstream http version validation --- .../Validation/ReRouteFluentValidatorTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs index 9433bb89..82b3e646 100644 --- a/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/Validation/ReRouteFluentValidatorTests.cs @@ -305,6 +305,71 @@ .BDDfy(); } + [Theory] + [InlineData("1.0")] + [InlineData("1.1")] + [InlineData("2.0")] + [InlineData("1,0")] + [InlineData("1,1")] + [InlineData("2,0")] + [InlineData("1")] + [InlineData("2")] + [InlineData("")] + [InlineData(null)] + public void should_be_valid_re_route_using_downstream_http_version(string version) + { + var fileReRoute = new FileReRoute + { + DownstreamPathTemplate = "/test", + UpstreamPathTemplate = "/test", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 5000, + }, + }, + DownstreamHttpVersion = version, + }; + + this.Given(_ => GivenThe(fileReRoute)) + .When(_ => WhenIValidate()) + .Then(_ => ThenTheResultIsValid()) + .BDDfy(); + } + + [Theory] + [InlineData("retg1.1")] + [InlineData("re2.0")] + [InlineData("1,0a")] + [InlineData("a1,1")] + [InlineData("12,0")] + [InlineData("asdf")] + public void should_be_invalid_re_route_using_downstream_http_version(string version) + { + var fileReRoute = new FileReRoute + { + DownstreamPathTemplate = "/test", + UpstreamPathTemplate = "/test", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = 5000, + }, + }, + DownstreamHttpVersion = version, + }; + + this.Given(_ => GivenThe(fileReRoute)) + .When(_ => WhenIValidate()) + .Then(_ => ThenTheResultIsInvalid()) + .And(_ => ThenTheErrorsContains("'Downstream Http Version' is not in the correct format.")) + .BDDfy(); + } + private void GivenAnAuthProvider(string key) { var schemes = new List From b8922cef5f7fdffe5754e6aa40a2eba5f296a1ca Mon Sep 17 00:00:00 2001 From: TomPallister Date: Mon, 17 Feb 2020 08:03:11 +0000 Subject: [PATCH 3/7] tests for version creator --- .../Builder/DownstreamReRouteBuilder.cs | 6 ++-- .../Configuration/Creator/IVersionCreator.cs | 9 +++++ .../Configuration/Creator/ReRoutesCreator.cs | 9 +++-- .../Configuration/Creator/VersionCreator.cs | 17 ++++++++++ src/Ocelot/Configuration/DownstreamReRoute.cs | 4 +-- .../DependencyInjection/OcelotBuilder.cs | 1 + src/Ocelot/Request/Mapper/RequestMapper.cs | 7 +--- .../Configuration/ReRoutesCreatorTests.cs | 10 +++++- .../Configuration/VersionCreatorTests.cs | 34 +++++++++++++++++++ 9 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 src/Ocelot/Configuration/Creator/IVersionCreator.cs create mode 100644 src/Ocelot/Configuration/Creator/VersionCreator.cs create mode 100644 test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index c318b949..09f5c59a 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -43,7 +43,7 @@ namespace Ocelot.Configuration.Builder private bool _dangerousAcceptAnyServerCertificateValidator; private SecurityOptions _securityOptions; private string _downstreamHttpMethod; - private string _downstreamHttpVersion; + private Version _downstreamHttpVersion; public DownstreamReRouteBuilder() { @@ -257,9 +257,9 @@ namespace Ocelot.Configuration.Builder return this; } - public DownstreamReRouteBuilder WithHttpVersion(string httpVersion) + public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion) { - _downstreamHttpVersion = httpVersion; + _downstreamHttpVersion = downstreamHttpVersion; return this; } diff --git a/src/Ocelot/Configuration/Creator/IVersionCreator.cs b/src/Ocelot/Configuration/Creator/IVersionCreator.cs new file mode 100644 index 00000000..d810bde3 --- /dev/null +++ b/src/Ocelot/Configuration/Creator/IVersionCreator.cs @@ -0,0 +1,9 @@ +namespace Ocelot.Configuration.Creator +{ + using System; + + public interface IVersionCreator + { + Version Create(string downstreamHttpVersion); + } +} diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index 63f42197..ad896ac9 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -22,6 +22,7 @@ namespace Ocelot.Configuration.Creator private readonly IDownstreamAddressesCreator _downstreamAddressesCreator; private readonly IReRouteKeyCreator _reRouteKeyCreator; private readonly ISecurityOptionsCreator _securityOptionsCreator; + private readonly IVersionCreator _versionCreator; public ReRoutesCreator( IClaimsToThingCreator claimsToThingCreator, @@ -37,7 +38,8 @@ namespace Ocelot.Configuration.Creator IDownstreamAddressesCreator downstreamAddressesCreator, ILoadBalancerOptionsCreator loadBalancerOptionsCreator, IReRouteKeyCreator reRouteKeyCreator, - ISecurityOptionsCreator securityOptionsCreator + ISecurityOptionsCreator securityOptionsCreator, + IVersionCreator versionCreator ) { _reRouteKeyCreator = reRouteKeyCreator; @@ -55,6 +57,7 @@ namespace Ocelot.Configuration.Creator _httpHandlerOptionsCreator = httpHandlerOptionsCreator; _loadBalancerOptionsCreator = loadBalancerOptionsCreator; _securityOptionsCreator = securityOptionsCreator; + _versionCreator = versionCreator; } public List Create(FileConfiguration fileConfiguration) @@ -104,6 +107,8 @@ namespace Ocelot.Configuration.Creator var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions); + var downstreamHttpVersion = _versionCreator.Create(fileReRoute.DownstreamHttpVersion); + var reRoute = new DownstreamReRouteBuilder() .WithKey(fileReRoute.Key) .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) @@ -138,7 +143,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) - .WithHttpVersion(fileReRoute.DownstreamHttpVersion) + .WithHttpVersion(downstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/src/Ocelot/Configuration/Creator/VersionCreator.cs b/src/Ocelot/Configuration/Creator/VersionCreator.cs new file mode 100644 index 00000000..b6fb3ad1 --- /dev/null +++ b/src/Ocelot/Configuration/Creator/VersionCreator.cs @@ -0,0 +1,17 @@ +namespace Ocelot.Configuration.Creator +{ + using System; + + public class VersionCreator : IVersionCreator + { + public Version Create(string downstreamHttpVersion) + { + if (!Version.TryParse(downstreamHttpVersion, out Version version)) + { + version = new Version(1, 1); + } + + return version; + } + } +} diff --git a/src/Ocelot/Configuration/DownstreamReRoute.cs b/src/Ocelot/Configuration/DownstreamReRoute.cs index d8a3cb74..be044539 100644 --- a/src/Ocelot/Configuration/DownstreamReRoute.cs +++ b/src/Ocelot/Configuration/DownstreamReRoute.cs @@ -41,7 +41,7 @@ namespace Ocelot.Configuration bool dangerousAcceptAnyServerCertificateValidator, SecurityOptions securityOptions, string downstreamHttpMethod, - string downstreamHttpVersion) + Version downstreamHttpVersion) { DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; AddHeadersToDownstream = addHeadersToDownstream; @@ -112,6 +112,6 @@ namespace Ocelot.Configuration public bool DangerousAcceptAnyServerCertificateValidator { get; } public SecurityOptions SecurityOptions { get; } public string DownstreamHttpMethod { get; } - public string DownstreamHttpVersion { get; } + public Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 7f92d50d..2f1444f6 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -136,6 +136,7 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); + Services.TryAddSingleton(); //add security this.AddSecurity(); diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index f7ad7c38..7d383a70 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -20,17 +20,12 @@ { try { - if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version)) - { - version = new Version(1, 1); - } - var requestMessage = new HttpRequestMessage() { Content = await MapContent(request), Method = MapMethod(request, downstreamReRoute), RequestUri = MapUri(request), - Version = version, + Version = downstreamReRoute.DownstreamHttpVersion, }; MapHeaders(request, requestMessage); diff --git a/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs index 32c235aa..47bc07c8 100644 --- a/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/ReRoutesCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Moq; using Ocelot.Cache; using Ocelot.Configuration; @@ -30,6 +31,7 @@ private Mock _lboCreator; private Mock _rrkCreator; private Mock _soCreator; + private Mock _versionCreator; private FileConfiguration _fileConfig; private ReRouteOptions _rro; private string _requestId; @@ -46,6 +48,7 @@ private LoadBalancerOptions _lbo; private List _result; private SecurityOptions _securityOptions; + private Version _expectedVersion; public ReRoutesCreatorTests() { @@ -63,6 +66,7 @@ _lboCreator = new Mock(); _rrkCreator = new Mock(); _soCreator = new Mock(); + _versionCreator = new Mock(); _creator = new ReRoutesCreator( _cthCreator.Object, @@ -78,7 +82,8 @@ _daCreator.Object, _lboCreator.Object, _rrkCreator.Object, - _soCreator.Object + _soCreator.Object, + _versionCreator.Object ); } @@ -155,6 +160,7 @@ private void GivenTheDependenciesAreSetUpCorrectly() { + _expectedVersion = new Version("1.1"); _rro = new ReRouteOptions(false, false, false, false, false); _requestId = "testy"; _rrk = "besty"; @@ -182,6 +188,7 @@ _hfarCreator.Setup(x => x.Create(It.IsAny())).Returns(_ht); _daCreator.Setup(x => x.Create(It.IsAny())).Returns(_dhp); _lboCreator.Setup(x => x.Create(It.IsAny())).Returns(_lbo); + _versionCreator.Setup(x => x.Create(It.IsAny())).Returns(_expectedVersion); } private void ThenTheReRoutesAreCreated() @@ -209,6 +216,7 @@ private void ThenTheReRouteIsSet(FileReRoute expected, int reRouteIndex) { + _result[reRouteIndex].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_expectedVersion); _result[reRouteIndex].DownstreamReRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated); _result[reRouteIndex].DownstreamReRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised); _result[reRouteIndex].DownstreamReRoute[0].IsCached.ShouldBe(_rro.IsCached); diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs new file mode 100644 index 00000000..346f62e7 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -0,0 +1,34 @@ +namespace Ocelot.UnitTests.Configuration +{ + using Ocelot.Configuration.Creator; + using Shouldly; + using Xunit; + + public class VersionCreatorTests + { + private readonly VersionCreator _creator; + + public VersionCreatorTests() + { + _creator = new VersionCreator(); + } + + [Fact] + public void should_create_version_based_on_input() + { + var input = "2.0"; + var result = _creator.Create(input); + result.Major.ShouldBe(2); + result.Minor.ShouldBe(0); + } + + [Fact] + public void should_default_to_version_one_point_one() + { + var input = ""; + var result = _creator.Create(input); + result.Major.ShouldBe(1); + result.Minor.ShouldBe(1); + } + } +} From e969e9ff0b999a700df9110b8a25d6b2f4ba1cf4 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Tue, 18 Feb 2020 20:45:28 +0000 Subject: [PATCH 4/7] unit tests passing --- .../Builder/DownstreamReRouteBuilder.cs | 2 +- .../Configuration/Creator/ReRoutesCreator.cs | 2 +- .../Configuration/VersionCreatorTests.cs | 36 ++++++++++++++----- .../Request/Mapper/RequestMapperTests.cs | 20 ++++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs index 09f5c59a..71049795 100644 --- a/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs +++ b/src/Ocelot/Configuration/Builder/DownstreamReRouteBuilder.cs @@ -257,7 +257,7 @@ namespace Ocelot.Configuration.Builder return this; } - public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion) + public DownstreamReRouteBuilder WithDownstreamHttpVersion(Version downstreamHttpVersion) { _downstreamHttpVersion = downstreamHttpVersion; return this; diff --git a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs index ad896ac9..e307b6d3 100644 --- a/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs +++ b/src/Ocelot/Configuration/Creator/ReRoutesCreator.cs @@ -143,7 +143,7 @@ namespace Ocelot.Configuration.Creator .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream) .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator) .WithSecurityOptions(securityOptions) - .WithHttpVersion(downstreamHttpVersion) + .WithDownstreamHttpVersion(downstreamHttpVersion) .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod) .Build(); diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs index 346f62e7..f5119ec8 100644 --- a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -1,12 +1,16 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Ocelot.Configuration.Creator; using Shouldly; + using TestStack.BDDfy; using Xunit; public class VersionCreatorTests { private readonly VersionCreator _creator; + private string _input; + private Version _result; public VersionCreatorTests() { @@ -16,19 +20,35 @@ [Fact] public void should_create_version_based_on_input() { - var input = "2.0"; - var result = _creator.Create(input); - result.Major.ShouldBe(2); - result.Minor.ShouldBe(0); + this.Given(_ => GivenTheInput("2.0")) + .When(_ => WhenICreate()) + .Then(_ => ThenTheResultIs(2, 0)) + .BDDfy(); } [Fact] public void should_default_to_version_one_point_one() { - var input = ""; - var result = _creator.Create(input); - result.Major.ShouldBe(1); - result.Minor.ShouldBe(1); + this.Given(_ => GivenTheInput("")) + .When(_ => WhenICreate()) + .Then(_ => ThenTheResultIs(1, 1)) + .BDDfy(); + } + + private void GivenTheInput(string input) + { + _input = input; + } + + private void WhenICreate() + { + _result = _creator.Create(_input); + } + + private void ThenTheResultIs(int major, int minor) + { + _result.Major.ShouldBe(major); + _result.Minor.ShouldBe(minor); } } } diff --git a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs index 77091fc6..501e290c 100644 --- a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs +++ b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs @@ -51,6 +51,7 @@ .And(_ => GivenTheInputRequestHasHost(host)) .And(_ => GivenTheInputRequestHasPath(path)) .And(_ => GivenTheInputRequestHasQueryString(queryString)) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasUri(expectedUri)) @@ -80,6 +81,7 @@ { this.Given(_ => GivenTheInputRequestHasMethod(method)) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasMethod(method)) @@ -107,6 +109,7 @@ this.Given(_ => GivenTheInputRequestHasHeaders()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasEachHeader()) @@ -119,6 +122,7 @@ this.Given(_ => GivenTheInputRequestHasNoHeaders()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoHeaders()) @@ -131,6 +135,7 @@ this.Given(_ => GivenTheInputRequestHasContent("This is my content")) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContent("This is my content")) @@ -143,6 +148,7 @@ this.Given(_ => GivenTheInputRequestHasNullContent()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -155,6 +161,7 @@ this.Given(_ => GivenTheInputRequestHasNoContentType()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -167,6 +174,7 @@ this.Given(_ => GivenTheInputRequestHasNoContentLength()) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasNoContent()) @@ -192,6 +200,7 @@ .And(_ => GivenTheContentMD5Is(md5bytes)) .And(_ => GivenTheInputRequestHasMethod("GET")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContentTypeHeader("application/json")) @@ -213,6 +222,7 @@ .And(_ => GivenTheContentTypeIs("application/json")) .And(_ => GivenTheInputRequestHasMethod("POST")) .And(_ => GivenTheInputRequestHasAValidUri()) + .And(_ => GivenTheDownstreamReRoute()) .When(_ => WhenMapped()) .Then(_ => ThenNoErrorIsReturned()) .And(_ => ThenTheMappedRequestHasContentTypeHeader("application/json")) @@ -223,7 +233,15 @@ private void GivenTheDownstreamReRouteMethodIs(string input) { - _downstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); + _downstreamReRoute = new DownstreamReRouteBuilder() + .WithDownStreamHttpMethod(input) + .WithDownstreamHttpVersion(new Version("1.1")).Build(); + } + + private void GivenTheDownstreamReRoute() + { + _downstreamReRoute = new DownstreamReRouteBuilder() + .WithDownstreamHttpVersion(new Version("1.1")).Build(); } private void GivenTheInputRequestHasNoContentLength() From 2772ce406dcba54a16d2e1bfbe5a82663ae17096 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Wed, 19 Feb 2020 09:18:01 +0000 Subject: [PATCH 5/7] all acceptance tests passing --- .../Creator/ConfigurationCreator.cs | 10 +++++-- .../Configuration/Creator/DynamicsCreator.cs | 7 ++++- .../Configuration/File/FileDynamicReRoute.cs | 1 + .../File/FileGlobalConfiguration.cs | 2 ++ .../Configuration/IInternalConfiguration.cs | 4 +++ .../Configuration/InternalConfiguration.cs | 8 +++++- .../Finder/DownstreamRouteCreator.cs | 2 ++ .../LoadBalancerTests.cs | 2 +- test/Ocelot.AcceptanceTests/Steps.cs | 1 + ...wnstreamRouteFinderMiddlewareBenchmarks.cs | 2 +- .../ConfigurationCreatorTests.cs | 4 ++- .../Configuration/DynamicsCreatorTests.cs | 28 +++++++++++++++++-- .../FileConfigurationSetterTests.cs | 4 ++- .../FileInternalConfigurationCreatorTests.cs | 2 +- .../InMemoryConfigurationRepositoryTests.cs | 1 + .../DownstreamRouteCreatorTests.cs | 23 +++++++-------- .../DownstreamRouteFinderMiddlewareTests.cs | 3 +- .../DownstreamRouteFinderTests.cs | 4 ++- .../DownstreamRouteProviderFactoryTests.cs | 5 ++-- .../DownstreamUrlCreatorMiddlewareTests.cs | 2 +- .../Errors/ExceptionHandlerMiddlewareTests.cs | 8 +++--- ...ekaMiddlewareConfigurationProviderTests.cs | 4 +-- .../LoadBalancerMiddlewareTests.cs | 2 +- 23 files changed, 94 insertions(+), 35 deletions(-) diff --git a/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs index 1d5abc32..b3cced34 100644 --- a/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/ConfigurationCreator.cs @@ -13,13 +13,15 @@ namespace Ocelot.Configuration.Creator private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; private readonly IAdministrationPath _adminPath; private readonly ILoadBalancerOptionsCreator _loadBalancerOptionsCreator; + private readonly IVersionCreator _versionCreator; public ConfigurationCreator( IServiceProviderConfigurationCreator serviceProviderConfigCreator, IQoSOptionsCreator qosOptionsCreator, IHttpHandlerOptionsCreator httpHandlerOptionsCreator, IServiceProvider serviceProvider, - ILoadBalancerOptionsCreator loadBalancerOptionsCreator + ILoadBalancerOptionsCreator loadBalancerOptionsCreator, + IVersionCreator versionCreator ) { _adminPath = serviceProvider.GetService(); @@ -27,6 +29,7 @@ namespace Ocelot.Configuration.Creator _serviceProviderConfigCreator = serviceProviderConfigCreator; _qosOptionsCreator = qosOptionsCreator; _httpHandlerOptionsCreator = httpHandlerOptionsCreator; + _versionCreator = versionCreator; } public InternalConfiguration Create(FileConfiguration fileConfiguration, List reRoutes) @@ -41,6 +44,8 @@ namespace Ocelot.Configuration.Creator var adminPath = _adminPath != null ? _adminPath.Path : null; + var version = _versionCreator.Create(fileConfiguration.GlobalConfiguration.DownstreamHttpVersion); + return new InternalConfiguration(reRoutes, adminPath, serviceProviderConfiguration, @@ -48,7 +53,8 @@ namespace Ocelot.Configuration.Creator lbOptions, fileConfiguration.GlobalConfiguration.DownstreamScheme, qosOptions, - httpHandlerOptions + httpHandlerOptions, + version ); } } diff --git a/src/Ocelot/Configuration/Creator/DynamicsCreator.cs b/src/Ocelot/Configuration/Creator/DynamicsCreator.cs index 2b5193c0..57b86746 100644 --- a/src/Ocelot/Configuration/Creator/DynamicsCreator.cs +++ b/src/Ocelot/Configuration/Creator/DynamicsCreator.cs @@ -8,10 +8,12 @@ namespace Ocelot.Configuration.Creator public class DynamicsCreator : IDynamicsCreator { private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator; + private readonly IVersionCreator _versionCreator; - public DynamicsCreator(IRateLimitOptionsCreator rateLimitOptionsCreator) + public DynamicsCreator(IRateLimitOptionsCreator rateLimitOptionsCreator, IVersionCreator versionCreator) { _rateLimitOptionsCreator = rateLimitOptionsCreator; + _versionCreator = versionCreator; } public List Create(FileConfiguration fileConfiguration) @@ -26,10 +28,13 @@ namespace Ocelot.Configuration.Creator var rateLimitOption = _rateLimitOptionsCreator .Create(fileDynamicReRoute.RateLimitRule, globalConfiguration); + var version = _versionCreator.Create(fileDynamicReRoute.DownstreamHttpVersion); + var downstreamReRoute = new DownstreamReRouteBuilder() .WithEnableRateLimiting(rateLimitOption.EnableRateLimiting) .WithRateLimitOptions(rateLimitOption) .WithServiceName(fileDynamicReRoute.ServiceName) + .WithDownstreamHttpVersion(version) .Build(); var reRoute = new ReRouteBuilder() diff --git a/src/Ocelot/Configuration/File/FileDynamicReRoute.cs b/src/Ocelot/Configuration/File/FileDynamicReRoute.cs index 26d8b4d4..7657aa89 100644 --- a/src/Ocelot/Configuration/File/FileDynamicReRoute.cs +++ b/src/Ocelot/Configuration/File/FileDynamicReRoute.cs @@ -4,5 +4,6 @@ namespace Ocelot.Configuration.File { public string ServiceName { get; set; } public FileRateLimitRule RateLimitRule { get; set; } + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index 9844c15d..6692ba04 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -26,5 +26,7 @@ public string DownstreamScheme { get; set; } public FileHttpHandlerOptions HttpHandlerOptions { get; set; } + + public string DownstreamHttpVersion { get; set; } } } diff --git a/src/Ocelot/Configuration/IInternalConfiguration.cs b/src/Ocelot/Configuration/IInternalConfiguration.cs index 1780da33..0925265f 100644 --- a/src/Ocelot/Configuration/IInternalConfiguration.cs +++ b/src/Ocelot/Configuration/IInternalConfiguration.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; namespace Ocelot.Configuration { + using System; + public interface IInternalConfiguration { List ReRoutes { get; } @@ -19,5 +21,7 @@ namespace Ocelot.Configuration QoSOptions QoSOptions { get; } HttpHandlerOptions HttpHandlerOptions { get; } + + Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/Configuration/InternalConfiguration.cs b/src/Ocelot/Configuration/InternalConfiguration.cs index 7b7649e4..7a398982 100644 --- a/src/Ocelot/Configuration/InternalConfiguration.cs +++ b/src/Ocelot/Configuration/InternalConfiguration.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; namespace Ocelot.Configuration { + using System; + public class InternalConfiguration : IInternalConfiguration { public InternalConfiguration( @@ -12,7 +14,8 @@ namespace Ocelot.Configuration LoadBalancerOptions loadBalancerOptions, string downstreamScheme, QoSOptions qoSOptions, - HttpHandlerOptions httpHandlerOptions) + HttpHandlerOptions httpHandlerOptions, + Version downstreamHttpVersion) { ReRoutes = reRoutes; AdministrationPath = administrationPath; @@ -22,6 +25,7 @@ namespace Ocelot.Configuration DownstreamScheme = downstreamScheme; QoSOptions = qoSOptions; HttpHandlerOptions = httpHandlerOptions; + DownstreamHttpVersion = downstreamHttpVersion; } public List ReRoutes { get; } @@ -32,5 +36,7 @@ namespace Ocelot.Configuration public string DownstreamScheme { get; } public QoSOptions QoSOptions { get; } public HttpHandlerOptions HttpHandlerOptions { get; } + + public Version DownstreamHttpVersion { get; } } } diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs index ec6821ad..b2b1db95 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs @@ -1,5 +1,6 @@ namespace Ocelot.DownstreamRouteFinder.Finder { + using System; using Configuration; using Configuration.Builder; using Configuration.Creator; @@ -54,6 +55,7 @@ .WithQosOptions(qosOptions) .WithDownstreamScheme(configuration.DownstreamScheme) .WithLoadBalancerOptions(configuration.LoadBalancerOptions) + .WithDownstreamHttpVersion(configuration.DownstreamHttpVersion) .WithUpstreamPathTemplate(upstreamPathTemplate); var rateLimitOptions = configuration.ReRoutes != null diff --git a/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs b/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs index b3adf531..5ce9c1e2 100644 --- a/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs +++ b/test/Ocelot.AcceptanceTests/LoadBalancerTests.cs @@ -27,7 +27,7 @@ namespace Ocelot.AcceptanceTests public void should_load_balance_request_with_least_connection() { int portOne = 50591; - int portTwo = 51482; + int portTwo = 54483; var downstreamServiceOneUrl = $"http://localhost:{portOne}"; var downstreamServiceTwoUrl = $"http://localhost:{portTwo}"; diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index e8aee690..976b7422 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -392,6 +392,7 @@ namespace Ocelot.AcceptanceTests _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); + Thread.Sleep(1000); } public void WhenIGetUrlOnTheApiGatewayWaitingForTheResponseToBeOk(string url) diff --git a/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs b/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs index 57f7386a..55869213 100644 --- a/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs +++ b/test/Ocelot.Benchmarks/DownstreamRouteFinderMiddlewareBenchmarks.cs @@ -59,7 +59,7 @@ namespace Ocelot.Benchmarks _downstreamContext = new DownstreamContext(httpContext) { - Configuration = new InternalConfiguration(new List(), null, null, null, null, null, null, null) + Configuration = new InternalConfiguration(new List(), null, null, null, null, null, null, null, null) }; } diff --git a/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs index a51ec161..68516ad6 100644 --- a/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/ConfigurationCreatorTests.cs @@ -19,6 +19,7 @@ namespace Ocelot.UnitTests.Configuration private readonly Mock _qosCreator; private readonly Mock _hhoCreator; private readonly Mock _lboCreator; + private readonly Mock _vCreator; private FileConfiguration _fileConfig; private List _reRoutes; private ServiceProviderConfiguration _spc; @@ -30,6 +31,7 @@ namespace Ocelot.UnitTests.Configuration public ConfigurationCreatorTests() { + _vCreator = new Mock(); _lboCreator = new Mock(); _hhoCreator = new Mock(); _qosCreator = new Mock(); @@ -117,7 +119,7 @@ namespace Ocelot.UnitTests.Configuration private void WhenICreate() { var serviceProvider = _serviceCollection.BuildServiceProvider(); - _creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object); + _creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object, _vCreator.Object); _result = _creator.Create(_fileConfig, _reRoutes); } } diff --git a/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs index 8a0601d3..b502aa73 100644 --- a/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.Configuration { + using System; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; @@ -14,15 +15,18 @@ { private readonly DynamicsCreator _creator; private readonly Mock _rloCreator; + private readonly Mock _versionCreator; private List _result; private FileConfiguration _fileConfig; private RateLimitOptions _rlo1; private RateLimitOptions _rlo2; + private Version _version; public DynamicsCreatorTests() { + _versionCreator = new Mock(); _rloCreator = new Mock(); - _creator = new DynamicsCreator(_rloCreator.Object); + _creator = new DynamicsCreator(_rloCreator.Object, _versionCreator.Object); } [Fact] @@ -50,7 +54,8 @@ RateLimitRule = new FileRateLimitRule { EnableRateLimiting = false - } + }, + DownstreamHttpVersion = "1.1" }, new FileDynamicReRoute { @@ -58,16 +63,19 @@ RateLimitRule = new FileRateLimitRule { EnableRateLimiting = true - } + }, + DownstreamHttpVersion = "2.0" } } }; this.Given(_ => GivenThe(fileConfig)) .And(_ => GivenTheRloCreatorReturns()) + .And(_ => GivenTheVersionCreatorReturns()) .When(_ => WhenICreate()) .Then(_ => ThenTheReRoutesAreReturned()) .And(_ => ThenTheRloCreatorIsCalledCorrectly()) + .And(_ => ThenTheVersionCreatorIsCalledCorrectly()) .BDDfy(); } @@ -80,18 +88,32 @@ _fileConfig.GlobalConfiguration), Times.Once); } + private void ThenTheVersionCreatorIsCalledCorrectly() + { + _versionCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[0].DownstreamHttpVersion), Times.Once); + _versionCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[1].DownstreamHttpVersion), Times.Once); + } + private void ThenTheReRoutesAreReturned() { _result.Count.ShouldBe(2); _result[0].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeFalse(); _result[0].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo1); + _result[0].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_version); _result[0].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[0].ServiceName); _result[1].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeTrue(); _result[1].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo2); + _result[1].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_version); _result[1].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[1].ServiceName); } + private void GivenTheVersionCreatorReturns() + { + _version = new Version("1.1"); + _versionCreator.Setup(x => x.Create(It.IsAny())).Returns(_version); + } + private void GivenTheRloCreatorReturns() { _rlo1 = new RateLimitOptionsBuilder().Build(); diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs index 85fdd53c..c647f410 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs @@ -15,6 +15,8 @@ using Xunit; namespace Ocelot.UnitTests.Configuration { + using System; + public class FileConfigurationSetterTests { private FileConfiguration _fileConfiguration; @@ -38,7 +40,7 @@ namespace Ocelot.UnitTests.Configuration { var fileConfig = new FileConfiguration(); var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build(); - var config = new InternalConfiguration(new List(), string.Empty, serviceProviderConfig, "asdf", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + var config = new InternalConfiguration(new List(), string.Empty, serviceProviderConfig, "asdf", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); this.Given(x => GivenTheFollowingConfiguration(fileConfig)) .And(x => GivenTheRepoReturns(new OkResponse())) diff --git a/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs index 6ccc4448..ccf3a677 100644 --- a/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileInternalConfigurationCreatorTests.cs @@ -87,7 +87,7 @@ _reRoutes = new List { new ReRouteBuilder().Build() }; _aggregates = new List { new ReRouteBuilder().Build() }; _dynamics = new List { new ReRouteBuilder().Build() }; - _internalConfig = new InternalConfiguration(null, "", null, "", null, "", null, null); + _internalConfig = new InternalConfiguration(null, "", null, "", null, "", null, null, null); _reRoutesCreator.Setup(x => x.Create(It.IsAny())).Returns(_reRoutes); _aggregatesCreator.Setup(x => x.Create(It.IsAny(), It.IsAny>())).Returns(_aggregates); diff --git a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs index 508f25e0..4639837e 100644 --- a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs @@ -120,6 +120,7 @@ namespace Ocelot.UnitTests.Configuration public string DownstreamScheme { get; } public QoSOptions QoSOptions { get; } public HttpHandlerOptions HttpHandlerOptions { get; } + public Version DownstreamHttpVersion { get; } } } } diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs index a3348627..fd8190ea 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; @@ -44,7 +45,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_create_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -71,7 +72,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder var reRoutes = new List { reRoute }; - var configuration = new InternalConfiguration(reRoutes, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(reRoutes, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -83,7 +84,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_cache_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, "/geoffisthebest/")) .When(_ => WhenICreate()) @@ -96,7 +97,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_not_cache_downstream_route() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, "/geoffistheworst/")) .When(_ => WhenICreate()) @@ -110,7 +111,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_no_path() { var upstreamUrlPath = "/auth/"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -122,7 +123,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_only_first_segment_no_traling_slash() { var upstreamUrlPath = "/auth"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -134,7 +135,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_with_segments_no_traling_slash() { var upstreamUrlPath = "/auth/test"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -146,7 +147,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_and_remove_query_string() { var upstreamUrlPath = "/auth/test?test=1&best=2"; - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration, upstreamUrlPath)) .When(_ => WhenICreate()) @@ -158,7 +159,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder public void should_create_downstream_route_for_sticky_sessions() { var loadBalancerOptions = new LoadBalancerOptionsBuilder().WithType(nameof(CookieStickySessions)).WithKey("boom").WithExpiryInMs(1).Build(); - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) @@ -174,7 +175,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .WithTimeoutValue(1) .Build(); - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .And(_ => GivenTheQosCreatorReturns(qoSOptions)) @@ -186,7 +187,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder [Fact] public void should_create_downstream_route_with_handler_options() { - var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions); + var configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter", _loadBalancerOptions, "http", _qoSOptions, _handlerOptions, new Version("1.1")); this.Given(_ => GivenTheConfiguration(configuration)) .When(_ => WhenICreate()) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs index 3b333258..12e0a804 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Microsoft.AspNetCore.Http; using Moq; using Ocelot.Configuration; @@ -48,7 +49,7 @@ [Fact] public void should_call_scoped_data_repository_correctly() { - var config = new InternalConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + var config = new InternalConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); var downstreamReRoute = new DownstreamReRouteBuilder() .WithDownstreamPathTemplate("any old string") diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index 7419fe55..5f9e1a50 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -13,6 +13,8 @@ using Xunit; namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; + public class DownstreamRouteFinderTests { private readonly IDownstreamRouteProvider _downstreamRouteFinder; @@ -739,7 +741,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void GivenTheConfigurationIs(List reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig) { _reRoutesConfig = reRoutesConfig; - _config = new InternalConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs index 437033f7..ac8f5e0a 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs @@ -1,5 +1,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { + using System; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration; @@ -140,12 +141,12 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void GivenTheReRoutes(List reRoutes) { - _config = new InternalConfiguration(reRoutes, "", null, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(reRoutes, "", null, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } private void GivenTheReRoutes(List reRoutes, ServiceProviderConfiguration config) { - _config = new InternalConfiguration(reRoutes, "", config, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build()); + _config = new InternalConfiguration(reRoutes, "", config, "", new LoadBalancerOptionsBuilder().Build(), "", new QoSOptionsBuilder().Build(), new HttpHandlerOptionsBuilder().Build(), new Version("1.1")); } } } diff --git a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs index 7ed6a02a..f6e0a2d5 100644 --- a/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamUrlCreator/DownstreamUrlCreatorMiddlewareTests.cs @@ -382,7 +382,7 @@ private void GivenTheServiceProviderConfigIs(ServiceProviderConfiguration config) { - var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null); + var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null); _downstreamContext.Configuration = configuration; } diff --git a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs index 3d86505e..932f40b2 100644 --- a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs @@ -52,7 +52,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void NoDownstreamException() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -65,7 +65,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void DownstreamException() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -77,7 +77,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void ShouldSetRequestId() { - var config = new InternalConfiguration(null, null, null, "requestidkey", null, null, null, null); + var config = new InternalConfiguration(null, null, null, "requestidkey", null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) @@ -90,7 +90,7 @@ namespace Ocelot.UnitTests.Errors [Fact] public void ShouldSetAspDotNetRequestId() { - var config = new InternalConfiguration(null, null, null, null, null, null, null, null); + var config = new InternalConfiguration(null, null, null, null, null, null, null, null, null); this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) .And(_ => GivenTheConfigurationIs(config)) diff --git a/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs index fc7f3c82..5a6a5306 100644 --- a/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Eureka/EurekaMiddlewareConfigurationProviderTests.cs @@ -20,7 +20,7 @@ { var configRepo = new Mock(); configRepo.Setup(x => x.Get()) - .Returns(new OkResponse(new InternalConfiguration(null, null, null, null, null, null, null, null))); + .Returns(new OkResponse(new InternalConfiguration(null, null, null, null, null, null, null, null, null))); var services = new ServiceCollection(); services.AddSingleton(configRepo.Object); var sp = services.BuildServiceProvider(); @@ -35,7 +35,7 @@ var client = new Mock(); var configRepo = new Mock(); configRepo.Setup(x => x.Get()) - .Returns(new OkResponse(new InternalConfiguration(null, null, serviceProviderConfig, null, null, null, null, null))); + .Returns(new OkResponse(new InternalConfiguration(null, null, serviceProviderConfig, null, null, null, null, null, null))); var services = new ServiceCollection(); services.AddSingleton(configRepo.Object); services.AddSingleton(client.Object); diff --git a/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs b/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs index 2705b3b7..1a6202cf 100644 --- a/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/LoadBalancer/LoadBalancerMiddlewareTests.cs @@ -139,7 +139,7 @@ namespace Ocelot.UnitTests.LoadBalancer private void GivenTheConfigurationIs(ServiceProviderConfiguration config) { _config = config; - var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null); + var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null); _downstreamContext.Configuration = configuration; } From ec8a5b6ddf65572d93fdc0030c63fb51123e0732 Mon Sep 17 00:00:00 2001 From: TomPallister Date: Fri, 21 Feb 2020 16:49:26 +0000 Subject: [PATCH 6/7] finished tests around http2 --- ...ersionCreator.cs => HttpVersionCreator.cs} | 2 +- .../DependencyInjection/OcelotBuilder.cs | 2 +- test/Ocelot.AcceptanceTests/HttpTests.cs | 205 ++++++++++++++++++ test/Ocelot.AcceptanceTests/ServiceHandler.cs | 27 +++ .../Configuration/VersionCreatorTests.cs | 4 +- 5 files changed, 236 insertions(+), 4 deletions(-) rename src/Ocelot/Configuration/Creator/{VersionCreator.cs => HttpVersionCreator.cs} (86%) create mode 100644 test/Ocelot.AcceptanceTests/HttpTests.cs diff --git a/src/Ocelot/Configuration/Creator/VersionCreator.cs b/src/Ocelot/Configuration/Creator/HttpVersionCreator.cs similarity index 86% rename from src/Ocelot/Configuration/Creator/VersionCreator.cs rename to src/Ocelot/Configuration/Creator/HttpVersionCreator.cs index b6fb3ad1..f80f98ff 100644 --- a/src/Ocelot/Configuration/Creator/VersionCreator.cs +++ b/src/Ocelot/Configuration/Creator/HttpVersionCreator.cs @@ -2,7 +2,7 @@ { using System; - public class VersionCreator : IVersionCreator + public class HttpVersionCreator : IVersionCreator { public Version Create(string downstreamHttpVersion) { diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 2f1444f6..0336f691 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -136,7 +136,7 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); - Services.TryAddSingleton(); + Services.TryAddSingleton(); //add security this.AddSecurity(); diff --git a/test/Ocelot.AcceptanceTests/HttpTests.cs b/test/Ocelot.AcceptanceTests/HttpTests.cs new file mode 100644 index 00000000..89ab12f4 --- /dev/null +++ b/test/Ocelot.AcceptanceTests/HttpTests.cs @@ -0,0 +1,205 @@ +namespace Ocelot.AcceptanceTests +{ + using Microsoft.AspNetCore.Http; + using Ocelot.Configuration.File; + using System; + using System.Collections.Generic; + using System.IO; + using System.Net; + using System.Net.Http; + using Microsoft.AspNetCore.Server.Kestrel.Core; + using TestStack.BDDfy; + using Xunit; + + public class HttpTests : IDisposable + { + private readonly Steps _steps; + private readonly ServiceHandler _serviceHandler; + + public HttpTests() + { + _serviceHandler = new ServiceHandler(); + _steps = new Steps(); + } + + [Fact] + public void should_return_response_200_when_using_http_one_point_one() + { + const int port = 53279; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.1", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_using_http_two_point_zero() + { + const int port = 53675; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "2.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + [Fact] + public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two() + { + const int port = 53677; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.1", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError)) + .BDDfy(); + } + + [Fact] + public void should_return_response_200_when_using_http_two_to_talk_to_server_running_http_one_point_one() + { + const int port = 53679; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "2.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + const string expected = "here is some content"; + var httpContent = new StringContent(expected); + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) + .BDDfy(); + } + + private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int port, HttpProtocols protocols) + { + _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context => + { + context.Response.StatusCode = 200; + var reader = new StreamReader(context.Request.Body); + var body = await reader.ReadToEndAsync(); + await context.Response.WriteAsync(body); + }, port, protocols); + } + + public void Dispose() + { + _serviceHandler.Dispose(); + _steps.Dispose(); + } + } +} diff --git a/test/Ocelot.AcceptanceTests/ServiceHandler.cs b/test/Ocelot.AcceptanceTests/ServiceHandler.cs index c10c122b..9fc8b0f5 100644 --- a/test/Ocelot.AcceptanceTests/ServiceHandler.cs +++ b/test/Ocelot.AcceptanceTests/ServiceHandler.cs @@ -6,9 +6,11 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; + using System.ComponentModel; using System.IO; using System.Net; using System.Threading.Tasks; + using Microsoft.AspNetCore.Server.Kestrel.Core; public class ServiceHandler : IDisposable { @@ -47,6 +49,31 @@ _builder.Start(); } + public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate del, int port, HttpProtocols protocols) + { + _builder = new WebHostBuilder() + .UseUrls(baseUrl) + .UseKestrel() + .ConfigureKestrel(serverOptions => + { + serverOptions.Listen(IPAddress.Loopback, port, listenOptions => + { + listenOptions.UseHttps("idsrv3test.pfx", "idsrv3test"); + listenOptions.Protocols = protocols; + }); + }) + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .Configure(app => + { + app.UsePathBase(basePath); + app.Run(del); + }) + .Build(); + + _builder.Start(); + } + public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string fileName, string password, int port, RequestDelegate del) { _builder = new WebHostBuilder() diff --git a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs index f5119ec8..bb440c5c 100644 --- a/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs @@ -8,13 +8,13 @@ public class VersionCreatorTests { - private readonly VersionCreator _creator; + private readonly HttpVersionCreator _creator; private string _input; private Version _result; public VersionCreatorTests() { - _creator = new VersionCreator(); + _creator = new HttpVersionCreator(); } [Fact] From ede5650a3cf01fc37cf39ea5bd66220011ef89af Mon Sep 17 00:00:00 2001 From: TomPallister Date: Fri, 21 Feb 2020 16:53:03 +0000 Subject: [PATCH 7/7] docs --- docs/features/configuration.rst | 8 ++++- test/Ocelot.AcceptanceTests/HttpTests.cs | 38 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/features/configuration.rst b/docs/features/configuration.rst index f9cb7841..ebd82203 100644 --- a/docs/features/configuration.rst +++ b/docs/features/configuration.rst @@ -25,6 +25,7 @@ Here is an example ReRoute configuration, You don't need to set all of these thi "Get" ], "DownstreamHttpMethod": "", + "DownstreamHttpVersion": "", "AddHeadersToRequest": {}, "AddClaimsToRequest": {}, "RouteClaimsRequirement": {}, @@ -278,4 +279,9 @@ Registering a callback { _callbackHolder.Dispose(); } - } \ No newline at end of file + } + +DownstreamHttpVersion +--------------------- + +Ocelot allows you to choose the HTTP version it will use to make the proxy request. It can be set as "1.0", "1.1" or "2.0". \ No newline at end of file diff --git a/test/Ocelot.AcceptanceTests/HttpTests.cs b/test/Ocelot.AcceptanceTests/HttpTests.cs index 89ab12f4..b63ad4bb 100644 --- a/test/Ocelot.AcceptanceTests/HttpTests.cs +++ b/test/Ocelot.AcceptanceTests/HttpTests.cs @@ -22,6 +22,44 @@ namespace Ocelot.AcceptanceTests _steps = new Steps(); } + [Fact] + public void should_return_response_200_when_using_http_one() + { + const int port = 53219; + + var configuration = new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/{url}", + DownstreamScheme = "https", + UpstreamPathTemplate = "/{url}", + UpstreamHttpMethod = new List { "Get" }, + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "localhost", + Port = port, + }, + }, + DownstreamHttpMethod = "POST", + DownstreamHttpVersion = "1.0", + DangerousAcceptAnyServerCertificateValidator = true + }, + }, + }; + + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) + .And(x => _steps.GivenThereIsAConfiguration(configuration)) + .And(x => _steps.GivenOcelotIsRunning()) + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + [Fact] public void should_return_response_200_when_using_http_one_point_one() {