GatewayRequestHandler extends SimpleRequestHandler<GatewayRequestAndLambdaContext, GatewayResponse> { @Override protected JRestlessContainerRequest createContainerRequest(GatewayRequestAndLambdaContext requestAndLambdaContext) { requireNonNull(requestAndLambdaContext); GatewayRequest request = requestAndLambdaContext.getGatewayRequest(); requireNonNull(request); requireNonNull(request.getPath()); String body = request.getBody(); InputStream entityStream; if (body != null) { entityStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)); } else { entityStream = new ByteArrayInputStream(new byte[0]); } RequestAndBaseUri requestAndBaseUri = getRequestAndBaseUri(requestAndLambdaContext); LOG.trace("passing baseUri='{}' and requestUri='{}' to the container", requestAndBaseUri.getBaseUri(), requestAndBaseUri.getRequestUri()); return new DefaultJRestlessContainerRequest(requestAndBaseUri, request.getHttpMethod(), entityStream, HeaderUtils.expandHeaders(request.getHeaders())); } protected  GatewayRequestHandler();   }
@Test public void createContainerRequest_NoPathGiven_ShouldThrowNpe() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setPath(null); assertThrows(NullPointerException.class, () -> gatewayHandler.createContainerRequest(request)); }
@Test public void createContainerRequest_NoHttpMethodGiven_ShouldThrowNpe() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setHttpMethod(null); assertThrows(NullPointerException.class, () -> gatewayHandler.createContainerRequest(request)); }
@Test public void createContainerRequest_NoBodyGiven_ShouldUseEmptyBaos() { GatewayRequestAndLambdaContext request = createMinimalRequest(); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); InputStream is = containerRequest.getEntityStream(); assertEquals(ByteArrayInputStream.class, is.getClass()); assertEquals("", toString((ByteArrayInputStream) is)); }
@Test public void createContainerRequest_BodyGiven_ShouldUseBody() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setBody("abc"); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); InputStream is = containerRequest.getEntityStream(); assertEquals(ByteArrayInputStream.class, is.getClass()); assertEquals("abc", toString((ByteArrayInputStream) is)); }
@Test public void createContainerRequest_HttpMethodGiven_ShouldUseHttpMethod() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setHttpMethod("POST"); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals("POST", containerRequest.getHttpMethod()); }
@Test public void createContainerRequest_PathWithNoQueryParamsGiven_ShouldUsePathAsRequestUri() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setPath("/abc"); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(URI.create("/abc"), containerRequest.getRequestUri()); }
@Test public void createContainerRequest_PathWithOneQueryParamsGiven_ShouldUseQueryParamsInRequestUri() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setPath("/abc"); ((DefaultGatewayRequest) request.getGatewayRequest()).setQueryStringParameters(ImmutableMap.of("a_k", "a_v")); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(URI.create("/abc?a_k=a_v"), containerRequest.getRequestUri()); }
@Test public void createContainerRequest_PathWithMultipleQueryParamsGiven_ShouldUseQueryParamsInRequestUri() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setPath("/abc"); ((DefaultGatewayRequest) request.getGatewayRequest()).setQueryStringParameters(ImmutableMap.of("a_k", "a_v", "b_k", "b_v")); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(URI.create("/abc?a_k=a_v&b_k=b_v"), containerRequest.getRequestUri()); }
@Test public void createContainerRequest_HeadersGiven_ShouldUseHeaders() { GatewayRequestAndLambdaContext request = createMinimalRequest(); ((DefaultGatewayRequest) request.getGatewayRequest()).setHeaders(ImmutableMap.of("a_k", "a_v", "b_k", "b_v")); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(ImmutableMap.of("a_k", singletonList("a_v"), "b_k", singletonList("b_v")), containerRequest.getHeaders()); }
@Test public void createContainerRequest_NullHeaderKeyGiven_ShouldFilterHeader() { GatewayRequestAndLambdaContext request = createMinimalRequest(); Map<String, String> headers = new HashMap<>(); headers.put(null, "a_v"); headers.put("b_k", "b_v"); ((DefaultGatewayRequest) request.getGatewayRequest()).setHeaders(headers); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(ImmutableMap.of("b_k", singletonList("b_v")), containerRequest.getHeaders()); }
@Test public void createContainerRequest_NullHeaderValueGiven_ShouldFilterHeader() { GatewayRequestAndLambdaContext request = createMinimalRequest(); Map<String, String> headers = new HashMap<>(); headers.put("a_k", null); headers.put("b_k", "b_v"); ((DefaultGatewayRequest) request.getGatewayRequest()).setHeaders(headers); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(ImmutableMap.of("b_k", singletonList("b_v")), containerRequest.getHeaders()); }
@Test public void createContainerRequest_CommaSeparatedHeaderValueGiven_ShouldNotSpreadHeader() { GatewayRequestAndLambdaContext request = createMinimalRequest(); Map<String, String> headers = new HashMap<>(); headers.put("a_k", "a_v0,a_v1"); ((DefaultGatewayRequest) request.getGatewayRequest()).setHeaders(headers); JRestlessContainerRequest containerRequest = gatewayHandler.createContainerRequest(request); assertEquals(ImmutableMap.of("a_k", singletonList("a_v0,a_v1")), containerRequest.getHeaders()); }