Skip to content
This repository was archived by the owner on Oct 8, 2023. It is now read-only.

Commit 197595b

Browse files
committed
Allow returning ApiGatewayResponse directly
1 parent 25fdfbb commit 197595b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/main/java/com/github/kaklakariada/aws/lambda/RequestHandlingService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ private ApiGatewayRequest parseRequest(InputStream input) {
102102
private ApiGatewayResponse handleRequest(final ApiGatewayRequest request, Context context) {
103103
try {
104104
final Object result = handler.handleRequest(request, context);
105+
if (result instanceof ApiGatewayResponse) {
106+
LOG.trace("Result type is already ApiGatewayResponse: don't serialize body.");
107+
return (ApiGatewayResponse) result;
108+
}
105109
final String responseBody = serializeResult(result);
106110
return ApiGatewayResponse.ok(responseBody);
107111
} catch (final LambdaException e) {

src/test/java/com/github/kaklakariada/aws/lambda/RequestHandlingServiceTest.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package com.github.kaklakariada.aws.lambda;
1919

20+
import static java.util.Collections.emptyMap;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
2122
import static org.mockito.ArgumentMatchers.any;
2223
import static org.mockito.ArgumentMatchers.same;
@@ -41,17 +42,17 @@
4142
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
4243
import com.fasterxml.jackson.databind.node.ObjectNode;
4344
import com.github.kaklakariada.aws.lambda.listener.RequestProcessingListener;
44-
import com.github.kaklakariada.aws.lambda.model.request.ApiGatewayRequest;
45+
import com.github.kaklakariada.aws.lambda.model.response.ApiGatewayResponse;
4546

4647
public class RequestHandlingServiceTest {
4748

49+
private static final String RESPONSE_MESSAGE = "msg";
50+
4851
@Mock
4952
private ControllerAdapter controllerMock;
5053
@Mock
5154
private Context contextMock;
5255
@Mock
53-
private ApiGatewayRequest apiGatewayRequestMock;
54-
@Mock
5556
private TestResponse responseMock;
5657
@Mock
5758
private RequestProcessingListener listenerMock;
@@ -66,13 +67,20 @@ public void setup() {
6667
objectMapper = new ObjectMapper();
6768
jsonFactory = JsonNodeFactory.instance;
6869
service = new RequestHandlingService(objectMapper, controllerMock, listenerMock);
69-
when(controllerMock.handleRequest(same(apiGatewayRequestMock), same(contextMock)))
70-
.thenReturn(new TestResponse());
70+
when(controllerMock.handleRequest(any(), same(contextMock))).thenReturn(new TestResponse(RESPONSE_MESSAGE));
7171
}
7272

7373
@Test
7474
public void testRequest() {
75-
assertEquals(response(200, "null"), handleRequest(jsonFactory.objectNode()));
75+
assertEquals(response(200, "{\"message\":\"" + RESPONSE_MESSAGE + "\"}"),
76+
handleRequest(jsonFactory.objectNode()));
77+
}
78+
79+
@Test
80+
public void testRequestReturnApiGatewayResponseUnchanged() {
81+
final ApiGatewayResponse response = new ApiGatewayResponse(201, emptyMap(), "response2");
82+
when(controllerMock.handleRequest(any(), same(contextMock))).thenReturn(response);
83+
assertEquals(response(201, "response2"), handleRequest(jsonFactory.objectNode()));
7684
}
7785

7886
@Test
@@ -117,6 +125,15 @@ private String handleRequest(String request) {
117125
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
118126
}
119127

120-
private static class TestResponse {
128+
static class TestResponse {
129+
private final String message;
130+
131+
TestResponse(String message) {
132+
this.message = message;
133+
}
134+
135+
public String getMessage() {
136+
return message;
137+
}
121138
}
122139
}

0 commit comments

Comments
 (0)