Skip to content

Commit fdab8fa

Browse files
committed
Avoid duplicate Content-Type in MockHttpServletRequest
Fixes gh-34913
1 parent ae8b45a commit fdab8fa

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1064,8 +1064,7 @@ public Cookie[] getCookies() {
10641064
* @see #getDateHeader
10651065
*/
10661066
public void addHeader(String name, Object value) {
1067-
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) &&
1068-
!this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
1067+
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
10691068
setContentType(value.toString());
10701069
}
10711070
else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -226,6 +226,15 @@ void contentTypeHeaderUTF8() {
226226
assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8");
227227
}
228228

229+
@Test
230+
void contentTypeMultipleCalls() {
231+
String contentType = "text/html";
232+
request.addHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
233+
request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
234+
assertThat(request.getContentType()).isEqualTo(contentType);
235+
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType);
236+
}
237+
229238
@Test // SPR-12677
230239
void setContentTypeHeaderWithMoreComplexCharsetSyntax() {
231240
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -471,12 +471,12 @@ void contentTypeViaHeaderWithInvalidValue() {
471471
assertThat(request.getContentType()).isEqualTo("yaml");
472472
}
473473

474-
@Test // SPR-11308
474+
@Test
475475
void contentTypeViaMultipleHeaderValues() {
476-
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE);
476+
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.TEXT_PLAIN_VALUE);
477477
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
478478

479-
assertThat(request.getContentType()).isEqualTo("text/html");
479+
assertThat(request.getContentType()).isEqualTo("text/plain");
480480
}
481481

482482
@Test

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1063,8 +1063,7 @@ public Cookie[] getCookies() {
10631063
* @see #getDateHeader
10641064
*/
10651065
public void addHeader(String name, Object value) {
1066-
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) &&
1067-
!this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
1066+
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
10681067
setContentType(value.toString());
10691068
}
10701069
else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&

0 commit comments

Comments
 (0)