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

MockMvcWebTestClient forces HTTP POST for multipart requests #28545

Closed
kewne opened this issue May 31, 2022 · 2 comments
Closed

MockMvcWebTestClient forces HTTP POST for multipart requests #28545

kewne opened this issue May 31, 2022 · 2 comments
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@kewne
Copy link

kewne commented May 31, 2022

Affects: latest (6.0.0, 5.3.20)

When making a multipart/form-data request using WebTestClient, the HTTP method of the request is overwritten to POST:

For example, the following test:

        @Test
        void returns_no_content_on_success() {
            var builder = new MultipartBodyBuilder();
            builder.part("file", TestResources.JPEG_IMAGE, MediaType.IMAGE_JPEG);
            webTestClient.put()
                .uri("/test")
                .bodyValue(builder.build())
                .exchange()
                .expectStatus().isNoContent();
        }

generates the following log output:

> PUT /test
> WebTestClient-Request-Id: [1]
> Content-Type: [multipart/form-data;boundary=1TV9nXkH1G9MW2IM7_NyhWx2AJYVwdNGd]

159180 bytes of content.

< 405 METHOD_NOT_ALLOWED Method Not Allowed
< Allow: [PUT,DELETE]
< Content-Type: [application/problem+json]

156 bytes of content.

======================  MockMvc (Server) ===============================

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /test
       Parameters = {}
          Headers = [Content-Type:"multipart/form-data;charset=UTF-8", WebTestClient-Request-Id:"1"]
             Body = null

I've traced this to a some code that implies that Multipart requests are always POST:

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 31, 2022
@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) labels May 31, 2022
@rstoyanchev rstoyanchev self-assigned this Jun 1, 2022
@rstoyanchev rstoyanchev added this to the 5.3.21 milestone Jun 1, 2022
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 1, 2022
@rstoyanchev rstoyanchev changed the title WebTestClient multipart request forces POST method MockMvcWebTestClient forces HTTP POST for multipart requests Jun 1, 2022
@rstoyanchev
Copy link
Contributor

Indeed, it looks like MockMvc doesn't have methods in MockMvcRequestBuilders to create a multipart request with an HTTP method other than POST, so hardcoded.

@kewne
Copy link
Author

kewne commented Jun 8, 2022

Apologies for the potential spam but really wanted to leave my thanks for taking care of this, keep up the good work! 👍

izeye added a commit to izeye/spring-framework that referenced this issue Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants