Skip to content

Commit 232f2b4

Browse files
committed
changed test to cover full response control
1 parent ecff967 commit 232f2b4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/test/groovy/graphql/servlet/AbstractGraphQLHttpServletSpec.groovy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class AbstractGraphQLHttpServletSpec extends Specification {
285285
getBatchedResponseContent()[1].data.echo == "test"
286286
}
287287

288-
def "Execution Result Handler allows limiting number of queries"() {
288+
def "Batch Execution Handler allows limiting batches and sending error messages."() {
289289
setup:
290290
servlet = TestUtils.createBatchCustomizedServlet({ env -> env.arguments.arg }, { env -> env.arguments.arg }, { env ->
291291
AtomicReference<SingleSubscriberPublisher<String>> publisherRef = new AtomicReference<>()
@@ -304,9 +304,8 @@ class AbstractGraphQLHttpServletSpec extends Specification {
304304
servlet.doGet(request, response)
305305

306306
then:
307-
response.getStatus() == STATUS_OK
308-
response.getContentType() == CONTENT_TYPE_JSON_UTF8
309-
getBatchedResponseContent().size() == 2
307+
response.getStatus() == STATUS_BAD_REQUEST
308+
response.getErrorMessage() == TestBatchExecutionHandler.BATCH_ERROR_MESSAGE
310309
}
311310

312311
def "Default Execution Result Handler does not limit number of queries"() {

src/test/groovy/graphql/servlet/TestBatchExecutionHandler.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,29 @@
1313

1414
public class TestBatchExecutionHandler implements BatchExecutionHandler {
1515

16+
public static String BATCH_ERROR_MESSAGE = "Batch limit exceeded";
17+
1618
@Override
1719
public void handleBatch(GraphQLBatchedInvocationInput batchedInvocationInput, HttpServletResponse response, GraphQLObjectMapper graphQLObjectMapper,
1820
BiFunction<GraphQLInvocationInput, ExecutionInput, ExecutionResult> queryFunction) {
19-
List<ExecutionResult> results = batchedInvocationInput.getExecutionInputs().parallelStream()
20-
.limit(2)
21+
List<ExecutionInput> inputs = batchedInvocationInput.getExecutionInputs();
22+
if (inputs.size() > 2) {
23+
handleBadInput(response);
24+
}
25+
List<ExecutionResult> results = inputs.parallelStream()
2126
.map(input -> queryFunction.apply(batchedInvocationInput, input))
2227
.collect(Collectors.toList());
2328
writeResults(results, response, graphQLObjectMapper);
2429
}
2530

31+
private void handleBadInput(HttpServletResponse response) {
32+
try {
33+
response.sendError(HttpServletResponse.SC_BAD_REQUEST, BATCH_ERROR_MESSAGE);
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
2639
private void writeResults(List<ExecutionResult> results, HttpServletResponse response, GraphQLObjectMapper mapper) {
2740
response.setContentType(AbstractGraphQLHttpServlet.APPLICATION_JSON_UTF8);
2841
response.setStatus(AbstractGraphQLHttpServlet.STATUS_OK);

0 commit comments

Comments
 (0)