Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipartGraphQlHttpHandler.readMultipartFiles throws java.lang.IllegalArgumentException #2

Closed
assens opened this issue Feb 21, 2024 · 0 comments · Fixed by #3
Closed

Comments

@assens
Copy link
Contributor

assens commented Feb 21, 2024

When trying to test the file upload form spring boot test, with mock mvc web environment, and having spring security in place, the MultipartGraphQlHttpHandler's method readMultipartFiles throws illegal argument exception.

The assertion is does not allow for wrapped HttpServletRequests to pass.

Assert.isInstanceOf(MultipartHttpServletRequest.class, httpServletRequest,
            "Request should be of type MultipartHttpServletRequest");

When having spring security in the filter chain, the httpServletRequest is of instance SecurityContextHolderAwareRequestWrapper. Instead of using this strict assertion, one could use

WebUtils.getNativeRequest(httpServletRequest, MultipartHttpServletRequest.class);

to obtain the instance of the wrapped MultipartHttpServletRequest.

I'll provide a pull request with a fix. This is just to keep track on the issue.
Below is my unit test, that can be used to reproduce the exception:

 @Test
  void testWithMockMvc() throws Exception {

    // arrange
    final var fileName = "fileName.xlsx";
    final var fileByteArray = new byte[100];

    final var operations = """
        { "query" : "mutation FileUpload($file: Upload!) { uploadFile(file: $file) { fileName size } }" }
        """;

    final var variables = """
        { "file" : ["variables.file"] }
        """;

    final var filePart = new MockMultipartFile("file", fileName, "application/octet-stream", fileByteArray);
    final var operationsPart = new MockPart("operations", operations.getBytes());
    final var variablesPart = new MockPart("map", variables.getBytes());

    // act & assert
    final var asyncMvcResult = mockMvc.perform(MockMvcRequestBuilders
        .multipart("/graphql")
        .file(filePart)
        .part(operationsPart)
        .part(variablesPart)
        .accept(MediaType.APPLICATION_GRAPHQL_RESPONSE_VALUE)
        .with(SecurityMockMvcRequestPostProcessors.jwt().authorities(new SimpleGrantedAuthority("ADMIN"))))
        .andReturn();

    mockMvc.perform(asyncDispatch(asyncMvcResult))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.data.uploadFile.fileName").value(fileName));

  }
assens added a commit to assens/multipart-spring-graphql that referenced this issue Feb 21, 2024
@assens assens mentioned this issue Feb 21, 2024
@nkonev nkonev closed this as completed in #3 Feb 22, 2024
nkonev added a commit that referenced this issue Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant