Skip to content

Commit

Permalink
feature written, tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnuda committed Jun 30, 2023
1 parent 433dfca commit 859403d
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/Ocelot/Configuration/Builder/DownstreamRouteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DownstreamRouteBuilder
private SecurityOptions _securityOptions;
private string _downstreamHttpMethod;
private Version _downstreamHttpVersion;
private HttpVersionPolicy _downstreamVersionPolicy;

public DownstreamRouteBuilder()
{
Expand Down Expand Up @@ -263,6 +264,12 @@ public DownstreamRouteBuilder WithDownstreamHttpVersion(Version downstreamHttpVe
return this;
}

public DownstreamRouteBuilder WithDownstreamPolicyBuilder(HttpVersionPolicy downstreamVersionPolicy)
{
_downstreamVersionPolicy = downstreamVersionPolicy;
return this;
}

public DownstreamRoute Build()
{
return new DownstreamRoute(
Expand Down Expand Up @@ -299,6 +306,7 @@ public DownstreamRoute Build()
_dangerousAcceptAnyServerCertificateValidator,
_securityOptions,
_downstreamHttpMethod,
_downstreamHttpVersion);
_downstreamHttpVersion,
_downstreamVersionPolicy);
}
}
14 changes: 14 additions & 0 deletions src/Ocelot/Configuration/Creator/IVersionPolicyCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Ocelot.Configuration.Creator
{
public interface IVersionPolicyCreator
{
HttpVersionPolicy Create(string downstreamVersionPolicy);
}
}
8 changes: 7 additions & 1 deletion src/Ocelot/Configuration/Creator/RoutesCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class RoutesCreator : IRoutesCreator
private readonly IRouteKeyCreator _routeKeyCreator;
private readonly ISecurityOptionsCreator _securityOptionsCreator;
private readonly IVersionCreator _versionCreator;
private readonly IVersionPolicyCreator _versionPolicyCreator;

public RoutesCreator(
IClaimsToThingCreator claimsToThingCreator,
Expand All @@ -42,7 +43,8 @@ public RoutesCreator(
ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
IRouteKeyCreator routeKeyCreator,
ISecurityOptionsCreator securityOptionsCreator,
IVersionCreator versionCreator
IVersionCreator versionCreator,
IVersionPolicyCreator versionPolicyCreator
)
{
_routeKeyCreator = routeKeyCreator;
Expand All @@ -61,6 +63,7 @@ IVersionCreator versionCreator
_loadBalancerOptionsCreator = loadBalancerOptionsCreator;
_securityOptionsCreator = securityOptionsCreator;
_versionCreator = versionCreator;
_versionPolicyCreator = versionPolicyCreator;
}

public List<Route> Create(FileConfiguration fileConfiguration)
Expand Down Expand Up @@ -111,6 +114,8 @@ private DownstreamRoute SetUpDownstreamRoute(FileRoute fileRoute, FileGlobalConf
var securityOptions = _securityOptionsCreator.Create(fileRoute.SecurityOptions);

var downstreamHttpVersion = _versionCreator.Create(fileRoute.DownstreamHttpVersion);

var downstreamVersionPolicy = _versionPolicyCreator.Create(fileRoute.DownstreamVersionPolicy);

var route = new DownstreamRouteBuilder()
.WithKey(fileRoute.Key)
Expand Down Expand Up @@ -147,6 +152,7 @@ private DownstreamRoute SetUpDownstreamRoute(FileRoute fileRoute, FileGlobalConf
.WithDangerousAcceptAnyServerCertificateValidator(fileRoute.DangerousAcceptAnyServerCertificateValidator)
.WithSecurityOptions(securityOptions)
.WithDownstreamHttpVersion(downstreamHttpVersion)
.WithDownstreamPolicyBuilder(downstreamVersionPolicy)
.WithDownStreamHttpMethod(fileRoute.DownstreamHttpMethod)
.Build();

Expand Down
18 changes: 18 additions & 0 deletions src/Ocelot/Configuration/Creator/VersionPolicyCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net.Http;

namespace Ocelot.Configuration.Creator
{
public class VersionPolicyCreator : IVersionPolicyCreator
{
public HttpVersionPolicy Create(string downstreamVersionPolicy)
{
return downstreamVersionPolicy switch
{
"exact" => HttpVersionPolicy.RequestVersionExact,
"upgradeable" => HttpVersionPolicy.RequestVersionOrHigher,
"downgradeable" => HttpVersionPolicy.RequestVersionOrLower,
_ => HttpVersionPolicy.RequestVersionOrLower,
};
}
}
}
9 changes: 6 additions & 3 deletions src/Ocelot/Configuration/DownstreamRoute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;

using System.Net.Http;
using Ocelot.Configuration.Creator;

using Ocelot.Values;
Expand Down Expand Up @@ -43,7 +43,8 @@ public DownstreamRoute(
bool dangerousAcceptAnyServerCertificateValidator,
SecurityOptions securityOptions,
string downstreamHttpMethod,
Version downstreamHttpVersion)
Version downstreamHttpVersion,
HttpVersionPolicy downstreamVersionPolicy)
{
DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
AddHeadersToDownstream = addHeadersToDownstream;
Expand Down Expand Up @@ -79,6 +80,7 @@ public DownstreamRoute(
SecurityOptions = securityOptions;
DownstreamHttpMethod = downstreamHttpMethod;
DownstreamHttpVersion = downstreamHttpVersion;
DownstreamVersionPolicy = downstreamVersionPolicy;
}

public string Key { get; }
Expand Down Expand Up @@ -114,6 +116,7 @@ public DownstreamRoute(
public bool DangerousAcceptAnyServerCertificateValidator { get; }
public SecurityOptions SecurityOptions { get; }
public string DownstreamHttpMethod { get; }
public Version DownstreamHttpVersion { get; }
public Version DownstreamHttpVersion { get; }
public HttpVersionPolicy DownstreamVersionPolicy { get; }
}
}
1 change: 1 addition & 0 deletions src/Ocelot/Configuration/File/FileRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ public FileRoute()
public bool DangerousAcceptAnyServerCertificateValidator { get; set; }
public FileSecurityOptions SecurityOptions { get; set; }
public string DownstreamHttpVersion { get; set; }
public string DownstreamVersionPolicy { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Ocelot/Configuration/Validator/RouteFluentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public RouteFluentValidator(IAuthenticationSchemeProvider authenticationSchemePr
{
RuleFor(r => r.DownstreamHttpVersion).Matches("^[0-9]([.,][0-9]{1,1})?$");
});

When(route => !string.IsNullOrEmpty(route.DownstreamVersionPolicy), () =>
{
RuleFor(r => r.DownstreamVersionPolicy).Matches("^(exact|upgradeable|downgradeable)$");
});
}

private async Task<bool> IsSupportedAuthenticationProviders(FileAuthenticationOptions authenticationOptions, CancellationToken cancellationToken)
Expand Down
1 change: 1 addition & 0 deletions src/Ocelot/Request/Mapper/RequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public async Task<Response<HttpRequestMessage>> Map(HttpRequest request, Downstr
Method = MapMethod(request, downstreamRoute),
RequestUri = MapUri(request),
Version = downstreamRoute.DownstreamHttpVersion,
VersionPolicy = downstreamRoute.DownstreamVersionPolicy,
};

MapHeaders(request, requestMessage);
Expand Down
2 changes: 2 additions & 0 deletions src/Ocelot/Requester/HttpClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public IHttpClient Create(DownstreamRoute downstreamRoute)
_httpClient = new HttpClient(CreateHttpMessageHandler(handler, downstreamRoute))
{
Timeout = timeout,
DefaultRequestVersion = downstreamRoute.DownstreamHttpVersion,
DefaultVersionPolicy = downstreamRoute.DownstreamVersionPolicy,
};

_client = new HttpClientWrapper(_httpClient);
Expand Down
10 changes: 9 additions & 1 deletion test/Ocelot.UnitTests/Configuration/RoutesCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using TestStack.BDDfy;
using Xunit;

Expand All @@ -32,6 +33,7 @@ public class RoutesCreatorTests
private readonly Mock<IRouteKeyCreator> _rrkCreator;
private readonly Mock<ISecurityOptionsCreator> _soCreator;
private readonly Mock<IVersionCreator> _versionCreator;
private readonly Mock<IVersionPolicyCreator> _versionPolicyCreator;
private FileConfiguration _fileConfig;
private RouteOptions _rro;
private string _requestId;
Expand All @@ -48,6 +50,7 @@ public class RoutesCreatorTests
private LoadBalancerOptions _lbo;
private List<Route> _result;
private Version _expectedVersion;
private HttpVersionPolicy _expectedVersionPolicy;

public RoutesCreatorTests()
{
Expand All @@ -66,6 +69,7 @@ public RoutesCreatorTests()
_rrkCreator = new Mock<IRouteKeyCreator>();
_soCreator = new Mock<ISecurityOptionsCreator>();
_versionCreator = new Mock<IVersionCreator>();
_versionPolicyCreator = new Mock<IVersionPolicyCreator>();

_creator = new RoutesCreator(
_cthCreator.Object,
Expand All @@ -82,7 +86,8 @@ public RoutesCreatorTests()
_lboCreator.Object,
_rrkCreator.Object,
_soCreator.Object,
_versionCreator.Object
_versionCreator.Object,
_versionPolicyCreator.Object
);
}

Expand Down Expand Up @@ -160,6 +165,7 @@ private void ThenTheDependenciesAreCalledCorrectly()
private void GivenTheDependenciesAreSetUpCorrectly()
{
_expectedVersion = new Version("1.1");
_expectedVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
_rro = new RouteOptions(false, false, false, false, false);
_requestId = "testy";
_rrk = "besty";
Expand Down Expand Up @@ -188,6 +194,7 @@ private void GivenTheDependenciesAreSetUpCorrectly()
_daCreator.Setup(x => x.Create(It.IsAny<FileRoute>())).Returns(_dhp);
_lboCreator.Setup(x => x.Create(It.IsAny<FileLoadBalancerOptions>())).Returns(_lbo);
_versionCreator.Setup(x => x.Create(It.IsAny<string>())).Returns(_expectedVersion);
_versionPolicyCreator.Setup(x => x.Create(It.IsAny<string>())).Returns(_expectedVersionPolicy);
}

private void ThenTheRoutesAreCreated()
Expand Down Expand Up @@ -216,6 +223,7 @@ private void WhenICreate()
private void ThenTheRouteIsSet(FileRoute expected, int routeIndex)
{
_result[routeIndex].DownstreamRoute[0].DownstreamHttpVersion.ShouldBe(_expectedVersion);
_result[routeIndex].DownstreamRoute[0].DownstreamVersionPolicy.ShouldBe(_expectedVersionPolicy);
_result[routeIndex].DownstreamRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated);
_result[routeIndex].DownstreamRoute[0].IsAuthorized.ShouldBe(_rro.IsAuthorized);
_result[routeIndex].DownstreamRoute[0].IsCached.ShouldBe(_rro.IsCached);
Expand Down
62 changes: 62 additions & 0 deletions test/Ocelot.UnitTests/Configuration/VersionPolicyCreatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Ocelot.Configuration.Creator;
using System.Net.Http;
using Shouldly;
using TestStack.BDDfy;
using Xunit;

namespace Ocelot.UnitTests.Configuration
{
public class VersionPolicyCreatorTests
{
private readonly VersionPolicyCreator _creator;
private string _input;
private HttpVersionPolicy _result;

public VersionPolicyCreatorTests()
{
_creator = new VersionPolicyCreator();
}

[Fact]
public void should_create_version_policy_based_on_input()
{
this.Given(_ => GivenTheInput("upgradeable"))
.When(_ => WhenICreate())
.Then(_ => ThenTheResultIs(HttpVersionPolicy.RequestVersionOrHigher))
.BDDfy();
}

[Fact]
public void should_default_to_request_version_or_lower()
{
this.Given(_ => GivenTheInput(string.Empty))
.When(_ => WhenICreate())
.Then(_ => ThenTheResultIs(HttpVersionPolicy.RequestVersionOrLower))
.BDDfy();
}

[Fact]
public void should_default_to_request_version_or_lower_when_setting_gibberish()
{
this.Given(_ => GivenTheInput("string is gibberish"))
.When(_ => WhenICreate())
.Then(_ => ThenTheResultIs(HttpVersionPolicy.RequestVersionOrLower))
.BDDfy();
}

private void GivenTheInput(string input)
{
_input = input;
}

private void WhenICreate()
{
_result = _creator.Create(_input);
}

private void ThenTheResultIs(HttpVersionPolicy result)
{
_result.ShouldBe(result);
}
}
}

0 comments on commit 859403d

Please sign in to comment.