diff --git a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/HttpRequestUtility.cs b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/HttpRequestUtility.cs index a20820069..be3482bb2 100644 --- a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/HttpRequestUtility.cs +++ b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/HttpRequestUtility.cs @@ -1,92 +1,91 @@ -namespace Amazon.Lambda.TestTool +namespace Amazon.Lambda.TestTool.Utilities; + +/// +/// Utility class for handling HTTP requests in the context of API Gateway emulation. +/// +public static class HttpRequestUtility { /// - /// Utility class for handling HTTP requests in the context of API Gateway emulation. + /// Determines whether the specified content type represents binary content. /// - public static class HttpRequestUtility + /// The content type to check. + /// True if the content type represents binary content; otherwise, false. + public static bool IsBinaryContent(string? contentType) { - /// - /// Determines whether the specified content type represents binary content. - /// - /// The content type to check. - /// True if the content type represents binary content; otherwise, false. - public static bool IsBinaryContent(string? contentType) - { - if (string.IsNullOrEmpty(contentType)) - return false; + if (string.IsNullOrEmpty(contentType)) + return false; - return contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) || - contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || - contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase) || - contentType.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase); - } + return contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) || + contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || + contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase) || + contentType.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase); + } - /// - /// Reads the body of the HTTP request as a string. - /// - /// The HTTP request. - /// The body of the request as a string. - public static string ReadRequestBody(HttpRequest request) + /// + /// Reads the body of the HTTP request as a string. + /// + /// The HTTP request. + /// The body of the request as a string. + public static string ReadRequestBody(HttpRequest request) + { + using (var reader = new StreamReader(request.Body)) { - using (var reader = new StreamReader(request.Body)) - { - return reader.ReadToEnd(); - } + return reader.ReadToEnd(); } + } - /// - /// Extracts headers from the request, separating them into single-value and multi-value dictionaries. - /// - /// The request headers. - /// A tuple containing single-value and multi-value header dictionaries. - /// - /// For headers: - /// Accept: text/html - /// Accept: application/xhtml+xml - /// X-Custom-Header: value1 - /// - /// The method will return: - /// singleValueHeaders: { "Accept": "application/xhtml+xml", "X-Custom-Header": "value1" } - /// multiValueHeaders: { "Accept": ["text/html", "application/xhtml+xml"], "X-Custom-Header": ["value1"] } - /// - public static (IDictionary, IDictionary>) ExtractHeaders(IHeaderDictionary headers) - { - var singleValueHeaders = new Dictionary(); - var multiValueHeaders = new Dictionary>(); - - foreach (var header in headers) - { - singleValueHeaders[header.Key] = header.Value.Last() ?? ""; - multiValueHeaders[header.Key] = [.. header.Value]; - } + /// + /// Extracts headers from the request, separating them into single-value and multi-value dictionaries. + /// + /// The request headers. + /// A tuple containing single-value and multi-value header dictionaries. + /// + /// For headers: + /// Accept: text/html + /// Accept: application/xhtml+xml + /// X-Custom-Header: value1 + /// + /// The method will return: + /// singleValueHeaders: { "Accept": "application/xhtml+xml", "X-Custom-Header": "value1" } + /// multiValueHeaders: { "Accept": ["text/html", "application/xhtml+xml"], "X-Custom-Header": ["value1"] } + /// + public static (IDictionary, IDictionary>) ExtractHeaders(IHeaderDictionary headers) + { + var singleValueHeaders = new Dictionary(); + var multiValueHeaders = new Dictionary>(); - return (singleValueHeaders, multiValueHeaders); + foreach (var header in headers) + { + singleValueHeaders[header.Key] = header.Value.Last() ?? ""; + multiValueHeaders[header.Key] = [.. header.Value]; } - /// - /// Extracts query string parameters from the request, separating them into single-value and multi-value dictionaries. - /// - /// The query string collection. - /// A tuple containing single-value and multi-value query parameter dictionaries. - /// - /// For query string: ?param1=value1&param2=value2&param2=value3 - /// - /// The method will return: - /// singleValueParams: { "param1": "value1", "param2": "value3" } - /// multiValueParams: { "param1": ["value1"], "param2": ["value2", "value3"] } - /// - public static (IDictionary, IDictionary>) ExtractQueryStringParameters(IQueryCollection query) - { - var singleValueParams = new Dictionary(); - var multiValueParams = new Dictionary>(); + return (singleValueHeaders, multiValueHeaders); + } - foreach (var param in query) - { - singleValueParams[param.Key] = param.Value.Last() ?? ""; - multiValueParams[param.Key] = [.. param.Value]; - } + /// + /// Extracts query string parameters from the request, separating them into single-value and multi-value dictionaries. + /// + /// The query string collection. + /// A tuple containing single-value and multi-value query parameter dictionaries. + /// + /// For query string: ?param1=value1&param2=value2&param2=value3 + /// + /// The method will return: + /// singleValueParams: { "param1": "value1", "param2": "value3" } + /// multiValueParams: { "param1": ["value1"], "param2": ["value2", "value3"] } + /// + public static (IDictionary, IDictionary>) ExtractQueryStringParameters(IQueryCollection query) + { + var singleValueParams = new Dictionary(); + var multiValueParams = new Dictionary>(); - return (singleValueParams, multiValueParams); + foreach (var param in query) + { + singleValueParams[param.Key] = param.Value.Last() ?? ""; + multiValueParams[param.Key] = [.. param.Value]; } + + return (singleValueParams, multiValueParams); } } diff --git a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/RouteTemplateUtility.cs b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/RouteTemplateUtility.cs index 4d9b13d44..fa9da4e0d 100644 --- a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/RouteTemplateUtility.cs +++ b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/RouteTemplateUtility.cs @@ -1,67 +1,66 @@ -using Microsoft.AspNetCore.Routing.Template; +namespace Amazon.Lambda.TestTool.Utilities; -namespace Amazon.Lambda.TestTool.Utilities +using Microsoft.AspNetCore.Routing.Template; + +/// +/// Provides utility methods for working with route templates and extracting path parameters. +/// +public static class RouteTemplateUtility { /// - /// Provides utility methods for working with route templates and extracting path parameters. + /// Extracts path parameters from an actual path based on a route template. /// - public static class RouteTemplateUtility + /// The route template to match against. + /// The actual path to extract parameters from. + /// A dictionary of extracted path parameters and their values. + /// + /// Using this method: + /// + /// var routeTemplate = "/users/{id}/orders/{orderId}"; + /// var actualPath = "/users/123/orders/456"; + /// var parameters = RouteTemplateUtility.ExtractPathParameters(routeTemplate, actualPath); + /// // parameters will contain: { {"id", "123"}, {"orderId", "456"} } + /// + /// + public static Dictionary ExtractPathParameters(string routeTemplate, string actualPath) { - /// - /// Extracts path parameters from an actual path based on a route template. - /// - /// The route template to match against. - /// The actual path to extract parameters from. - /// A dictionary of extracted path parameters and their values. - /// - /// Using this method: - /// - /// var routeTemplate = "/users/{id}/orders/{orderId}"; - /// var actualPath = "/users/123/orders/456"; - /// var parameters = RouteTemplateUtility.ExtractPathParameters(routeTemplate, actualPath); - /// // parameters will contain: { {"id", "123"}, {"orderId", "456"} } - /// - /// - public static Dictionary ExtractPathParameters(string routeTemplate, string actualPath) + var template = TemplateParser.Parse(routeTemplate); + var matcher = new TemplateMatcher(template, GetDefaults(template)); + var routeValues = new RouteValueDictionary(); + + if (matcher.TryMatch(actualPath, routeValues)) { - var template = TemplateParser.Parse(routeTemplate); - var matcher = new TemplateMatcher(template, GetDefaults(template)); - var routeValues = new RouteValueDictionary(); + return routeValues.ToDictionary(rv => rv.Key, rv => rv.Value?.ToString() ?? string.Empty); + } - if (matcher.TryMatch(actualPath, routeValues)) - { - return routeValues.ToDictionary(rv => rv.Key, rv => rv.Value?.ToString() ?? string.Empty); - } + return new Dictionary(); + } - return new Dictionary(); - } + /// + /// Gets the default values for parameters in a parsed route template. + /// + /// The parsed route template. + /// A dictionary of default values for the template parameters. + /// + /// Using this method: + /// + /// var template = TemplateParser.Parse("/api/{version=v1}/users/{id}"); + /// var defaults = RouteTemplateUtility.GetDefaults(template); + /// // defaults will contain: { {"version", "v1"} } + /// + /// + public static RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate) + { + var result = new RouteValueDictionary(); - /// - /// Gets the default values for parameters in a parsed route template. - /// - /// The parsed route template. - /// A dictionary of default values for the template parameters. - /// - /// Using this method: - /// - /// var template = TemplateParser.Parse("/api/{version=v1}/users/{id}"); - /// var defaults = RouteTemplateUtility.GetDefaults(template); - /// // defaults will contain: { {"version", "v1"} } - /// - /// - public static RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate) + foreach (var parameter in parsedTemplate.Parameters) { - var result = new RouteValueDictionary(); - - foreach (var parameter in parsedTemplate.Parameters) + if (parameter.DefaultValue != null) { - if (parameter.DefaultValue != null) - { - if (parameter.Name != null) result.Add(parameter.Name, parameter.DefaultValue); - } + if (parameter.Name != null) result.Add(parameter.Name, parameter.DefaultValue); } - - return result; } + + return result; } }