Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: greedy variable can be any value #1911

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 +143,16 @@ private bool IsRouteConfigValid(ApiGatewayRouteConfig routeConfig)
return false;
}

var occurrences = routeConfig.Path.Split("{proxy+}").Length - 1;
philasmar marked this conversation as resolved.
Show resolved Hide resolved
if (occurrences > 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 == 1 && !routeConfig.Path.EndsWith("/{proxy+}"))
{
_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 Expand Up @@ -412,7 +410,7 @@ private bool IsVariableSegment(string segment)
/// <returns><c>true</c> if the segment is a greedy variable segment; <c>false</c> otherwise.</returns>
private bool IsGreedyVariable(string segment)
{
return segment.Equals("{proxy+}", StringComparison.InvariantCultureIgnoreCase);
return segment.StartsWith("{") && segment.EndsWith("+}");
}

/// <summary>
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 Expand Up @@ -159,8 +198,10 @@ public void Constructor_LoadsAndParsesListOfConfigs()
Assert.Equal("Function2", result2.LambdaResourceName);
}

[Fact]
public void CatchAllRouteConfig()
[Theory]
[InlineData("proxy")]
[InlineData("anyvalue")]
public void CatchAllRouteConfig(string variableName)
{
// Arange
var routeConfigs = new List<ApiGatewayRouteConfig>
Expand All @@ -169,7 +210,7 @@ public void CatchAllRouteConfig()
{
LambdaResourceName = "F1",
HttpMethod = "ANY",
Path = "/{proxy+}"
philasmar marked this conversation as resolved.
Show resolved Hide resolved
Path = $"/{{{variableName}+}}"
}
};

Expand Down
Loading