|
1 |
| -using Amazon.Lambda.APIGatewayEvents; |
| 1 | +namespace Amazon.Lambda.TestTool; |
| 2 | + |
| 3 | +using Amazon.Lambda.APIGatewayEvents; |
2 | 4 | using System.Text;
|
3 | 5 | using System.Web;
|
4 | 6 | using static Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest;
|
5 | 7 |
|
6 |
| -namespace Amazon.Lambda.TestTool |
| 8 | +/// <summary> |
| 9 | +/// Provides extension methods to translate an <see cref="HttpContext"/> to different types of API Gateway requests. |
| 10 | +/// </summary> |
| 11 | +public static class HttpContextExtensions |
7 | 12 | {
|
| 13 | + private static IHttpRequestUtility _httpRequestUtility = new HttpRequestUtility(); |
| 14 | + private static IRouteConfigurationParser _routeConfigurationParser; |
| 15 | + |
| 16 | + public static void SetHttpRequestUtility(IHttpRequestUtility httpRequestUtility) |
| 17 | + { |
| 18 | + _httpRequestUtility = httpRequestUtility ?? throw new ArgumentNullException(nameof(httpRequestUtility)); |
| 19 | + } |
| 20 | + |
| 21 | + public static void SetRouteConfigurationParser(IRouteConfigurationParser routeConfigurationParser) |
| 22 | + { |
| 23 | + _routeConfigurationParser = routeConfigurationParser ?? throw new ArgumentNullException(nameof(routeConfigurationParser)); |
| 24 | + } |
| 25 | + |
8 | 26 | /// <summary>
|
9 |
| - /// Provides extension methods to translate an <see cref="HttpContext"/> to different types of API Gateway requests. |
| 27 | + /// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayHttpApiV2ProxyRequest"/>. |
10 | 28 | /// </summary>
|
11 |
| - public static class HttpContextExtensions |
| 29 | + /// <param name="context">The <see cref="HttpContext"/> to be translated.</param> |
| 30 | + /// <returns>An <see cref="APIGatewayHttpApiV2ProxyRequest"/> object representing the translated request.</returns> |
| 31 | + public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request( |
| 32 | + this HttpContext context) |
12 | 33 | {
|
13 |
| - private static IHttpRequestUtility _httpRequestUtility = new HttpRequestUtility(); |
14 |
| - private static IRouteConfigurationParser _routeConfigurationParser; |
| 34 | + var request = context.Request; |
15 | 35 |
|
16 |
| - public static void SetHttpRequestUtility(IHttpRequestUtility httpRequestUtility) |
17 |
| - { |
18 |
| - _httpRequestUtility = httpRequestUtility ?? throw new ArgumentNullException(nameof(httpRequestUtility)); |
19 |
| - } |
| 36 | + var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); |
| 37 | + var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); |
20 | 38 |
|
21 |
| - public static void SetRouteConfigurationParser(IRouteConfigurationParser routeConfigurationParser) |
22 |
| - { |
23 |
| - _routeConfigurationParser = routeConfigurationParser ?? throw new ArgumentNullException(nameof(routeConfigurationParser)); |
24 |
| - } |
| 39 | + var (headers, _) = _httpRequestUtility.ExtractHeaders(request.Headers); |
| 40 | + var (queryStringParameters, _) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); |
25 | 41 |
|
26 |
| - /// <summary> |
27 |
| - /// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayHttpApiV2ProxyRequest"/>. |
28 |
| - /// </summary> |
29 |
| - /// <param name="context">The <see cref="HttpContext"/> to be translated.</param> |
30 |
| - /// <returns>An <see cref="APIGatewayHttpApiV2ProxyRequest"/> object representing the translated request.</returns> |
31 |
| - public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request( |
32 |
| - this HttpContext context) |
| 42 | + var httpApiV2ProxyRequest = new APIGatewayHttpApiV2ProxyRequest |
33 | 43 | {
|
34 |
| - var request = context.Request; |
35 |
| - |
36 |
| - var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); |
37 |
| - var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); |
38 |
| - |
39 |
| - var (headers, _) = _httpRequestUtility.ExtractHeaders(request.Headers); |
40 |
| - var (queryStringParameters, _) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); |
41 |
| - |
42 |
| - var httpApiV2ProxyRequest = new APIGatewayHttpApiV2ProxyRequest |
| 44 | + RouteKey = $"{request.Method} {matchedConfig.Path}", |
| 45 | + RawPath = request.Path, |
| 46 | + RawQueryString = request.QueryString.Value, |
| 47 | + Cookies = request.Cookies.Select(c => $"{c.Key}={c.Value}").ToArray(), |
| 48 | + Headers = headers, |
| 49 | + QueryStringParameters = queryStringParameters, |
| 50 | + PathParameters = pathParameters ?? new Dictionary<string, string>(), |
| 51 | + Body = _httpRequestUtility.ReadRequestBody(request), |
| 52 | + IsBase64Encoded = false, |
| 53 | + RequestContext = new ProxyRequestContext |
43 | 54 | {
|
44 |
| - RouteKey = $"{request.Method} {matchedConfig.Path}", |
45 |
| - RawPath = request.Path, |
46 |
| - RawQueryString = request.QueryString.Value, |
47 |
| - Cookies = request.Cookies.Select(c => $"{c.Key}={c.Value}").ToArray(), |
48 |
| - Headers = headers, |
49 |
| - QueryStringParameters = queryStringParameters, |
50 |
| - PathParameters = pathParameters ?? new Dictionary<string, string>(), |
51 |
| - Body = _httpRequestUtility.ReadRequestBody(request), |
52 |
| - IsBase64Encoded = false, |
53 |
| - RequestContext = new ProxyRequestContext |
| 55 | + Http = new HttpDescription |
54 | 56 | {
|
55 |
| - Http = new HttpDescription |
56 |
| - { |
57 |
| - Method = request.Method, |
58 |
| - Path = request.Path, |
59 |
| - Protocol = request.Protocol |
60 |
| - }, |
61 |
| - RouteKey = $"{request.Method} {matchedConfig.Path}" |
| 57 | + Method = request.Method, |
| 58 | + Path = request.Path, |
| 59 | + Protocol = request.Protocol |
62 | 60 | },
|
63 |
| - Version = "2.0" |
64 |
| - }; |
| 61 | + RouteKey = $"{request.Method} {matchedConfig.Path}" |
| 62 | + }, |
| 63 | + Version = "2.0" |
| 64 | + }; |
65 | 65 |
|
66 |
| - if (_httpRequestUtility.IsBinaryContent(request.ContentType)) |
67 |
| - { |
68 |
| - httpApiV2ProxyRequest.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(httpApiV2ProxyRequest.Body)); |
69 |
| - httpApiV2ProxyRequest.IsBase64Encoded = true; |
70 |
| - } |
71 |
| - |
72 |
| - return httpApiV2ProxyRequest; |
| 66 | + if (_httpRequestUtility.IsBinaryContent(request.ContentType)) |
| 67 | + { |
| 68 | + httpApiV2ProxyRequest.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(httpApiV2ProxyRequest.Body)); |
| 69 | + httpApiV2ProxyRequest.IsBase64Encoded = true; |
73 | 70 | }
|
74 | 71 |
|
75 |
| - /// <summary> |
76 |
| - /// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayProxyRequest"/>. |
77 |
| - /// </summary> |
78 |
| - /// <param name="context">The <see cref="HttpContext"/> to be translated.</param> |
79 |
| - /// <returns>An <see cref="APIGatewayProxyRequest"/> object representing the translated request.</returns> |
80 |
| - public static APIGatewayProxyRequest ToApiGatewayRequest( |
81 |
| - this HttpContext context) |
82 |
| - { |
83 |
| - var request = context.Request; |
| 72 | + return httpApiV2ProxyRequest; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayProxyRequest"/>. |
| 77 | + /// </summary> |
| 78 | + /// <param name="context">The <see cref="HttpContext"/> to be translated.</param> |
| 79 | + /// <returns>An <see cref="APIGatewayProxyRequest"/> object representing the translated request.</returns> |
| 80 | + public static APIGatewayProxyRequest ToApiGatewayRequest( |
| 81 | + this HttpContext context) |
| 82 | + { |
| 83 | + var request = context.Request; |
84 | 84 |
|
85 |
| - var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); |
86 |
| - var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); |
| 85 | + var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path); |
| 86 | + var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path); |
87 | 87 |
|
88 |
| - var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers); |
89 |
| - var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); |
| 88 | + var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers); |
| 89 | + var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query); |
90 | 90 |
|
91 |
| - var proxyRequest = new APIGatewayProxyRequest |
92 |
| - { |
93 |
| - Resource = matchedConfig.Path, |
94 |
| - Path = HttpUtility.UrlEncode(request.Path), |
95 |
| - HttpMethod = request.Method, |
96 |
| - Headers = headers, |
97 |
| - MultiValueHeaders = multiValueHeaders, |
98 |
| - QueryStringParameters = queryStringParameters, |
99 |
| - MultiValueQueryStringParameters = multiValueQueryStringParameters, |
100 |
| - PathParameters = pathParameters ?? new Dictionary<string, string>(), |
101 |
| - Body = _httpRequestUtility.ReadRequestBody(request), |
102 |
| - IsBase64Encoded = false |
103 |
| - }; |
104 |
| - |
105 |
| - if (_httpRequestUtility.IsBinaryContent(request.ContentType)) |
106 |
| - { |
107 |
| - proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body)); |
108 |
| - proxyRequest.IsBase64Encoded = true; |
109 |
| - } |
| 91 | + var proxyRequest = new APIGatewayProxyRequest |
| 92 | + { |
| 93 | + Resource = matchedConfig.Path, |
| 94 | + Path = HttpUtility.UrlEncode(request.Path), |
| 95 | + HttpMethod = request.Method, |
| 96 | + Headers = headers, |
| 97 | + MultiValueHeaders = multiValueHeaders, |
| 98 | + QueryStringParameters = queryStringParameters, |
| 99 | + MultiValueQueryStringParameters = multiValueQueryStringParameters, |
| 100 | + PathParameters = pathParameters ?? new Dictionary<string, string>(), |
| 101 | + Body = _httpRequestUtility.ReadRequestBody(request), |
| 102 | + IsBase64Encoded = false |
| 103 | + }; |
110 | 104 |
|
111 |
| - return proxyRequest; |
| 105 | + if (_httpRequestUtility.IsBinaryContent(request.ContentType)) |
| 106 | + { |
| 107 | + proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body)); |
| 108 | + proxyRequest.IsBase64Encoded = true; |
112 | 109 | }
|
| 110 | + |
| 111 | + return proxyRequest; |
113 | 112 | }
|
114 | 113 | }
|
0 commit comments