Search in sources :

Example 1 with HttpResponse

use of com.google.cloud.functions.HttpResponse in project spring-cloud-function by spring-cloud.

the class FunctionInvokerHttpTests method testHttpFunction.

private <I, O> void testHttpFunction(Class<?> configurationClass, I input, O expectedOutput) throws Exception {
    try (FunctionInvoker handler = new FunctionInvoker(configurationClass)) {
        HttpRequest request = Mockito.mock(HttpRequest.class);
        if (input != null) {
            when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(input))));
        }
        HttpResponse response = Mockito.mock(HttpResponse.class);
        StringWriter writer = new StringWriter();
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        when(response.getWriter()).thenReturn(bufferedWriter);
        handler.service(request, response);
        // Closing the writer is done by the Framework/caller.
        bufferedWriter.close();
        if (expectedOutput != null) {
            assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
        }
    }
}
Also used : HttpRequest(com.google.cloud.functions.HttpRequest) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) HttpResponse(com.google.cloud.functions.HttpResponse) BufferedWriter(java.io.BufferedWriter)

Aggregations

HttpRequest (com.google.cloud.functions.HttpRequest)1 HttpResponse (com.google.cloud.functions.HttpResponse)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1