Fix Request/Response attributes, add more assertions in tests

This commit is contained in:
Oleg Zhurakousky
2023-03-07 14:30:00 +01:00
parent 0c82bca08a
commit 017093a8b5
14 changed files with 282 additions and 597 deletions

View File

@@ -19,31 +19,25 @@ package org.springframework.cloud.function.adapter.aws.web;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.Filter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest;
import org.springframework.cloud.function.serverless.web.ProxyHttpServletResponse;
import org.springframework.cloud.function.serverless.web.ProxyMvc;
import org.springframework.cloud.function.serverless.web.ProxyServletContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
*
@@ -74,10 +68,9 @@ public class WebProxyInvoker {
*/
@SuppressWarnings("unchecked")
private HttpServletRequest prepareRequest(InputStream input) throws IOException {
Map<String, Object> request = mapper.readValue(input, Map.class);
if (logger.isDebugEnabled()) {
logger.debug("Request: " + request);
if (logger.isInfoEnabled()) {
logger.info("Request: " + request);
}
String httpMethod = (String) request.get("httpMethod");
String path = (String) request.get("path");
@@ -86,18 +79,29 @@ public class WebProxyInvoker {
logger.debug("path: " + path);
}
ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(null, httpMethod, path);
// CONTENT
if (StringUtils.hasText((String) request.get("body"))) {
httpRequest.setContent(((String) request.get("body")).getBytes());
}
if (request.get("queryStringParameters") != null) {
httpRequest.setParameters((Map<String, ?>) request.get("queryStringParameters"));
// REQUEST PARAM
if (request.get("multiValueQueryStringParameters") != null) {
Map<String, List<String>> parameters = (Map<String, List<String>>) request.get("multiValueQueryStringParameters");
for (Entry<String, List<String>> parameter : parameters.entrySet()) {
httpRequest.setParameter(parameter.getKey(), parameter.getValue().toArray(new String[] {}));
}
}
Map<String, Object> headers = (Map<String, Object>) request.get("headers");
headers.putAll((Map<String, Object>) request.get("multiValueHeaders"));
for (Entry<String, Object> entry : headers.entrySet()) {
httpRequest.addHeader(entry.getKey(), entry.getValue());
// HEADERS
Map<String, List<String>> headers = (Map<String, List<String>>) request.get("multiValueHeaders");
HttpHeaders httpHeaders = new HttpHeaders();
for (Entry<String, List<String>> entry : headers.entrySet()) {
// TODO may need to do some header formatting
httpHeaders.addAll(entry.getKey(), entry.getValue());
}
httpRequest.setHeaders(httpHeaders);
return httpRequest;
}
@@ -121,47 +125,20 @@ public class WebProxyInvoker {
}
Map<String, Object> apiGatewayResponseStructure = new HashMap<String, Object>();
apiGatewayResponseStructure.put("isBase64Encoded", false);
apiGatewayResponseStructure.put("statusCode", 200);
apiGatewayResponseStructure.put("statusCode", HttpStatus.OK.value());
apiGatewayResponseStructure.put("body", responseString);
Map<String, List<String>> multiValueHeaders = new HashMap<>();
Map<String, String> headers = new HashMap<>();
for (String headerName : httpResponse.getHeaderNames()) {
multiValueHeaders.put(headerName, httpResponse.getHeaders(headerName));
headers.put(headerName, httpResponse.getHeaders(headerName).toString());
}
// TODO investigate why AWS doesn't like List as value
// apiGatewayResponseStructure.put("headers", multiValueHeaders);
apiGatewayResponseStructure.put("multiValueHeaders", multiValueHeaders);
apiGatewayResponseStructure.put("headers", headers);
byte[] apiGatewayResponseBytes = mapper.writeValueAsBytes(apiGatewayResponseStructure);
StreamUtils.copy(apiGatewayResponseBytes, output);
}
}
private static class ProxyServletConfig implements ServletConfig {
private final ServletContext servletContext;
ProxyServletConfig(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public String getServletName() {
return "serverless-proxy";
}
@Override
public ServletContext getServletContext() {
return this.servletContext;
}
@Override
public Enumeration<String> getInitParameterNames() {
return Collections.enumeration(new ArrayList<String>());
}
@Override
public String getInitParameter(String name) {
return null;
}
}
}

View File

@@ -19,106 +19,157 @@ package org.springframework.cloud.function.adapter.aws.web;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Oleg Zhurakousky
*/
public class WebProxyInvokerTests {
static String apiGatewayEvent = "{\n" +
" \"resource\": \"/pets\",\n" +
" \"path\": \"/pets/64f56d94-a059-4111-9eeb-ee0c994b1ba8?foo=bar\",\n" +
" \"httpMethod\": \"GET\",\n" +
" \"headers\": {\n" +
" \"accept\": \"*/*\",\n" +
" \"content-type\": \"application/json\",\n" +
" \"Host\": \"fhul32ccy2.execute-api.eu-west-3.amazonaws.com\",\n" +
" \"User-Agent\": \"curl/7.54.0\",\n" +
" \"X-Amzn-Trace-Id\": \"Root=1-5ece339e-e0595766066d703ec70f1522\",\n" +
" \"X-Forwarded-For\": \"90.37.8.133\",\n" +
" \"X-Forwarded-Port\": \"443\",\n" +
" \"X-Forwarded-Proto\": \"https\"\n" +
" },\n" +
" \"multiValueHeaders\": {\n" +
" \"accept\": [\n" +
" \"*/*\"\n" +
" ],\n" +
" \"content-type\": [\n" +
" \"application/json\"\n" +
" ],\n" +
" \"Host\": [\n" +
" \"fhul32ccy2.execute-api.eu-west-3.amazonaws.com\"\n" +
" ],\n" +
" \"User-Agent\": [\n" +
" \"curl/7.54.0\"\n" +
" ],\n" +
" \"X-Amzn-Trace-Id\": [\n" +
" \"Root=1-5ece339e-e0595766066d703ec70f1522\"\n" +
" ],\n" +
" \"X-Forwarded-For\": [\n" +
" \"90.37.8.133\"\n" +
" ],\n" +
" \"X-Forwarded-Port\": [\n" +
" \"443\"\n" +
" ],\n" +
" \"X-Forwarded-Proto\": [\n" +
" \"https\"\n" +
" ]\n" +
" },\n" +
" \"queryStringParameters\": null,\n" +
" \"multiValueQueryStringParameters\": null,\n" +
" \"pathParameters\": null,\n" +
" \"stageVariables\": null,\n" +
" \"requestContext\": {\n" +
" \"resourceId\": \"qf0io6\",\n" +
" \"resourcePath\": \"/pets\",\n" +
" \"httpMethod\": \"GET\",\n" +
" \"extendedRequestId\": \"NL0A1EokCGYFZOA=\",\n" +
" \"requestTime\": \"27/May/2020:09:32:14 +0000\",\n" +
" \"path\": \"/test/uppercase2\",\n" +
" \"accountId\": \"123456789098\",\n" +
" \"protocol\": \"HTTP/1.1\",\n" +
" \"stage\": \"test\",\n" +
" \"domainPrefix\": \"fhul32ccy2\",\n" +
" \"requestTimeEpoch\": 1590571934872,\n" +
" \"requestId\": \"b96500aa-f92a-43c3-9360-868ba4053a00\",\n" +
" \"identity\": {\n" +
" \"cognitoIdentityPoolId\": null,\n" +
" \"accountId\": null,\n" +
" \"cognitoIdentityId\": null,\n" +
" \"caller\": null,\n" +
" \"sourceIp\": \"90.37.8.133\",\n" +
" \"principalOrgId\": null,\n" +
" \"accessKey\": null,\n" +
" \"cognitoAuthenticationType\": null,\n" +
" \"cognitoAuthenticationProvider\": null,\n" +
" \"userArn\": null,\n" +
" \"userAgent\": \"curl/7.54.0\",\n" +
" \"user\": null\n" +
" },\n" +
" \"domainName\": \"fhul32ccy2.execute-api.eu-west-3.amazonaws.com\",\n" +
" \"apiId\": \"fhul32ccy2\"\n" +
" },\n" +
" \"body\":\"\",\n" +
" \"isBase64Encoded\": false\n" +
"}";
static String API_GATEWAY_EVENT = "{\n"
+ " \"version\": \"1.0\",\n"
+ " \"resource\": \"$default\",\n"
+ " \"path\": \"/pets\",\n"
+ " \"httpMethod\": \"POST\",\n"
+ " \"headers\": {\n"
+ " \"Content-Length\": \"45\",\n"
+ " \"Content-Type\": \"application/json\",\n"
+ " \"Host\": \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\",\n"
+ " \"User-Agent\": \"curl/7.79.1\",\n"
+ " \"X-Amzn-Trace-Id\": \"Root=1-64087690-2151375b219d3ba3389ea84e\",\n"
+ " \"X-Forwarded-For\": \"109.210.252.44\",\n"
+ " \"X-Forwarded-Port\": \"443\",\n"
+ " \"X-Forwarded-Proto\": \"https\",\n"
+ " \"accept\": \"*/*\"\n"
+ " },\n"
+ " \"multiValueHeaders\": {\n"
+ " \"Content-Length\": [\n"
+ " \"45\"\n"
+ " ],\n"
+ " \"Content-Type\": [\n"
+ " \"application/json\"\n"
+ " ],\n"
+ " \"Host\": [\n"
+ " \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\"\n"
+ " ],\n"
+ " \"User-Agent\": [\n"
+ " \"curl/7.79.1\"\n"
+ " ],\n"
+ " \"X-Amzn-Trace-Id\": [\n"
+ " \"Root=1-64087690-2151375b219d3ba3389ea84e\"\n"
+ " ],\n"
+ " \"X-Forwarded-For\": [\n"
+ " \"109.210.252.44\"\n"
+ " ],\n"
+ " \"X-Forwarded-Port\": [\n"
+ " \"443\"\n"
+ " ],\n"
+ " \"X-Forwarded-Proto\": [\n"
+ " \"https\"\n"
+ " ],\n"
+ " \"accept\": [\n"
+ " \"*/*\"\n"
+ " ]\n"
+ " },\n"
+ " \"queryStringParameters\": {\n"
+ " \"abc\": \"xyz\",\n"
+ " \"foo\": \"baz\"\n"
+ " },\n"
+ " \"multiValueQueryStringParameters\": {\n"
+ " \"abc\": [\n"
+ " \"xyz\"\n"
+ " ],\n"
+ " \"foo\": [\n"
+ " \"bar\",\n"
+ " \"baz\"\n"
+ " ]\n"
+ " },\n"
+ " \"requestContext\": {\n"
+ " \"accountId\": \"123456789098\",\n"
+ " \"apiId\": \"i76bfhczs0\",\n"
+ " \"domainName\": \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\",\n"
+ " \"domainPrefix\": \"i76bfhczs0\",\n"
+ " \"extendedRequestId\": \"Bdd2ngt5iGYEMIg=\",\n"
+ " \"httpMethod\": \"POST\",\n"
+ " \"identity\": {\n"
+ " \"accessKey\": null,\n"
+ " \"accountId\": null,\n"
+ " \"caller\": null,\n"
+ " \"cognitoAmr\": null,\n"
+ " \"cognitoAuthenticationProvider\": null,\n"
+ " \"cognitoAuthenticationType\": null,\n"
+ " \"cognitoIdentityId\": null,\n"
+ " \"cognitoIdentityPoolId\": null,\n"
+ " \"principalOrgId\": null,\n"
+ " \"sourceIp\": \"109.210.252.44\",\n"
+ " \"user\": null,\n"
+ " \"userAgent\": \"curl/7.79.1\",\n"
+ " \"userArn\": null\n"
+ " },\n"
+ " \"path\": \"/pets\",\n"
+ " \"protocol\": \"HTTP/1.1\",\n"
+ " \"requestId\": \"Bdd2ngt5iGYEMIg=\",\n"
+ " \"requestTime\": \"08/Mar/2023:11:50:40 +0000\",\n"
+ " \"requestTimeEpoch\": 1678276240455,\n"
+ " \"resourceId\": \"$default\",\n"
+ " \"resourcePath\": \"$default\",\n"
+ " \"stage\": \"$default\"\n"
+ " },\n"
+ " \"pathParameters\": null,\n"
+ " \"stageVariables\": null,\n"
+ " \"body\": \"{\\\"id\\\":\\\"123\\\",\\\"breed\\\":\\\"Datsun\\\",\\\"name\\\":\\\"Donald\\\"}\",\n"
+ " \"isBase64Encoded\": false\n"
+ "}";
@Test
public void validateHttpServletRequestConstruction() throws Exception {
System.setProperty("MAIN_CLASS", PetStoreSpringAppConfig.class.getName());
WebProxyInvoker invoker = new WebProxyInvoker();
InputStream targetStream = new ByteArrayInputStream(API_GATEWAY_EVENT.getBytes());
Method prepareRequest = ReflectionUtils.findMethod(WebProxyInvoker.class, "prepareRequest", InputStream.class);
prepareRequest.setAccessible(true);
ProxyHttpServletRequest request = (ProxyHttpServletRequest) prepareRequest.invoke(invoker, targetStream);
assertThat(request.getContentType()).isEqualTo("application/json");
assertThat(request.getParameterValues("foo").length).isEqualTo(2);
assertThat(request.getParameterValues("foo")[0]).isEqualTo("bar");
assertThat(request.getParameterValues("abc").length).isEqualTo(1);
assertThat(request.getParameterValues("abc")[0]).isEqualTo("xyz");
assertThat(request.getHeaders(HttpHeaders.CONTENT_TYPE).nextElement()).isEqualTo("application/json");
assertThat(request.getContentAsString()).isEqualTo("{\"id\":\"123\",\"breed\":\"Datsun\",\"name\":\"Donald\"}");
assertThat(request.getContentLength()).isEqualTo(45);
assertThat(request.getMethod()).isEqualTo("POST");
}
@Test
public void testApiGatewayProxy() throws Exception {
System.setProperty("MAIN_CLASS", PetStoreSpringAppConfig.class.getName());
WebProxyInvoker invoker = new WebProxyInvoker();
InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes());
InputStream targetStream = new ByteArrayInputStream(API_GATEWAY_EVENT.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output);
ObjectMapper mapper = new ObjectMapper();
System.out.println("RESULT: =======> " + new String(output.toByteArray()));
Map result = mapper.readValue(output.toByteArray(), Map.class);
System.out.println(result);
assertThat((boolean) result.get("isBase64Encoded")).isFalse();
assertThat(((Map<String, List<String>>) result.get("multiValueHeaders")).get("Content-Type").get(0)).isEqualTo("application/json");
assertThat(result.get("statusCode")).isEqualTo(200);
Pet pet = mapper.readValue((String) result.get("body"), Pet.class);
assertThat(pet.getName()).isEqualTo("Donald");
}
}