Skip to content

Commit 9c0b28f

Browse files
committed
Fix StringIndexOutOfBoundsException
Closes gh-29588
1 parent 08e7f5a commit 9c0b28f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ public void setContentType(@Nullable String contentType) {
488488
}
489489
catch (IllegalArgumentException ex) {
490490
// Try to get charset value anyway
491-
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
491+
contentType = contentType.toLowerCase();
492+
int charsetIndex = contentType.indexOf(CHARSET_PREFIX);
492493
if (charsetIndex != -1) {
493494
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
494495
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ void setContentTypeUTF8() {
185185
assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8");
186186
}
187187

188+
@Test // gh-29255
189+
void setContentTypeInvalidWithNonAsciiCharacterAndCharset() {
190+
String contentType = "İcharset=";
191+
request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
192+
assertThat(request.getContentType()).isEqualTo(contentType);
193+
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType);
194+
assertThat(request.getCharacterEncoding()).isEqualTo("");
195+
}
196+
188197
@Test
189198
void contentTypeHeader() {
190199
String contentType = "test/plain";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ public void setContentType(@Nullable String contentType) {
488488
}
489489
catch (IllegalArgumentException ex) {
490490
// Try to get charset value anyway
491-
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
491+
contentType = contentType.toLowerCase();
492+
int charsetIndex = contentType.indexOf(CHARSET_PREFIX);
492493
if (charsetIndex != -1) {
493494
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
494495
}

0 commit comments

Comments
 (0)