|
1 | 1 | package com.amazonaws.serverless.proxy.model; |
2 | 2 |
|
3 | 3 | import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import org.junit.Test; |
5 | 6 |
|
6 | 7 | import java.io.IOException; |
7 | 8 |
|
8 | | -import static org.junit.Assert.assertEquals; |
9 | | -import static org.junit.Assert.assertNotNull; |
| 9 | +import static junit.framework.TestCase.assertTrue; |
| 10 | +import static org.junit.Assert.*; |
10 | 11 |
|
11 | 12 | public class AwsProxyRequestTest { |
12 | 13 | private static final String CUSTOM_HEADER_KEY_LOWER_CASE = "custom-header"; |
@@ -67,13 +68,96 @@ public class AwsProxyRequestTest { |
67 | 68 | " \"apiId\": \"apiId\"\n" + |
68 | 69 | " },\n" + |
69 | 70 | " \"body\": null,\n" + |
70 | | - " \"isBase64Encoded\": false\n" + |
| 71 | + " \"isBase64Encoded\": true\n" + |
71 | 72 | "}"; |
72 | 73 |
|
73 | 74 | @Test |
74 | 75 | public void deserialize_multiValuedHeaders_caseInsensitive() throws IOException { |
75 | | - AwsProxyRequest req = new AwsProxyRequestBuilder().fromJsonString(REQUEST_JSON).build(); |
| 76 | + AwsProxyRequest req = new AwsProxyRequestBuilder() |
| 77 | + .fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build(); |
76 | 78 | assertNotNull(req.getMultiValueHeaders().get(CUSTOM_HEADER_KEY_LOWER_CASE.toUpperCase())); |
77 | 79 | assertEquals(CUSTOM_HEADER_VALUE, req.getMultiValueHeaders().get(CUSTOM_HEADER_KEY_LOWER_CASE.toUpperCase()).get(0)); |
| 80 | + assertTrue(req.isBase64Encoded()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void deserialize_base64Encoded_readsBoolCorrectly() throws IOException { |
| 85 | + AwsProxyRequest req = new AwsProxyRequestBuilder() |
| 86 | + .fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build(); |
| 87 | + assertTrue(req.isBase64Encoded()); |
| 88 | + req = new AwsProxyRequestBuilder() |
| 89 | + .fromJsonString(getRequestJson(false, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build(); |
| 90 | + assertFalse(req.isBase64Encoded()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void serialize_base64Encoded_fieldContainsIsPrefix() throws IOException { |
| 95 | + AwsProxyRequest req = new AwsProxyRequestBuilder() |
| 96 | + .fromJsonString(getRequestJson(true, CUSTOM_HEADER_KEY_LOWER_CASE, CUSTOM_HEADER_VALUE)).build(); |
| 97 | + ObjectMapper mapper = new ObjectMapper(); |
| 98 | + String serializedRequest = mapper.writeValueAsString(req); |
| 99 | + System.out.println(serializedRequest); |
| 100 | + assertTrue(serializedRequest.contains("\"isBase64Encoded\":true")); |
| 101 | + } |
| 102 | + |
| 103 | + private String getRequestJson(boolean base64Encoded, String headerKey, String headerValue) { |
| 104 | + return "{\n" + |
| 105 | + " \"resource\": \"/api/{proxy+}\",\n" + |
| 106 | + " \"path\": \"/api/endpoint\",\n" + |
| 107 | + " \"httpMethod\": \"OPTIONS\",\n" + |
| 108 | + " \"headers\": {\n" + |
| 109 | + " \"Accept\": \"*/*\",\n" + |
| 110 | + " \"User-Agent\": \"PostmanRuntime/7.1.1\",\n" + |
| 111 | + " \"" + headerKey +"\":" + "\"" + headerValue + "\"\n" + |
| 112 | + " },\n" + |
| 113 | + " \"multiValueHeaders\": {\n" + |
| 114 | + " \"Accept\": [\n" + |
| 115 | + " \"*/*\"\n" + |
| 116 | + " ],\n" + |
| 117 | + " \"User-Agent\": [\n" + |
| 118 | + " \"PostmanRuntime/7.1.1\"\n" + |
| 119 | + " ],\n" + |
| 120 | + " \"" + headerKey + "\": [\n" + |
| 121 | + " \"" + headerValue + "\"\n" + |
| 122 | + " ]\n" + |
| 123 | + " },\n" + |
| 124 | + " \"queryStringParameters\": null,\n" + |
| 125 | + " \"multiValueQueryStringParameters\": null,\n" + |
| 126 | + " \"pathParameters\": {\n" + |
| 127 | + " \"proxy\": \"endpoint\"\n" + |
| 128 | + " },\n" + |
| 129 | + " \"stageVariables\": null,\n" + |
| 130 | + " \"requestContext\": {\n" + |
| 131 | + " \"resourceId\": null,\n" + |
| 132 | + " \"resourcePath\": \"/api/{proxy+}\",\n" + |
| 133 | + " \"httpMethod\": \"OPTIONS\",\n" + |
| 134 | + " \"extendedRequestId\": null,\n" + |
| 135 | + " \"requestTime\": \"15/Dec/2018:20:37:47 +0000\",\n" + |
| 136 | + " \"path\": \"/api/endpoint\",\n" + |
| 137 | + " \"accountId\": null,\n" + |
| 138 | + " \"protocol\": \"HTTP/1.1\",\n" + |
| 139 | + " \"stage\": \"stage_name\",\n" + |
| 140 | + " \"domainPrefix\": null,\n" + |
| 141 | + " \"requestTimeEpoch\": 1544906267828,\n" + |
| 142 | + " \"requestId\": null,\n" + |
| 143 | + " \"identity\": {\n" + |
| 144 | + " \"cognitoIdentityPoolId\": null,\n" + |
| 145 | + " \"accountId\": null,\n" + |
| 146 | + " \"cognitoIdentityId\": null,\n" + |
| 147 | + " \"caller\": null,\n" + |
| 148 | + " \"sourceIp\": \"54.240.196.171\",\n" + |
| 149 | + " \"accessKey\": null,\n" + |
| 150 | + " \"cognitoAuthenticationType\": null,\n" + |
| 151 | + " \"cognitoAuthenticationProvider\": null,\n" + |
| 152 | + " \"userArn\": null,\n" + |
| 153 | + " \"userAgent\": \"PostmanRuntime/7.1.1\",\n" + |
| 154 | + " \"user\": null\n" + |
| 155 | + " },\n" + |
| 156 | + " \"domainName\": \"https://apiId.execute-api.eu-central-1.amazonaws.com/\",\n" + |
| 157 | + " \"apiId\": \"apiId\"\n" + |
| 158 | + " },\n" + |
| 159 | + " \"body\": null,\n" + |
| 160 | + " \"isBase64Encoded\": " + (base64Encoded?"true":"false") + "\n" + |
| 161 | + "}"; |
78 | 162 | } |
79 | 163 | } |
0 commit comments