Merge pull request #139 from TomPallister/feature/fix-135

looking at #135 validation
This commit is contained in:
Tom Pallister 2017-11-02 12:34:13 +00:00 committed by GitHub
commit 1d1a68ff95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 122 additions and 17 deletions

View File

@ -0,0 +1,12 @@
using Ocelot.Errors;
namespace Ocelot.Configuration.Validator
{
public class PathTemplateDoesntStartWithForwardSlash : Error
{
public PathTemplateDoesntStartWithForwardSlash(string message)
: base(message, OcelotErrorCode.PathTemplateDoesntStartWithForwardSlash)
{
}
}
}

View File

@ -26,6 +26,20 @@ namespace Ocelot.Configuration.Validator
return new OkResponse<ConfigurationValidationResult>(result); return new OkResponse<ConfigurationValidationResult>(result);
} }
result = CheckDownstreamTemplatePathBeingsWithForwardSlash(configuration);
if (result.IsError)
{
return new OkResponse<ConfigurationValidationResult>(result);
}
result = CheckUpstreamTemplatePathBeingsWithForwardSlash(configuration);
if (result.IsError)
{
return new OkResponse<ConfigurationValidationResult>(result);
}
result = await CheckForUnsupportedAuthenticationProviders(configuration); result = await CheckForUnsupportedAuthenticationProviders(configuration);
if (result.IsError) if (result.IsError)
@ -49,6 +63,46 @@ namespace Ocelot.Configuration.Validator
return new OkResponse<ConfigurationValidationResult>(result); return new OkResponse<ConfigurationValidationResult>(result);
} }
private ConfigurationValidationResult CheckDownstreamTemplatePathBeingsWithForwardSlash(FileConfiguration configuration)
{
var errors = new List<Error>();
foreach(var reRoute in configuration.ReRoutes)
{
if(!reRoute.DownstreamPathTemplate.StartsWith("/"))
{
errors.Add(new PathTemplateDoesntStartWithForwardSlash($"{reRoute.DownstreamPathTemplate} doesnt start with forward slash"));
}
}
if(errors.Any())
{
return new ConfigurationValidationResult(true, errors);
}
return new ConfigurationValidationResult(false, errors);
}
private ConfigurationValidationResult CheckUpstreamTemplatePathBeingsWithForwardSlash(FileConfiguration configuration)
{
var errors = new List<Error>();
foreach(var reRoute in configuration.ReRoutes)
{
if(!reRoute.UpstreamPathTemplate.StartsWith("/"))
{
errors.Add(new PathTemplateDoesntStartWithForwardSlash($"{reRoute.DownstreamPathTemplate} doesnt start with forward slash"));
}
}
if(errors.Any())
{
return new ConfigurationValidationResult(true, errors);
}
return new ConfigurationValidationResult(false, errors);
}
private async Task<ConfigurationValidationResult> CheckForUnsupportedAuthenticationProviders(FileConfiguration configuration) private async Task<ConfigurationValidationResult> CheckForUnsupportedAuthenticationProviders(FileConfiguration configuration)
{ {
var errors = new List<Error>(); var errors = new List<Error>();

View File

@ -31,6 +31,7 @@
UnableToFindQoSProviderError, UnableToFindQoSProviderError,
UnableToSetConfigInConsulError, UnableToSetConfigInConsulError,
UnmappableRequestError, UnmappableRequestError,
RateLimitOptionsError RateLimitOptionsError,
PathTemplateDoesntStartWithForwardSlash
} }
} }

View File

@ -119,7 +119,7 @@ namespace Ocelot.AcceptanceTests
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "41879/", DownstreamPathTemplate = "/41879/",
DownstreamPort = 41879, DownstreamPort = 41879,
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHost = "localhost", DownstreamHost = "localhost",

View File

@ -111,7 +111,7 @@ namespace Ocelot.AcceptanceTests
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "api/products", DownstreamPathTemplate = "/api/products",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHost = "localhost", DownstreamHost = "localhost",
DownstreamPort = 51879, DownstreamPort = 51879,

View File

@ -57,7 +57,7 @@ namespace Ocelot.UnitTests.Configuration
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/api/products/", DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "http://asdf.com" UpstreamPathTemplate = "/asdf/"
} }
} }
})) }))
@ -66,6 +66,44 @@ namespace Ocelot.UnitTests.Configuration
.BDDfy(); .BDDfy();
} }
[Fact]
public void configuration_is_invalid_without_slash_prefix_downstream_path_template()
{
this.Given(x => x.GivenAConfiguration(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "api/products/",
UpstreamPathTemplate = "/asdf/"
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.BDDfy();
}
[Fact]
public void configuration_is_invalid_without_slash_prefix_upstream_path_template()
{
this.Given(x => x.GivenAConfiguration(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "api/prod/",
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.BDDfy();
}
[Fact] [Fact]
public void configuration_is_valid_with_valid_authentication_provider() public void configuration_is_valid_with_valid_authentication_provider()
{ {
@ -76,7 +114,7 @@ namespace Ocelot.UnitTests.Configuration
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/api/products/", DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "http://asdf.com", UpstreamPathTemplate = "/asdf/",
AuthenticationOptions = new FileAuthenticationOptions() AuthenticationOptions = new FileAuthenticationOptions()
{ {
AuthenticationProviderKey = "Test" AuthenticationProviderKey = "Test"
@ -90,14 +128,6 @@ namespace Ocelot.UnitTests.Configuration
.BDDfy(); .BDDfy();
} }
private void GivenTheAuthSchemeExists(string name)
{
_provider.Setup(x => x.GetAllSchemesAsync()).ReturnsAsync(new List<AuthenticationScheme>
{
new AuthenticationScheme(name, name, typeof(TestHandler))
});
}
[Fact] [Fact]
public void configuration_is_invalid_with_invalid_authentication_provider() public void configuration_is_invalid_with_invalid_authentication_provider()
{ {
@ -108,7 +138,7 @@ namespace Ocelot.UnitTests.Configuration
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/api/products/", DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "http://asdf.com", UpstreamPathTemplate = "/asdf/",
AuthenticationOptions = new FileAuthenticationOptions() AuthenticationOptions = new FileAuthenticationOptions()
{ {
AuthenticationProviderKey = "Test" AuthenticationProviderKey = "Test"
@ -131,12 +161,12 @@ namespace Ocelot.UnitTests.Configuration
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/api/products/", DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "http://asdf.com" UpstreamPathTemplate = "/asdf/"
}, },
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "http://www.bbc.co.uk", DownstreamPathTemplate = "http://www.bbc.co.uk",
UpstreamPathTemplate = "http://asdf.com" UpstreamPathTemplate = "/asdf/"
} }
} }
})) }))
@ -171,6 +201,14 @@ namespace Ocelot.UnitTests.Configuration
_result.Data.Errors[0].ShouldBeOfType<T>(); _result.Data.Errors[0].ShouldBeOfType<T>();
} }
private void GivenTheAuthSchemeExists(string name)
{
_provider.Setup(x => x.GetAllSchemesAsync()).ReturnsAsync(new List<AuthenticationScheme>
{
new AuthenticationScheme(name, name, typeof(TestHandler))
});
}
private class TestOptions : AuthenticationSchemeOptions private class TestOptions : AuthenticationSchemeOptions
{ {
} }

View File

@ -120,7 +120,7 @@ namespace Ocelot.UnitTests.Responder
// If this test fails then it's because the number of error codes has changed. // If this test fails then it's because the number of error codes has changed.
// You should make the appropriate changes to the test cases here to ensure // You should make the appropriate changes to the test cases here to ensure
// they cover all the error codes, and then modify this assertion. // they cover all the error codes, and then modify this assertion.
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(30, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?"); Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(31, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
} }
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode) private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)