diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index bb1b861fdd6..b8b117df539 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -1064,8 +1064,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * @see #getDateHeader */ public void addHeader(String name, Object value) { - if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && - !this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) { + if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { setContentType(value.toString()); } else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) && diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index 006c68745f8..6ab8ba41c80 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -226,6 +226,15 @@ class MockHttpServletRequestTests { 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 void setContentTypeHeaderWithMoreComplexCharsetSyntax() { String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 6b0dd46fcdb..5c09d17fa08 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -471,12 +471,12 @@ class MockHttpServletRequestBuilderTests { assertThat(request.getContentType()).isEqualTo("yaml"); } - @Test // SPR-11308 + @Test 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); - assertThat(request.getContentType()).isEqualTo("text/html"); + assertThat(request.getContentType()).isEqualTo("text/plain"); } @Test diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java index c909946b92a..5c202756064 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -1063,8 +1063,7 @@ public class MockHttpServletRequest implements HttpServletRequest { * @see #getDateHeader */ public void addHeader(String name, Object value) { - if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && - !this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) { + if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { setContentType(value.toString()); } else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&