-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57bfdc2
commit 2e94f8b
Showing
14 changed files
with
387 additions
and
577 deletions.
There are no files selected for viewing
71 changes: 0 additions & 71 deletions
71
...LambdaTestTool-v2/src/Amazon.Lambda.TestTool/ApiGatewayHttpApiV2ProxyRequestTranslator.cs
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/ApiGatewayMode.cs
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/ApiGatewayProxyRequestTranslator.cs
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/ApiGatewayRequestTranslatorFactory.cs
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/ApiGatewayRouteConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Amazon.Lambda.TestTool | ||
{ | ||
public class ApiGatewayRouteConfig | ||
{ | ||
public string LambdaResourceName { get; set; } | ||
public string Endpoint { get; set; } | ||
public string HttpMethod { get; set; } | ||
public string Path { get; set; } | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/HttpContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Amazon.Lambda.APIGatewayEvents; | ||
using System.Text; | ||
using System.Web; | ||
using static Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest; | ||
|
||
namespace Amazon.Lambda.TestTool | ||
{ | ||
/// <summary> | ||
/// Provides extension methods to translate an <see cref="HttpContext"/> to different types of API Gateway requests. | ||
/// </summary> | ||
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)); | ||
} | ||
|
||
/// <summary> | ||
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayHttpApiV2ProxyRequest"/>. | ||
/// </summary> | ||
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param> | ||
/// <returns>An <see cref="APIGatewayHttpApiV2ProxyRequest"/> object representing the translated request.</returns> | ||
public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request( | ||
this HttpContext context) | ||
{ | ||
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<string, string>(), | ||
Body = _httpRequestUtility.ReadRequestBody(request), | ||
IsBase64Encoded = false, | ||
RequestContext = new ProxyRequestContext | ||
{ | ||
Http = new HttpDescription | ||
{ | ||
Method = request.Method, | ||
Path = request.Path, | ||
Protocol = request.Protocol | ||
}, | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayProxyRequest"/>. | ||
/// </summary> | ||
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param> | ||
/// <returns>An <see cref="APIGatewayProxyRequest"/> object representing the translated request.</returns> | ||
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 (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<string, string>(), | ||
Body = _httpRequestUtility.ReadRequestBody(request), | ||
IsBase64Encoded = false | ||
}; | ||
|
||
if (_httpRequestUtility.IsBinaryContent(request.ContentType)) | ||
{ | ||
proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body)); | ||
proxyRequest.IsBase64Encoded = true; | ||
} | ||
|
||
return proxyRequest; | ||
} | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/IApiGatewayRequestTranslator.cs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/IApiGatewayRequestTranslatorFactory.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/IRouteConfigurationParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Amazon.Lambda.TestTool | ||
{ | ||
public interface IRouteConfigurationParser | ||
{ | ||
ApiGatewayRouteConfig GetRouteConfig(string httpMethod, string path); | ||
IDictionary<string, string> ExtractPathParameters(ApiGatewayRouteConfig routeConfig, string requestPath); | ||
} | ||
} |
Oops, something went wrong.