com.amazonaws.serverless.proxy.model.AwsProxyResponse Java Examples

The following examples show how to use com.amazonaws.serverless.proxy.model.AwsProxyResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamLambdaHandlerTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void ping_streamRequest_respondsWithHello() {
    InputStream requestStream = new AwsProxyRequestBuilder("/ping", HttpMethod.GET)
                                        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
                                        .buildStream();
    ByteArrayOutputStream responseStream = new ByteArrayOutputStream();

    handle(requestStream, responseStream);

    AwsProxyResponse response = readResponse(responseStream);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode());

    assertFalse(response.isBase64Encoded());

    assertTrue(response.getBody().contains("pong"));
    assertTrue(response.getBody().contains("Hello, World!"));

    assertTrue(response.getMultiValueHeaders().containsKey(HttpHeaders.CONTENT_TYPE));
    assertTrue(response.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON));
}
 
Example #2
Source File: Struts2AwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void queryParam_encoding_expectUnencodedParam() {
    assumeTrue("API_GW".equals(type));
    String paramValue = "p%2Fz%2B3";
    String decodedParam = "";
    try {
        decodedParam = URLDecoder.decode(paramValue, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        fail("Could not decode parameter");
    }
    AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET").queryString(QUERY_STRING_KEY, decodedParam);

    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, decodedParam);
}
 
Example #3
Source File: MicronautAwsProxyTest.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
@Test
public void static_resource() {
    AwsProxyRequest request = getRequestBuilder("/static-lorem/lorem.txt", "GET")
            .build();

    AwsProxyResponse resp = handler.proxy(request, lambdaContext);

    final String expectedBody = "Lorem ipsum";

    List<String> contentLength = resp.getMultiValueHeaders().get("Content-Length");

    assertEquals(200, resp.getStatusCode());
    assertEquals(expectedBody, resp.getBody());
    assertNotNull(contentLength);
    assertEquals(contentLength.get(0), String.valueOf(expectedBody.length()));
}
 
Example #4
Source File: MicronautAwsProxyTest.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
@Test
public void alb_basicRequest_expectSuccess() {
    AwsProxyRequest request = getRequestBuilder("/echo/headers", "GET")
            .json()
            .header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
            .alb()
            .build();

    AwsProxyResponse output = handler.proxy(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertEquals("application/json", output.getMultiValueHeaders().getFirst("Content-Type"));
    assertNotNull(output.getStatusDescription());
    System.out.println(output.getStatusDescription());

    validateMapResponseModel(output);
}
 
Example #5
Source File: AwsProxyRequestDispatcherTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void include_addsToResponse_appendsCorrectly() throws InvalidRequestEventException, IOException {
    final String firstPart = "first";
    final String secondPart = "second";
    AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder("/hello", "GET").build();

    AwsProxyResponse resp = mockLambdaHandler((AwsProxyHttpServletRequest req, AwsHttpServletResponse res)-> {
        if (req.getAttribute("cnt") == null) {
            res.getOutputStream().write(firstPart.getBytes());
            req.setAttribute("cnt", 1);
            req.getRequestDispatcher("/includer").include(req, res);
            res.setStatus(200);
            res.flushBuffer();
        } else {
            res.getOutputStream().write(secondPart.getBytes());
        }
    }).proxy(proxyRequest, new MockLambdaContext());
    assertEquals(firstPart + secondPart, resp.getBody());
}
 
Example #6
Source File: StreamLambdaHandlerTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void ping_streamRequest_respondsWithHello() {
    InputStream requestStream = new AwsProxyRequestBuilder("/ping", HttpMethod.GET)
                                        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
                                        .buildStream();
    ByteArrayOutputStream responseStream = new ByteArrayOutputStream();

    handle(requestStream, responseStream);

    AwsProxyResponse response = readResponse(responseStream);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode());

    assertFalse(response.isBase64Encoded());

    assertTrue(response.getBody().contains("pong"));
    assertTrue(response.getBody().contains("Hello, World!"));

    assertTrue(response.getMultiValueHeaders().containsKey(HttpHeaders.CONTENT_TYPE));
    assertTrue(response.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON));
}
 
Example #7
Source File: StreamLambdaHandlerTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void ping_streamRequest_respondsWithHello() {
    InputStream requestStream = new AwsProxyRequestBuilder("/ping", HttpMethod.GET)
                                        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
                                        .buildStream();
    ByteArrayOutputStream responseStream = new ByteArrayOutputStream();

    handle(requestStream, responseStream);

    AwsProxyResponse response = readResponse(responseStream);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode());

    assertFalse(response.isBase64Encoded());

    assertTrue(response.getBody().contains("pong"));
    assertTrue(response.getBody().contains("Hello, World!"));

    assertTrue(response.getMultiValueHeaders().containsKey(HttpHeaders.CONTENT_TYPE));
    assertTrue(response.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON));
}
 
Example #8
Source File: MicronautAwsProxyTest.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
@Test
public void error_statusCode_methodNotAllowed() throws ContainerInitializationException {
    MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler(
            ApplicationContext.build(CollectionUtils.mapOf(
                    "spec.name", "MicronautAwsProxyTest",
                    "micronaut.security.enabled", false,
                    "micronaut.views.handlebars.enabled", true,
                    "micronaut.router.static-resources.lorem.paths", "classpath:static-lorem/",
                    "micronaut.router.static-resources.lorem.mapping", "/static-lorem/**"
            ))
    );
    AwsProxyRequest request = getRequestBuilder("/echo/status-code", "POST")
            .json()
            .queryString("status", "201")
            .build();

    AwsProxyResponse output = handler.proxy(request, lambdaContext);
    assertEquals(405, output.getStatusCode());
}
 
Example #9
Source File: MicronautAwsProxyTest.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
@Test
public void errors_unknownRoute_expect404() throws ContainerInitializationException {
    MicronautLambdaContainerHandler handler = new MicronautLambdaContainerHandler(
            ApplicationContext.build(CollectionUtils.mapOf(
                    "spec.name", "MicronautAwsProxyTest",
                    "micronaut.security.enabled", false,
                    "micronaut.views.handlebars.enabled", true,
                    "micronaut.router.static-resources.lorem.paths", "classpath:static-lorem/",
                    "micronaut.router.static-resources.lorem.mapping", "/static-lorem/**"
            ))
    );
    AwsProxyRequest request = getRequestBuilder("/echo/test33", "GET").build();

    AwsProxyResponse output = handler.proxy(request, lambdaContext);
    assertEquals(404, output.getStatusCode());
}
 
Example #10
Source File: JerseyParamEncodingTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryParam_encoding_expectDecodedParam() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/decoded-param", "GET").queryString("param", SIMPLE_ENCODED_PARAM);

    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, SIMPLE_ENCODED_PARAM);
}
 
Example #11
Source File: SpringLambdaContainerHandler.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and sets the given profiles as active
 * @param applicationContext A custom ConfigurableWebApplicationContext to be used
 * @param profiles The spring profiles to activate
 * @return An initialized instance of the `SpringLambdaContainerHandler`
 * @throws ContainerInitializationException When the Spring framework fails to start.
 */
public static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext, String... profiles)
        throws ContainerInitializationException {
    return new SpringProxyHandlerBuilder<AwsProxyRequest>()
            .defaultProxy()
            .initializationWrapper(new InitializationWrapper())
            .springApplicationContext(applicationContext)
            .profiles(profiles)
            .buildAndInitialize();
}
 
Example #12
Source File: Struts2AwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void queryParam_encoding_expectEncodedParam() {
    assumeTrue("API_GW".equals(type));
    String paramValue = "p%2Fz%2B3";
    AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET").queryString(QUERY_STRING_KEY, paramValue);

    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, paramValue);
}
 
Example #13
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void stripBasePath_route_shouldReturn404() {
    AwsProxyRequestBuilder request = getRequestBuilder("/custompath/echo/status-code", "GET")
                                      .json()
                                      .queryString("status", "201");
    getHandler().stripBasePath("/custom");
    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(404, output.getStatusCode());
    getHandler().stripBasePath("");
}
 
Example #14
Source File: SpringBootLambdaContainerHandler.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and the given Spring profiles
 * @param springBootInitializer {@code SpringBootServletInitializer} class
 * @param profiles A list of Spring profiles to activate
 * @return An initialized instance of the `SpringLambdaContainerHandler`
 * @throws ContainerInitializationException If an error occurs while initializing the Spring framework
 */
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<?> springBootInitializer, String... profiles)
        throws ContainerInitializationException {
    return new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
            .defaultProxy()
            .initializationWrapper(new InitializationWrapper())
            .springBootApplication(springBootInitializer)
            .profiles(profiles)
            .buildAndInitialize();
}
 
Example #15
Source File: SecurityAppTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void helloRequest_withAuth_respondsWithSingleMessage() {
    AwsProxyRequest req = new AwsProxyRequestBuilder("/hello", "GET").build();
    AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
    assertEquals(401, resp.getStatusCode());
    assertTrue(resp.getMultiValueHeaders().containsKey(HttpHeaders.WWW_AUTHENTICATE));
    req = new AwsProxyRequestBuilder("/hello", "GET")
            .basicAuth(SecurityConfig.USERNAME, SecurityConfig.PASSWORD)
            .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN)
            .build();
    resp = handler.handleRequest(req, lambdaContext);
    assertEquals(200, resp.getStatusCode());
}
 
Example #16
Source File: JerseyParamEncodingTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void jsonQueryParam_encoding_expectDecodedParam() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/decoded-param", "GET").queryString("param", JSON_ENCODED_PARAM);

    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, JSON_ENCODED_PARAM);
}
 
Example #17
Source File: JerseyParamEncodingTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleQueryParam_encoding_expectEncodedParam() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/encoded-param", "GET").queryString("param", SIMPLE_ENCODED_PARAM);
    String encodedVal = "";
    try {
        encodedVal = URLEncoder.encode(SIMPLE_ENCODED_PARAM, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        fail("Could not encode parameter value");
    }
    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, encodedVal);
}
 
Example #18
Source File: JerseyParamEncodingTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void jsonQueryParam_encoding_expectEncodedParam() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/encoded-param", "GET").queryString("param", JSON_ENCODED_PARAM);
    String encodedVal = "";
    try {
        encodedVal = URLEncoder.encode(JSON_ENCODED_PARAM, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        fail("Could not encode parameter value");
    }
    AwsProxyResponse resp = executeRequest(request, lambdaContext);
    assertEquals(200, resp.getStatusCode());
    validateSingleValueModel(resp, encodedVal);
}
 
Example #19
Source File: SpringBootAppTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
private void validateSingleValueModel(AwsProxyResponse output, String value) {
    try {
        SingleValueModel response = mapper.readValue(output.getBody(), SingleValueModel.class);
        assertNotNull(response.getValue());
        assertEquals(value, response.getValue());
    } catch (IOException e) {
        e.printStackTrace();
        fail("Exception while parsing response body: " + e.getMessage());
    }
}
 
Example #20
Source File: HelloWorldSparkStreamTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void nullPathRequest_doesntFail_expectA404() {
    try {
        ByteArrayOutputStream outputStream = executeRequest(getRequestBuilder().method("GET").path(null), new MockLambdaContext());
        AwsProxyResponse response = LambdaContainerHandler.getObjectMapper().readValue(outputStream.toByteArray(), AwsProxyResponse.class);

        assertEquals(404, response.getStatusCode());
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }
}
 
Example #21
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void statusCode_responseStatusCode_customStatusCode() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/status-code", "GET")
            .json()
            .queryString("status", "201");

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(201, output.getStatusCode());
}
 
Example #22
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void responseBody_responseWriter_validBody() throws JsonProcessingException {
    SingleValueModel singleValueModel = new SingleValueModel();
    singleValueModel.setValue(CUSTOM_HEADER_VALUE);
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/json-body", "POST")
            .json()
            .body(objectMapper.writeValueAsString(singleValueModel));

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertNotNull(output.getBody());

    validateSingleValueModel(output, CUSTOM_HEADER_VALUE);
}
 
Example #23
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void error_statusCode_methodNotAllowed() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/status-code", "POST")
            .json()
            .queryString("status", "201");

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(405, output.getStatusCode());
}
 
Example #24
Source File: Struts2AwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void context_servletResponse_setCustomHeader() {
    AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET")
            .queryString("customHeader", "true")
            .json();

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertTrue(output.getMultiValueHeaders().containsKey("XX"));
}
 
Example #25
Source File: Struts2AwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void headers_getHeaders_echo() {
    AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
            .queryString("mode", "headers")
            .header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
            .json();

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertEquals(CONTENT_TYPE_APPLICATION_JSON, output.getMultiValueHeaders().getFirst("Content-Type"));

    validateMapResponseModel(output);
}
 
Example #26
Source File: Struts2AwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void error_statusCode_methodNotAllowed() {
    AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "POST")
            .queryString("mode", "not-allowed")
            .json();

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(405, output.getStatusCode());
}
 
Example #27
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void authorizer_securityContext_customPrincipalSuccess() {
    assumeTrue("API_GW".equals(type)); // TODO: We should figure out a way to run this for HTTP_API too
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/authorizer-principal", "GET")
            .json()
            .authorizerPrincipal(AUTHORIZER_PRINCIPAL_ID);

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertEquals("application/json", output.getMultiValueHeaders().getFirst("Content-Type"));
    validateSingleValueModel(output, AUTHORIZER_PRINCIPAL_ID);
}
 
Example #28
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void requestFilter_injectsServletRequest_expectCustomAttribute() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/filter-attribute", "GET")
                                      .json();

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertEquals("application/json", output.getMultiValueHeaders().getFirst("Content-Type"));

    validateSingleValueModel(output, ServletRequestFilter.FILTER_ATTRIBUTE_VALUE);
}
 
Example #29
Source File: JerseyAwsProxyTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void requestScheme_valid_expectHttps() {
    AwsProxyRequestBuilder request = getRequestBuilder("/echo/scheme", "GET")
                                      .json();

    AwsProxyResponse output = executeRequest(request, lambdaContext);
    assertEquals(200, output.getStatusCode());
    assertEquals("application/json", output.getMultiValueHeaders().getFirst("Content-Type"));

    validateSingleValueModel(output, "https");
}
 
Example #30
Source File: HelloWorldSparkTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void basicServer_handleRequest_emptyFilters() {
    AwsProxyRequest req = getRequestBuilder().method("GET").path("/hello").build();
    AwsProxyResponse response = handler.proxy(req, new MockLambdaContext());

    assertEquals(200, response.getStatusCode());
    assertTrue(response.getMultiValueHeaders().containsKey(CUSTOM_HEADER_KEY));
    assertEquals(CUSTOM_HEADER_VALUE, response.getMultiValueHeaders().getFirst(CUSTOM_HEADER_KEY));
    assertEquals(BODY_TEXT_RESPONSE, response.getBody());
}