Skip to content

Commit

Permalink
Port "Avoid async handling in WebMvc if not necessary to wait" spring…
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed May 25, 2024
1 parent 978688c commit 30e26a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;


import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -65,7 +66,7 @@ public MultipartGraphQlHttpHandler(WebGraphQlHandler graphQlHandler, GenericHttp
this.genericHttpMessageConverter = genericHttpMessageConverter;
}

public ServerResponse handleMultipartRequest(ServerRequest serverRequest) {
public ServerResponse handleMultipartRequest(ServerRequest serverRequest) throws Exception {
HttpServletRequest httpServletRequest = serverRequest.servletRequest();

Map<String, Object> inputQuery = Optional.ofNullable(this.<Map<String, Object>>deserializePart(
Expand Down Expand Up @@ -124,7 +125,7 @@ public ServerResponse handleMultipartRequest(ServerRequest serverRequest) {
logger.debug("Executing: " + graphQlRequest);
}

Mono<ServerResponse> responseMono = this.graphQlHandler.handleRequest(graphQlRequest)
var future = this.graphQlHandler.handleRequest(graphQlRequest)
.map(response -> {
if (logger.isDebugEnabled()) {
logger.debug("Execution complete");
Expand All @@ -133,9 +134,21 @@ public ServerResponse handleMultipartRequest(ServerRequest serverRequest) {
builder.headers(headers -> headers.putAll(response.getResponseHeaders()));
builder.contentType(selectResponseMediaType(serverRequest));
return builder.body(response.toMap());
});
}).toFuture();

return ServerResponse.async(responseMono);
if (future.isDone()) {
try {
return future.get();
}
catch (ExecutionException ex) {
throw new ServletException(ex.getCause());
}
catch (InterruptedException ex) {
throw new ServletException(ex);
}
}

return ServerResponse.async(future);
}

private static class JsonMultipartInputMessage implements HttpInputMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.mock.web.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.function.AsyncServerResponse;
import org.springframework.web.servlet.function.EntityResponse;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

Expand Down Expand Up @@ -139,10 +140,10 @@ private byte[] getJsonArray(Object o) {
}

private MockHttpServletResponse handleMultipartRequest(
MockHttpServletRequest servletRequest, MultipartGraphQlHttpHandler handler) throws ServletException, IOException {
MockHttpServletRequest servletRequest, MultipartGraphQlHttpHandler handler) throws Exception {

ServerRequest request = ServerRequest.create(servletRequest, MESSAGE_READERS);
ServerResponse response = ((AsyncServerResponse) handler.handleMultipartRequest(request)).block();
ServerResponse response = handler.handleMultipartRequest(request);

MockHttpServletResponse servletResponse = new MockHttpServletResponse();
response.writeTo(servletRequest, servletResponse, new DefaultContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -115,7 +114,7 @@ public void testWithMockMvc() throws Exception {



final var asyncMvcResult = mockMvc.perform(
mockMvc.perform(
MockMvcRequestBuilders
.multipart("/graphql")
.file(filePart1)
Expand All @@ -125,9 +124,7 @@ public void testWithMockMvc() throws Exception {
.accept(MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE)
.with(csrf())
.with(SecurityMockMvcRequestPostProcessors.httpBasic("user", "password"))
).andReturn();

mockMvc.perform(asyncDispatch(asyncMvcResult))
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.multiFileUpload").isNotEmpty())
Expand Down

0 comments on commit 30e26a8

Please sign in to comment.