Skip to content

Commit 6590ab6

Browse files
committed
Consistently set content type of the request with multipart
Closes gh-33232
1 parent 265299c commit 6590ab6

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/AbstractMockMultipartHttpServletRequestBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public abstract class AbstractMockMultipartHttpServletRequestBuilder<B extends A
5858

5959
protected AbstractMockMultipartHttpServletRequestBuilder(HttpMethod httpMethod) {
6060
super(httpMethod);
61+
super.contentType(MediaType.MULTIPART_FORM_DATA);
6162
}
6263

6364
/**

spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ class MultipartTests {
232232
"name": "test"
233233
}""".getBytes(StandardCharsets.UTF_8));
234234

235+
@Test
236+
void multipartSetsContentType() {
237+
assertThat(mvc.put().uri("/multipart-put").multipart().file(file).file(JSON_PART_FILE))
238+
.request().satisfies(request -> assertThat(request.getContentType())
239+
.isEqualTo(MediaType.MULTIPART_FORM_DATA_VALUE));
240+
}
241+
235242
@Test
236243
void multipartWithPut() {
237244
assertThat(mvc.put().uri("/multipart-put").multipart().file(file).file(JSON_PART_FILE))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet.request;
18+
19+
import jakarta.servlet.ServletContext;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.http.HttpMethod;
23+
import org.springframework.http.MediaType;
24+
import org.springframework.mock.web.MockHttpServletRequest;
25+
import org.springframework.mock.web.MockServletContext;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link AbstractMockMultipartHttpServletRequestBuilder}.
31+
*
32+
* @author Stephane Nicoll
33+
*/
34+
public class AbstractMockMultipartHttpServletRequestBuilderTests {
35+
36+
private final ServletContext servletContext = new MockServletContext();
37+
38+
39+
@Test
40+
void builderSetsRequestContentType() {
41+
MockHttpServletRequest request = buildRequest(new TestRequestBuilder(HttpMethod.PUT).uri("/upload"));
42+
assertThat(request.getContentType()).isEqualTo(MediaType.MULTIPART_FORM_DATA_VALUE);
43+
}
44+
45+
46+
private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) {
47+
return builder.buildRequest(this.servletContext);
48+
}
49+
50+
51+
private static class TestRequestBuilder extends AbstractMockMultipartHttpServletRequestBuilder<TestRequestBuilder> {
52+
53+
TestRequestBuilder(HttpMethod httpMethod) {
54+
super(httpMethod);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)