|
1 | 1 | package com.amazonaws.serverless.proxy.model; |
2 | 2 |
|
| 3 | +import static junit.framework.TestCase.assertTrue; |
| 4 | +import static org.hamcrest.CoreMatchers.is; |
| 5 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | +import static org.junit.Assert.assertFalse; |
| 8 | +import static org.junit.Assert.assertNotNull; |
| 9 | +import java.io.IOException; |
| 10 | +import org.junit.Test; |
3 | 11 | import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; |
4 | 12 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | | -import org.junit.Test; |
6 | | - |
7 | | -import java.io.IOException; |
8 | | - |
9 | | -import static junit.framework.TestCase.assertTrue; |
10 | | -import static org.junit.Assert.*; |
11 | 13 |
|
12 | 14 | public class AwsProxyRequestTest { |
13 | 15 | private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header"; |
14 | 16 | private static final String CUSTOM_HEADER_VALUE = "123456"; |
15 | | - public static final String REQUEST_JSON = "{\n" + |
16 | | - " \"resource\": \"/api/{proxy+}\",\n" + |
17 | | - " \"path\": \"/api/endpoint\",\n" + |
18 | | - " \"httpMethod\": \"OPTIONS\",\n" + |
19 | | - " \"headers\": {\n" + |
20 | | - " \"Accept\": \"*/*\",\n" + |
21 | | - " \"User-Agent\": \"PostmanRuntime/7.1.1\",\n" + |
22 | | - " \"" + CUSTOM_HEADER_KEY_LOWER_CASE +"\":" + "\"" + CUSTOM_HEADER_VALUE + "\"\n" + |
23 | | - " },\n" + |
24 | | - " \"multiValueHeaders\": {\n" + |
25 | | - " \"Accept\": [\n" + |
26 | | - " \"*/*\"\n" + |
27 | | - " ],\n" + |
28 | | - " \"User-Agent\": [\n" + |
29 | | - " \"PostmanRuntime/7.1.1\"\n" + |
30 | | - " ],\n" + |
31 | | - " \"" + CUSTOM_HEADER_KEY_LOWER_CASE + "\": [\n" + |
32 | | - " \"" + CUSTOM_HEADER_VALUE + "\"\n" + |
33 | | - " ]\n" + |
34 | | - " },\n" + |
35 | | - " \"queryStringParameters\": null,\n" + |
36 | | - " \"multiValueQueryStringParameters\": null,\n" + |
37 | | - " \"pathParameters\": {\n" + |
38 | | - " \"proxy\": \"endpoint\"\n" + |
39 | | - " },\n" + |
40 | | - " \"stageVariables\": null,\n" + |
41 | | - " \"requestContext\": {\n" + |
42 | | - " \"resourceId\": null,\n" + |
43 | | - " \"resourcePath\": \"/api/{proxy+}\",\n" + |
44 | | - " \"httpMethod\": \"OPTIONS\",\n" + |
45 | | - " \"extendedRequestId\": null,\n" + |
46 | | - " \"requestTime\": \"15/Dec/2018:20:37:47 +0000\",\n" + |
47 | | - " \"path\": \"/api/endpoint\",\n" + |
48 | | - " \"accountId\": null,\n" + |
49 | | - " \"protocol\": \"HTTP/1.1\",\n" + |
50 | | - " \"stage\": \"stage_name\",\n" + |
51 | | - " \"domainPrefix\": null,\n" + |
52 | | - " \"requestTimeEpoch\": 1544906267828,\n" + |
53 | | - " \"requestId\": null,\n" + |
54 | | - " \"identity\": {\n" + |
55 | | - " \"cognitoIdentityPoolId\": null,\n" + |
56 | | - " \"accountId\": null,\n" + |
57 | | - " \"cognitoIdentityId\": null,\n" + |
58 | | - " \"caller\": null,\n" + |
59 | | - " \"sourceIp\": \"54.240.196.171\",\n" + |
60 | | - " \"accessKey\": null,\n" + |
61 | | - " \"cognitoAuthenticationType\": null,\n" + |
62 | | - " \"cognitoAuthenticationProvider\": null,\n" + |
63 | | - " \"userArn\": null,\n" + |
64 | | - " \"userAgent\": \"PostmanRuntime/7.1.1\",\n" + |
65 | | - " \"user\": null\n" + |
66 | | - " },\n" + |
67 | | - " \"domainName\": \"https://apiId.execute-api.eu-central-1.amazonaws.com/\",\n" + |
68 | | - " \"apiId\": \"apiId\"\n" + |
69 | | - " },\n" + |
70 | | - " \"body\": null,\n" + |
71 | | - " \"isBase64Encoded\": true\n" + |
72 | | - "}"; |
73 | 17 |
|
74 | 18 | @Test |
75 | 19 | public void deserialize_multiValuedHeaders_caseInsensitive() throws IOException { |
@@ -160,4 +104,35 @@ private String getRequestJson(boolean base64Encoded, String headerKey, String he |
160 | 104 | " \"isBase64Encoded\": " + (base64Encoded?"true":"false") + "\n" + |
161 | 105 | "}"; |
162 | 106 | } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void deserialize_singleValuedHeaders() throws IOException { |
| 110 | + AwsProxyRequest req = |
| 111 | + new AwsProxyRequestBuilder().fromJsonString(getSingleValueRequestJson()).build(); |
| 112 | + |
| 113 | + assertThat(req.getHeaders().get("accept"), is("*")); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Captured from a live request to an ALB with a Lambda integration with |
| 118 | + * lambda.multi_value_headers.enabled=false. |
| 119 | + */ |
| 120 | + private String getSingleValueRequestJson() { |
| 121 | + return "{\n" + " \"requestContext\": {\n" + " \"elb\": {\n" |
| 122 | + + " \"targetGroupArn\": \"arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/prod-example-function/e77803ebb6d2c24\"\n" |
| 123 | + + " }\n" + " },\n" + " \"httpMethod\": \"PUT\",\n" |
| 124 | + + " \"path\": \"/path/to/resource\",\n" + " \"queryStringParameters\": {},\n" |
| 125 | + + " \"headers\": {\n" + " \"accept\": \"*\",\n" |
| 126 | + + " \"content-length\": \"17\",\n" |
| 127 | + + " \"content-type\": \"application/json\",\n" |
| 128 | + + " \"host\": \"stackoverflow.name\",\n" |
| 129 | + + " \"user-agent\": \"curl/7.77.0\",\n" |
| 130 | + + " \"x-amzn-trace-id\": \"Root=1-62e22402-3a5f246225e45edd7735c182\",\n" |
| 131 | + + " \"x-forwarded-for\": \"24.14.13.186\",\n" |
| 132 | + + " \"x-forwarded-port\": \"443\",\n" |
| 133 | + + " \"x-forwarded-proto\": \"https\",\n" |
| 134 | + + " \"x-jersey-tracing-accept\": \"true\"\n" + " },\n" |
| 135 | + + " \"body\": \"{\\\"alpha\\\":\\\"bravo\\\"}\",\n" |
| 136 | + + " \"isBase64Encoded\": false\n" + "} \n"; |
| 137 | + } |
163 | 138 | } |
0 commit comments