From 9941893c3b1f41094ef9d6c6ba2e563d1c0f62d2 Mon Sep 17 00:00:00 2001 From: Garrett Beatty Date: Mon, 9 Dec 2024 12:28:52 -0500 Subject: [PATCH] top level namespace --- .../HttpContextExtensions.cs | 177 +++++---- .../HttpContextExtensionsTests.cs | 351 +++++++++--------- .../HttpRequestUtilityTests.cs | 6 +- 3 files changed, 266 insertions(+), 268 deletions(-) diff --git a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs index c557a8d7b..16a674f18 100644 --- a/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs +++ b/Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs @@ -1,114 +1,113 @@ -using Amazon.Lambda.APIGatewayEvents; +namespace Amazon.Lambda.TestTool; + +using Amazon.Lambda.APIGatewayEvents; using System.Text; using System.Web; using static Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest; -namespace Amazon.Lambda.TestTool +/// +/// Provides extension methods to translate an to different types of API Gateway requests. +/// +public static class HttpContextExtensions { + private static IHttpRequestUtility _httpRequestUtility = new HttpRequestUtility(); + private static IRouteConfigurationParser _routeConfigurationParser; + + public static void SetHttpRequestUtility(IHttpRequestUtility httpRequestUtility) + { + _httpRequestUtility = httpRequestUtility ?? throw new ArgumentNullException(nameof(httpRequestUtility)); + } + + public static void SetRouteConfigurationParser(IRouteConfigurationParser routeConfigurationParser) + { + _routeConfigurationParser = routeConfigurationParser ?? throw new ArgumentNullException(nameof(routeConfigurationParser)); + } + /// - /// Provides extension methods to translate an to different types of API Gateway requests. + /// Translates an to an . /// - public static class HttpContextExtensions + /// The to be translated. + /// An object representing the translated request. + public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request( + this HttpContext context) { - private static IHttpRequestUtility _httpRequestUtility = new HttpRequestUtility(); - private static IRouteConfigurationParser _routeConfigurationParser; + var request = context.Request; - public static void SetHttpRequestUtility(IHttpRequestUtility httpRequestUtility) - { - _httpRequestUtility = httpRequestUtility ?? throw new ArgumentNullException(nameof(httpRequestUtility)); - } + var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); + var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); - public static void SetRouteConfigurationParser(IRouteConfigurationParser routeConfigurationParser) - { - _routeConfigurationParser = routeConfigurationParser ?? throw new ArgumentNullException(nameof(routeConfigurationParser)); - } + var (headers, _) = _httpRequestUtility.ExtractHeaders(request.Headers); + var (queryStringParameters, _) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); - /// - /// Translates an to an . - /// - /// The to be translated. - /// An object representing the translated request. - public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request( - this HttpContext context) + var httpApiV2ProxyRequest = new APIGatewayHttpApiV2ProxyRequest { - var request = context.Request; - - var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); - var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); - - var (headers, _) = _httpRequestUtility.ExtractHeaders(request.Headers); - var (queryStringParameters, _) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); - - var httpApiV2ProxyRequest = new APIGatewayHttpApiV2ProxyRequest + RouteKey = $"{request.Method} {matchedConfig.Path}", + RawPath = request.Path, + RawQueryString = request.QueryString.Value, + Cookies = request.Cookies.Select(c => $"{c.Key}={c.Value}").ToArray(), + Headers = headers, + QueryStringParameters = queryStringParameters, + PathParameters = pathParameters ?? new Dictionary(), + Body = _httpRequestUtility.ReadRequestBody(request), + IsBase64Encoded = false, + RequestContext = new ProxyRequestContext { - RouteKey = $"{request.Method} {matchedConfig.Path}", - RawPath = request.Path, - RawQueryString = request.QueryString.Value, - Cookies = request.Cookies.Select(c => $"{c.Key}={c.Value}").ToArray(), - Headers = headers, - QueryStringParameters = queryStringParameters, - PathParameters = pathParameters ?? new Dictionary(), - Body = _httpRequestUtility.ReadRequestBody(request), - IsBase64Encoded = false, - RequestContext = new ProxyRequestContext + Http = new HttpDescription { - Http = new HttpDescription - { - Method = request.Method, - Path = request.Path, - Protocol = request.Protocol - }, - RouteKey = $"{request.Method} {matchedConfig.Path}" + Method = request.Method, + Path = request.Path, + Protocol = request.Protocol }, - Version = "2.0" - }; + RouteKey = $"{request.Method} {matchedConfig.Path}" + }, + Version = "2.0" + }; - if (_httpRequestUtility.IsBinaryContent(request.ContentType)) - { - httpApiV2ProxyRequest.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(httpApiV2ProxyRequest.Body)); - httpApiV2ProxyRequest.IsBase64Encoded = true; - } - - return httpApiV2ProxyRequest; + if (_httpRequestUtility.IsBinaryContent(request.ContentType)) + { + httpApiV2ProxyRequest.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(httpApiV2ProxyRequest.Body)); + httpApiV2ProxyRequest.IsBase64Encoded = true; } - /// - /// Translates an to an . - /// - /// The to be translated. - /// An object representing the translated request. - public static APIGatewayProxyRequest ToApiGatewayRequest( - this HttpContext context) - { - var request = context.Request; + return httpApiV2ProxyRequest; + } + + /// + /// Translates an to an . + /// + /// The to be translated. + /// An object representing the translated request. + public static APIGatewayProxyRequest ToApiGatewayRequest( + this HttpContext context) + { + var request = context.Request; - var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); - var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); + var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); + var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); - var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers); - var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); + var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers); + var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); - var proxyRequest = new APIGatewayProxyRequest - { - Resource = matchedConfig.Path, - Path = HttpUtility.UrlEncode(request.Path), - HttpMethod = request.Method, - Headers = headers, - MultiValueHeaders = multiValueHeaders, - QueryStringParameters = queryStringParameters, - MultiValueQueryStringParameters = multiValueQueryStringParameters, - PathParameters = pathParameters ?? new Dictionary(), - Body = _httpRequestUtility.ReadRequestBody(request), - IsBase64Encoded = false - }; - - if (_httpRequestUtility.IsBinaryContent(request.ContentType)) - { - proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body)); - proxyRequest.IsBase64Encoded = true; - } + var proxyRequest = new APIGatewayProxyRequest + { + Resource = matchedConfig.Path, + Path = HttpUtility.UrlEncode(request.Path), + HttpMethod = request.Method, + Headers = headers, + MultiValueHeaders = multiValueHeaders, + QueryStringParameters = queryStringParameters, + MultiValueQueryStringParameters = multiValueQueryStringParameters, + PathParameters = pathParameters ?? new Dictionary(), + Body = _httpRequestUtility.ReadRequestBody(request), + IsBase64Encoded = false + }; - return proxyRequest; + if (_httpRequestUtility.IsBinaryContent(request.ContentType)) + { + proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body)); + proxyRequest.IsBase64Encoded = true; } + + return proxyRequest; } } diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpContextExtensionsTests.cs b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpContextExtensionsTests.cs index cdd6d57e2..b3dda91d5 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpContextExtensionsTests.cs +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpContextExtensionsTests.cs @@ -1,189 +1,188 @@ -using Microsoft.AspNetCore.Http; +namespace Amazon.Lambda.TestTool.UnitTests; + +using Microsoft.AspNetCore.Http; using System.Web; using Xunit; using Moq; using System.Text; -namespace Amazon.Lambda.TestTool.UnitTests +public class HttpContextExtensionsTests { - public class HttpContextExtensionsTests + private readonly Mock _mockRouteConfigParser; + private readonly Mock _mockHttpRequestUtility; + + public HttpContextExtensionsTests() + { + _mockRouteConfigParser = new Mock(); + _mockHttpRequestUtility = new Mock(); + + _mockRouteConfigParser.Setup(x => x.GetRouteConfig(It.IsAny(), It.IsAny())) + .Returns((string method, string path) => new ApiGatewayRouteConfig + { + LambdaResourceName = "TestLambdaFunction", + Endpoint = $"{method} {path}", + HttpMethod = method, + Path = "/api/users/{userId}/orders" // This is now the resource + }); + _mockRouteConfigParser.Setup(x => x.ExtractPathParameters(It.IsAny(), It.IsAny())) + .Returns(new Dictionary { { "userId", "123" } }); + + _mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny())) + .Returns((new Dictionary(), new Dictionary>())); + _mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny())) + .Returns((new Dictionary(), new Dictionary>())); + _mockHttpRequestUtility.Setup(x => x.ReadRequestBody(It.IsAny())) + .Returns(string.Empty); + _mockHttpRequestUtility.Setup(x => x.IsBinaryContent(It.IsAny())) + .Returns(false); + + HttpContextExtensions.SetRouteConfigurationParser(_mockRouteConfigParser.Object); + HttpContextExtensions.SetHttpRequestUtility(_mockHttpRequestUtility.Object); + } + + [Fact] + public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyRequest() { - private readonly Mock _mockRouteConfigParser; - private readonly Mock _mockHttpRequestUtility; + var context = new DefaultHttpContext(); + var request = context.Request; + request.Method = "GET"; + request.Scheme = "https"; + request.Host = new HostString("api.example.com"); + request.Path = "/api/users/123/orders"; + request.QueryString = new QueryString("?status=pending"); + request.Headers["User-Agent"] = "TestAgent"; + request.Headers["Accept"] = "application/json"; + request.Headers["Cookie"] = "session=abc123; theme=dark"; + + var result = context.ToApiGatewayHttpV2Request(); + + Assert.NotNull(result); + Assert.Equal("2.0", result.Version); + Assert.Equal("GET /api/users/{userId}/orders", result.RouteKey); + Assert.Equal("/api/users/123/orders", result.RawPath); + Assert.Equal("?status=pending", result.RawQueryString); + Assert.Equal(2, result.Cookies.Length); + Assert.Contains("session=abc123", result.Cookies); + Assert.Contains("theme=dark", result.Cookies); + Assert.Equal("123", result.PathParameters["userId"]); + Assert.Equal("GET", result.RequestContext.Http.Method); + Assert.Equal("/api/users/123/orders", result.RequestContext.Http.Path); + Assert.Equal("GET /api/users/{userId}/orders", result.RequestContext.RouteKey); + + _mockRouteConfigParser.Verify(x => x.GetRouteConfig("GET", "/api/users/123/orders"), Times.Once); + _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/orders"), Times.Once); + } - public HttpContextExtensionsTests() - { - _mockRouteConfigParser = new Mock(); - _mockHttpRequestUtility = new Mock(); + [Fact] + public void ToApiGatewayHttpV2Request_WithBinaryContent_ShouldBase64EncodeBody() + { + var context = new DefaultHttpContext(); + var request = context.Request; + request.Method = "POST"; + request.Path = "/api/users/123/avatar"; + request.ContentType = "application/octet-stream"; + var bodyContent = new byte[] { 1, 2, 3, 4, 5 }; + request.Body = new MemoryStream(bodyContent); + + _mockHttpRequestUtility.Setup(x => x.IsBinaryContent(It.IsAny())).Returns(true); + _mockHttpRequestUtility.Setup(x => x.ReadRequestBody(It.IsAny())).Returns(Encoding.UTF8.GetString(bodyContent)); + + _mockRouteConfigParser.Setup(x => x.GetRouteConfig("POST", "/api/users/123/avatar")) + .Returns(new ApiGatewayRouteConfig + { + LambdaResourceName = "UploadAvatarFunction", + Endpoint = "POST /api/users/{userId}/avatar", + HttpMethod = "POST", + Path = "/api/users/{userId}/avatar" + }); + + var result = context.ToApiGatewayHttpV2Request(); + + Assert.NotNull(result); + Assert.True(result.IsBase64Encoded); + Assert.Equal(Convert.ToBase64String(bodyContent), result.Body); + Assert.Equal("123", result.PathParameters["userId"]); + Assert.Equal("POST /api/users/{userId}/avatar", result.RouteKey); + + _mockRouteConfigParser.Verify(x => x.GetRouteConfig("POST", "/api/users/123/avatar"), Times.Once); + _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/avatar"), Times.Once); + } - _mockRouteConfigParser.Setup(x => x.GetRouteConfig(It.IsAny(), It.IsAny())) - .Returns((string method, string path) => new ApiGatewayRouteConfig + [Fact] + public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest() + { + var context = new DefaultHttpContext(); + var request = context.Request; + request.Method = "GET"; + request.Scheme = "https"; + request.Host = new HostString("api.example.com"); + request.Path = "/api/users/123/orders"; + request.QueryString = new QueryString("?status=pending&tag=important&tag=urgent"); + request.Headers["User-Agent"] = "TestAgent"; + request.Headers["Accept"] = new Microsoft.Extensions.Primitives.StringValues(new[] { "text/html", "application/json" }); + request.Headers["Cookie"] = "session=abc123; theme=dark"; + request.Headers["X-Custom-Header"] = "value1"; + + _mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny())) + .Returns(( + new Dictionary + { + { "User-Agent", "TestAgent" }, + { "Accept", "application/json" }, + { "Cookie", "session=abc123; theme=dark" }, + { "X-Custom-Header", "value1" } + }, + new Dictionary> { - LambdaResourceName = "TestLambdaFunction", - Endpoint = $"{method} {path}", - HttpMethod = method, - Path = "/api/users/{userId}/orders" // This is now the resource - }); - _mockRouteConfigParser.Setup(x => x.ExtractPathParameters(It.IsAny(), It.IsAny())) - .Returns(new Dictionary { { "userId", "123" } }); - - _mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny())) - .Returns((new Dictionary(), new Dictionary>())); - _mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny())) - .Returns((new Dictionary(), new Dictionary>())); - _mockHttpRequestUtility.Setup(x => x.ReadRequestBody(It.IsAny())) - .Returns(string.Empty); - _mockHttpRequestUtility.Setup(x => x.IsBinaryContent(It.IsAny())) - .Returns(false); - - HttpContextExtensions.SetRouteConfigurationParser(_mockRouteConfigParser.Object); - HttpContextExtensions.SetHttpRequestUtility(_mockHttpRequestUtility.Object); - } - - [Fact] - public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyRequest() - { - var context = new DefaultHttpContext(); - var request = context.Request; - request.Method = "GET"; - request.Scheme = "https"; - request.Host = new HostString("api.example.com"); - request.Path = "/api/users/123/orders"; - request.QueryString = new QueryString("?status=pending"); - request.Headers["User-Agent"] = "TestAgent"; - request.Headers["Accept"] = "application/json"; - request.Headers["Cookie"] = "session=abc123; theme=dark"; - - var result = context.ToApiGatewayHttpV2Request(); - - Assert.NotNull(result); - Assert.Equal("2.0", result.Version); - Assert.Equal("GET /api/users/{userId}/orders", result.RouteKey); - Assert.Equal("/api/users/123/orders", result.RawPath); - Assert.Equal("?status=pending", result.RawQueryString); - Assert.Equal(2, result.Cookies.Length); - Assert.Contains("session=abc123", result.Cookies); - Assert.Contains("theme=dark", result.Cookies); - Assert.Equal("123", result.PathParameters["userId"]); - Assert.Equal("GET", result.RequestContext.Http.Method); - Assert.Equal("/api/users/123/orders", result.RequestContext.Http.Path); - Assert.Equal("GET /api/users/{userId}/orders", result.RequestContext.RouteKey); - - _mockRouteConfigParser.Verify(x => x.GetRouteConfig("GET", "/api/users/123/orders"), Times.Once); - _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/orders"), Times.Once); - } - - [Fact] - public void ToApiGatewayHttpV2Request_WithBinaryContent_ShouldBase64EncodeBody() - { - var context = new DefaultHttpContext(); - var request = context.Request; - request.Method = "POST"; - request.Path = "/api/users/123/avatar"; - request.ContentType = "application/octet-stream"; - var bodyContent = new byte[] { 1, 2, 3, 4, 5 }; - request.Body = new MemoryStream(bodyContent); - - _mockHttpRequestUtility.Setup(x => x.IsBinaryContent(It.IsAny())).Returns(true); - _mockHttpRequestUtility.Setup(x => x.ReadRequestBody(It.IsAny())).Returns(Encoding.UTF8.GetString(bodyContent)); - - _mockRouteConfigParser.Setup(x => x.GetRouteConfig("POST", "/api/users/123/avatar")) - .Returns(new ApiGatewayRouteConfig + { "User-Agent", new List { "TestAgent" } }, + { "Accept", new List { "text/html", "application/json" } }, + { "Cookie", new List { "session=abc123; theme=dark" } }, + { "X-Custom-Header", new List { "value1" } } + } + )); + + _mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny())) + .Returns(( + new Dictionary { - LambdaResourceName = "UploadAvatarFunction", - Endpoint = "POST /api/users/{userId}/avatar", - HttpMethod = "POST", - Path = "/api/users/{userId}/avatar" - }); - - var result = context.ToApiGatewayHttpV2Request(); - - Assert.NotNull(result); - Assert.True(result.IsBase64Encoded); - Assert.Equal(Convert.ToBase64String(bodyContent), result.Body); - Assert.Equal("123", result.PathParameters["userId"]); - Assert.Equal("POST /api/users/{userId}/avatar", result.RouteKey); - - _mockRouteConfigParser.Verify(x => x.GetRouteConfig("POST", "/api/users/123/avatar"), Times.Once); - _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/avatar"), Times.Once); - } - - [Fact] - public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest() - { - var context = new DefaultHttpContext(); - var request = context.Request; - request.Method = "GET"; - request.Scheme = "https"; - request.Host = new HostString("api.example.com"); - request.Path = "/api/users/123/orders"; - request.QueryString = new QueryString("?status=pending&tag=important&tag=urgent"); - request.Headers["User-Agent"] = "TestAgent"; - request.Headers["Accept"] = new Microsoft.Extensions.Primitives.StringValues(new[] { "text/html", "application/json" }); - request.Headers["Cookie"] = "session=abc123; theme=dark"; - request.Headers["X-Custom-Header"] = "value1"; - - _mockHttpRequestUtility.Setup(x => x.ExtractHeaders(It.IsAny())) - .Returns(( - new Dictionary - { - { "User-Agent", "TestAgent" }, - { "Accept", "application/json" }, - { "Cookie", "session=abc123; theme=dark" }, - { "X-Custom-Header", "value1" } - }, - new Dictionary> - { - { "User-Agent", new List { "TestAgent" } }, - { "Accept", new List { "text/html", "application/json" } }, - { "Cookie", new List { "session=abc123; theme=dark" } }, - { "X-Custom-Header", new List { "value1" } } - } - )); - - _mockHttpRequestUtility.Setup(x => x.ExtractQueryStringParameters(It.IsAny())) - .Returns(( - new Dictionary - { - { "status", "pending" }, - { "tag", "urgent" } - }, - new Dictionary> - { - { "status", new List { "pending" } }, - { "tag", new List { "important", "urgent" } } - } - )); - - var result = context.ToApiGatewayRequest(); - - Assert.NotNull(result); - Assert.Equal("/api/users/{userId}/orders", result.Resource); - Assert.Equal(HttpUtility.UrlEncode("/api/users/123/orders"), result.Path); - Assert.Equal("GET", result.HttpMethod); - - Assert.Equal("TestAgent", result.Headers["User-Agent"]); - Assert.Equal("application/json", result.Headers["Accept"]); - Assert.Equal("session=abc123; theme=dark", result.Headers["Cookie"]); - Assert.Equal("value1", result.Headers["X-Custom-Header"]); - - Assert.Equal(new List { "TestAgent" }, result.MultiValueHeaders["User-Agent"]); - Assert.Equal(new List { "text/html", "application/json" }, result.MultiValueHeaders["Accept"]); - Assert.Equal(new List { "session=abc123; theme=dark" }, result.MultiValueHeaders["Cookie"]); - Assert.Equal(new List { "value1" }, result.MultiValueHeaders["X-Custom-Header"]); - - Assert.Equal("pending", result.QueryStringParameters["status"]); - Assert.Equal("urgent", result.QueryStringParameters["tag"]); - - Assert.Equal(new List { "pending" }, result.MultiValueQueryStringParameters["status"]); - Assert.Equal(new List { "important", "urgent" }, result.MultiValueQueryStringParameters["tag"]); - - Assert.Equal("123", result.PathParameters["userId"]); - Assert.Equal(string.Empty, result.Body); - Assert.False(result.IsBase64Encoded); - - _mockRouteConfigParser.Verify(x => x.GetRouteConfig("GET", "/api/users/123/orders"), Times.Once); - _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/orders"), Times.Once); - } + { "status", "pending" }, + { "tag", "urgent" } + }, + new Dictionary> + { + { "status", new List { "pending" } }, + { "tag", new List { "important", "urgent" } } + } + )); + + var result = context.ToApiGatewayRequest(); + + Assert.NotNull(result); + Assert.Equal("/api/users/{userId}/orders", result.Resource); + Assert.Equal(HttpUtility.UrlEncode("/api/users/123/orders"), result.Path); + Assert.Equal("GET", result.HttpMethod); + + Assert.Equal("TestAgent", result.Headers["User-Agent"]); + Assert.Equal("application/json", result.Headers["Accept"]); + Assert.Equal("session=abc123; theme=dark", result.Headers["Cookie"]); + Assert.Equal("value1", result.Headers["X-Custom-Header"]); + + Assert.Equal(new List { "TestAgent" }, result.MultiValueHeaders["User-Agent"]); + Assert.Equal(new List { "text/html", "application/json" }, result.MultiValueHeaders["Accept"]); + Assert.Equal(new List { "session=abc123; theme=dark" }, result.MultiValueHeaders["Cookie"]); + Assert.Equal(new List { "value1" }, result.MultiValueHeaders["X-Custom-Header"]); + + Assert.Equal("pending", result.QueryStringParameters["status"]); + Assert.Equal("urgent", result.QueryStringParameters["tag"]); + + Assert.Equal(new List { "pending" }, result.MultiValueQueryStringParameters["status"]); + Assert.Equal(new List { "important", "urgent" }, result.MultiValueQueryStringParameters["tag"]); + + Assert.Equal("123", result.PathParameters["userId"]); + Assert.Equal(string.Empty, result.Body); + Assert.False(result.IsBase64Encoded); + + _mockRouteConfigParser.Verify(x => x.GetRouteConfig("GET", "/api/users/123/orders"), Times.Once); + _mockRouteConfigParser.Verify(x => x.ExtractPathParameters(It.IsAny(), "/api/users/123/orders"), Times.Once); } } diff --git a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpRequestUtilityTests.cs b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpRequestUtilityTests.cs index 2c70ce232..21b66fd95 100644 --- a/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpRequestUtilityTests.cs +++ b/Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/HttpRequestUtilityTests.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Http; +namespace Amazon.Lambda.TestTool.UnitTests; + +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Primitives; using Moq; using System; @@ -7,8 +9,6 @@ using System.Text; using System.Threading.Tasks; -namespace Amazon.Lambda.TestTool.UnitTests; - public class HttpRequestUtilityTests { private readonly HttpRequestUtility _utility;