Skip to content

Commit

Permalink
use streaming handler from field
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Aug 23, 2022
1 parent 1b70bef commit 4a3e82c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ String executeHandler(ApiGatewayProxyRequest request, Object unitTest) throws Cl
Class<?> clazz = Class.forName(className);

if (RequestStreamHandler.class.isAssignableFrom(clazz)) {
return handleRequestStreamHandler(request, (Class<? extends RequestStreamHandler>) clazz);
return handleRequestStreamHandler(request, unitTest, (Class<? extends RequestStreamHandler>) clazz);
}

Method method = Arrays.stream(clazz.getMethods())
Expand Down Expand Up @@ -120,8 +120,8 @@ String executeHandler(ApiGatewayProxyRequest request, Object unitTest) throws Cl
return outputStream.toString();
}

private String handleRequestStreamHandler(ApiGatewayProxyRequest request, Class<? extends RequestStreamHandler> handlerClass) throws IllegalAccessException, InstantiationException, IOException {
RequestStreamHandler streamHandler = handlerClass.newInstance();
private String handleRequestStreamHandler(ApiGatewayProxyRequest request, Object unitTest, Class<? extends RequestStreamHandler> handlerClass) throws IllegalAccessException, InstantiationException, IOException {
RequestStreamHandler streamHandler = getOrCreateHandler(unitTest, handlerClass);

ByteArrayInputStream bais = new ByteArrayInputStream(prepareRequestObject(request).getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ApiGatewayConfigurationSpec extends Specification {
@SuppressWarnings('UnusedPrivateField')
private final NonProxyHandler nonProxyHandler = new NonProxyHandler()

private final AnotherStreamingHandler anotherStreamingHandler = new AnotherStreamingHandler('ahoy')

void 'sample handler'() {
given:
// tag::simple[]
Expand Down Expand Up @@ -209,6 +211,22 @@ class ApiGatewayConfigurationSpec extends Specification {
gru.verify()
}

void 'proxy to streaming field'() {
given:
Gru gru = Gru.create(ApiGatewayProxy.create {
map '/seven' to AnotherStreamingHandler
})
when:
gru.reset().test {
get '/seven'
expect {
json 'seventhResponse.json'
}
}
then:
gru.verify()
}

}

class SampleHandler implements RequestHandler<Map, Map> {
Expand Down Expand Up @@ -257,3 +275,22 @@ class StreamingHandler implements RequestStreamHandler {
mapper.writer().writeValue(output, [body: [input: mapper.readValue(input, Map)]])
}
}

class AnotherStreamingHandler implements RequestStreamHandler {

private final String message;

AnotherStreamingHandler() {
this('hello')
}

AnotherStreamingHandler(String message) {
this.message = message
}

@Override
void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
ObjectMapper mapper = new ObjectMapper()
mapper.writer().writeValue(output, [body: [message: message, input: mapper.readValue(input, Map)]])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"message": "ahoy",
"input": {
"path": "/seven",
"httpMethod": "GET"
}
}

0 comments on commit 4a3e82c

Please sign in to comment.