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); + } + } +}