Browse Source

Avoid duplicate Content-Type in MockHttpServletRequest

Fixes gh-34913
pull/35405/head
Brian Clozel 7 months ago
parent
commit
fdab8fabd2
  1. 5
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 11
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
  3. 8
      spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
  4. 5
      spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -1064,8 +1064,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @see #getDateHeader * @see #getDateHeader
*/ */
public void addHeader(String name, Object value) { public void addHeader(String name, Object value) {
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
!this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
setContentType(value.toString()); setContentType(value.toString());
} }
else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) && else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -226,6 +226,15 @@ class MockHttpServletRequestTests {
assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8"); assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8");
} }
@Test
void contentTypeMultipleCalls() {
String contentType = "text/html";
request.addHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
assertThat(request.getContentType()).isEqualTo(contentType);
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType);
}
@Test // SPR-12677 @Test // SPR-12677
void setContentTypeHeaderWithMoreComplexCharsetSyntax() { void setContentTypeHeaderWithMoreComplexCharsetSyntax() {
String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -471,12 +471,12 @@ class MockHttpServletRequestBuilderTests {
assertThat(request.getContentType()).isEqualTo("yaml"); assertThat(request.getContentType()).isEqualTo("yaml");
} }
@Test // SPR-11308 @Test
void contentTypeViaMultipleHeaderValues() { void contentTypeViaMultipleHeaderValues() {
this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE); this.builder.header("Content-Type", MediaType.TEXT_HTML_VALUE, MediaType.TEXT_PLAIN_VALUE);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
assertThat(request.getContentType()).isEqualTo("text/html"); assertThat(request.getContentType()).isEqualTo("text/plain");
} }
@Test @Test

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -1063,8 +1063,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @see #getDateHeader * @see #getDateHeader
*/ */
public void addHeader(String name, Object value) { public void addHeader(String name, Object value) {
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
!this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
setContentType(value.toString()); setContentType(value.toString());
} }
else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) && else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&

Loading…
Cancel
Save