Skip to content

Commit

Permalink
reply to garrett's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Dec 20, 2024
1 parent 87a8dcf commit 4db624d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Text.Json;
using System.Text.RegularExpressions;
using Amazon.Lambda.TestTool.Models;
using Amazon.Lambda.TestTool.Services.IO;

Expand Down Expand Up @@ -142,24 +143,16 @@ private bool IsRouteConfigValid(ApiGatewayRouteConfig routeConfig)
return false;
}

var occurrences = routeConfig.Path
.Split('/')
.Where(
x => x.StartsWith("{") &&
x.EndsWith("+}"))
.ToList();
if (occurrences.Count > 1)
var segments = routeConfig.Path.Trim('/').Split('/');
foreach (var segment in segments)
{
_logger.LogError("The route config {Method} {Path} cannot have multiple greedy variables {{proxy+}}.",
routeConfig.HttpMethod, routeConfig.Path);
return false;
}

if (occurrences.Count == 1 && !routeConfig.Path.EndsWith($"/{occurrences.Last()}"))
{
_logger.LogError("The route config {Method} {Path} uses a greedy variable {{proxy+}} but does not end with it.",
routeConfig.HttpMethod, routeConfig.Path);
return false;
var regexPattern = "^(\\{[\\w.:-]+\\+?\\}|[a-zA-Z0-9.:_-]+)$";
if (!Regex.IsMatch(segment, regexPattern))
{
_logger.LogError("One or more path parts appear to be invalid. Parts are validated against this regular expression: {Regex}",
regexPattern);
return false;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,45 @@ public void GetRouteConfig_ReturnsNullForNonMatchingPath()
Assert.Null(result);
}

[Theory]
[InlineData("", "", "")]
[InlineData("F1", "", "")]
[InlineData("F1", "GET", "")]
[InlineData("F1", "GET", "//")]
[InlineData("F1", "GET", "/{}")]
[InlineData("F1", "GET", "/{+}")]
[InlineData("F1", "GET", "/{proxy+}+}")]
public void GetRouteConfig_InvalidPath(string lambdaName, string method, string template)
{
// Arrange
var routeConfig = new ApiGatewayRouteConfig
{
LambdaResourceName = lambdaName,
HttpMethod = method,
Path = template
};

_mockEnvironmentManager
.Setup(m => m.GetEnvironmentVariables())
.Returns(new Dictionary<string, string>
{
{ Constants.LambdaConfigEnvironmentVariablePrefix, JsonSerializer.Serialize(routeConfig) }
});

// Act
_ = new ApiGatewayRouteConfigService(_mockEnvironmentManager.Object, _mockLogger.Object);

// Assert
_mockLogger.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString() == $"The route config {method} {template} is not valid. It will be skipped."),
It.IsAny<Exception>(),
((Func<It.IsAnyType, Exception, string>)It.IsAny<object>())!),
Times.Once);
}

[Fact]
public void Constructor_LoadsAndParsesListOfConfigs()
{
Expand Down

0 comments on commit 4db624d

Please sign in to comment.