working on comments
This commit is contained in:
@@ -140,7 +140,7 @@ curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "h
|
|||||||
Setting custom HTTP statusCode:
|
Setting custom HTTP statusCode:
|
||||||
|
|
||||||
----
|
----
|
||||||
Users can specify the response statusCode that they want to set by wrapping "statusCode" in headers.
|
Users can specify the response statusCode that they want to set by wrapping "statusCode" in headers. This constant is available to users by calling `FunctionInvoker.HTTP_STATUS_CODE`
|
||||||
----
|
----
|
||||||
|
|
||||||
[source, java]
|
[source, java]
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ public class FunctionInvoker extends AbstractSpringFunctionAdapterInitializer<Ht
|
|||||||
private static final Log log = LogFactory.getLog(FunctionInvoker.class);
|
private static final Log log = LogFactory.getLog(FunctionInvoker.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant specifying Http Status Code.
|
* Constant specifying Http Status Code. Accessible to users by calling 'FunctionInvoker.HTTP_STATUS_CODE'
|
||||||
*/
|
*/
|
||||||
public static final String httpStatusCode = "statusCode";
|
public static final String HTTP_STATUS_CODE = "statusCode";
|
||||||
|
|
||||||
private String functionName = "";
|
private String functionName = "";
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ public class FunctionInvoker extends AbstractSpringFunctionAdapterInitializer<Ht
|
|||||||
Function<Message<BufferedReader>, Message<byte[]>> function = lookupFunction();
|
Function<Message<BufferedReader>, Message<byte[]>> function = lookupFunction();
|
||||||
|
|
||||||
Message<BufferedReader> message = getInputType() == Void.class || getInputType() == null ? null
|
Message<BufferedReader> message = getInputType() == Void.class || getInputType() == null ? null
|
||||||
: MessageBuilder.withPayload(httpRequest.getReader()).copyHeaders(httpRequest.getHeaders()).build();
|
: MessageBuilder.withPayload(httpRequest.getReader()).copyHeaders(httpRequest.getHeaders()).build();
|
||||||
|
|
||||||
Message<byte[]> result = function.apply(message);
|
Message<byte[]> result = function.apply(message);
|
||||||
|
|
||||||
@@ -122,11 +122,13 @@ public class FunctionInvoker extends AbstractSpringFunctionAdapterInitializer<Ht
|
|||||||
}
|
}
|
||||||
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
|
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
|
||||||
|
|
||||||
if (headers.containsKey(httpStatusCode) && (headers.get(httpStatusCode) instanceof Integer)) {
|
if (headers.containsKey(HTTP_STATUS_CODE)) {
|
||||||
httpResponse.setStatusCode((int) headers.get(httpStatusCode));
|
if (headers.get(HTTP_STATUS_CODE) instanceof Integer) {
|
||||||
}
|
httpResponse.setStatusCode((int) headers.get(HTTP_STATUS_CODE));
|
||||||
else if (headers.containsKey(httpStatusCode) && (!(headers.get(httpStatusCode) instanceof Integer))) {
|
}
|
||||||
log.warn("The statusCode should be an Integer value");
|
else {
|
||||||
|
log.warn("The statusCode should be an Integer value");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,7 @@ package org.springframework.cloud.function.adapter.gcp;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -34,8 +32,11 @@ import com.google.cloud.functions.HttpResponse;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.system.CapturedOutput;
|
||||||
|
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||||
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
|
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -54,17 +55,20 @@ import static org.mockito.Mockito.when;
|
|||||||
* @author Dmitry Solomakha
|
* @author Dmitry Solomakha
|
||||||
* @author Mike Eltsufin
|
* @author Mike Eltsufin
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ExtendWith(OutputCaptureExtension.class)
|
||||||
public class FunctionInvokerHttpTests {
|
public class FunctionInvokerHttpTests {
|
||||||
|
|
||||||
private static final Gson gson = new Gson();
|
private static final Gson gson = new Gson();
|
||||||
private static HttpRequest request = Mockito.mock(HttpRequest.class);
|
private HttpRequest request;
|
||||||
private static HttpResponse response = Mockito.mock(HttpResponse.class);
|
private HttpResponse response;
|
||||||
private BufferedWriter bufferedWriter;
|
private BufferedWriter bufferedWriter;
|
||||||
private StringWriter writer;
|
private StringWriter writer;
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void testSetup() throws IOException {
|
void testSetup() throws IOException {
|
||||||
|
request = Mockito.mock(HttpRequest.class);
|
||||||
|
response = Mockito.mock(HttpResponse.class);
|
||||||
writer = new StringWriter();
|
writer = new StringWriter();
|
||||||
bufferedWriter = new BufferedWriter(writer);
|
bufferedWriter = new BufferedWriter(writer);
|
||||||
when(response.getWriter()).thenReturn(bufferedWriter);
|
when(response.getWriter()).thenReturn(bufferedWriter);
|
||||||
@@ -79,9 +83,9 @@ public class FunctionInvokerHttpTests {
|
|||||||
handler.service(request, response);
|
handler.service(request, response);
|
||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
if (expectedOutput != null) {
|
|
||||||
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,9 +101,9 @@ public class FunctionInvokerHttpTests {
|
|||||||
handler.service(request, response);
|
handler.service(request, response);
|
||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
if (expectedOutput != null) {
|
|
||||||
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,27 +119,24 @@ public class FunctionInvokerHttpTests {
|
|||||||
handler.service(request, response);
|
handler.service(request, response);
|
||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
if (expectedOutput != null) {
|
|
||||||
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
assertThat(writer.toString()).isEqualTo(gson.toJson(expectedOutput));
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJsonInputConsumer_Background() throws Exception {
|
public void testJsonInputConsumer_Background(CapturedOutput capturedOutput) throws Exception {
|
||||||
|
|
||||||
FunctionInvoker handler = new FunctionInvoker(JsonInputConsumer.class);
|
FunctionInvoker handler = new FunctionInvoker(JsonInputConsumer.class);
|
||||||
|
|
||||||
IncomingRequest input = new IncomingRequest("hello");
|
IncomingRequest input = new IncomingRequest("hello");
|
||||||
|
|
||||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
|
||||||
System.setOut(new PrintStream(outContent));
|
|
||||||
|
|
||||||
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(input))));
|
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(input))));
|
||||||
handler.service(request, response);
|
handler.service(request, response);
|
||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
assertThat(outContent.toString()).isEqualTo("Thank you for sending the message: hello");
|
assertThat(capturedOutput.toString()).contains("Thank you for sending the message: hello");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +242,7 @@ public class FunctionInvokerHttpTests {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Consumer<IncomingRequest> function() {
|
public Consumer<IncomingRequest> function() {
|
||||||
return (in) -> System.out.print("Thank you for sending the message: " + in.message);
|
return (in) -> System.out.println("Thank you for sending the message: " + in.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user