looking at #135 validation

This commit is contained in:
Tom Gardham-Pallister
2017-11-01 18:57:27 +00:00
parent 7ec0c6a4d1
commit b57ae391ef
5 changed files with 120 additions and 15 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);
}
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);
if (result.IsError)
@ -49,6 +63,46 @@ namespace Ocelot.Configuration.Validator
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)
{
var errors = new List<Error>();

View File

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