Skip to content

Commit 4e10735

Browse files
committed
MockHttpServletRequest restores default locale for empty accept header
Closes gh-22877
1 parent 7aa61d9 commit 4e10735

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -199,7 +199,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
199199
private String remoteHost = DEFAULT_REMOTE_HOST;
200200

201201
/** List of locales in descending order. */
202-
private final List<Locale> locales = new LinkedList<>();
202+
private final LinkedList<Locale> locales = new LinkedList<>();
203203

204204
private boolean secure = false;
205205

@@ -403,12 +403,11 @@ public void setCharacterEncoding(@Nullable String characterEncoding) {
403403

404404
private void updateContentTypeHeader() {
405405
if (StringUtils.hasLength(this.contentType)) {
406-
StringBuilder sb = new StringBuilder(this.contentType);
407-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
408-
StringUtils.hasLength(this.characterEncoding)) {
409-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
406+
String value = this.contentType;
407+
if (StringUtils.hasLength(this.characterEncoding) && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
408+
value += ';' + CHARSET_PREFIX + this.characterEncoding;
410409
}
411-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
410+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
412411
}
413412
}
414413

@@ -780,7 +779,7 @@ public void clearAttributes() {
780779
*/
781780
public void addPreferredLocale(Locale locale) {
782781
Assert.notNull(locale, "Locale must not be null");
783-
this.locales.add(0, locale);
782+
this.locales.addFirst(locale);
784783
updateAcceptLanguageHeader();
785784
}
786785

@@ -818,7 +817,7 @@ private void updateAcceptLanguageHeader() {
818817
*/
819818
@Override
820819
public Locale getLocale() {
821-
return this.locales.get(0);
820+
return this.locales.getFirst();
822821
}
823822

824823
/**
@@ -1016,6 +1015,9 @@ else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&
10161015
List<Locale> locales = headers.getAcceptLanguageAsLocales();
10171016
this.locales.clear();
10181017
this.locales.addAll(locales);
1018+
if (this.locales.isEmpty()) {
1019+
this.locales.add(Locale.ENGLISH);
1020+
}
10191021
}
10201022
catch (IllegalArgumentException ex) {
10211023
// Invalid Accept-Language format -> just store plain header

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -351,6 +351,13 @@ public void invalidAcceptLanguageHeader() {
351351
assertEquals("en_US", request.getHeader("Accept-Language"));
352352
}
353353

354+
@Test
355+
public void emptyAcceptLanguageHeader() {
356+
request.addHeader("Accept-Language", "");
357+
assertEquals(Locale.ENGLISH, request.getLocale());
358+
assertEquals("", request.getHeader("Accept-Language"));
359+
}
360+
354361
@Test
355362
public void getServerNameWithDefaultName() {
356363
assertEquals("localhost", request.getServerName());

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -199,7 +199,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
199199
private String remoteHost = DEFAULT_REMOTE_HOST;
200200

201201
/** List of locales in descending order. */
202-
private final List<Locale> locales = new LinkedList<>();
202+
private final LinkedList<Locale> locales = new LinkedList<>();
203203

204204
private boolean secure = false;
205205

@@ -403,12 +403,11 @@ public void setCharacterEncoding(@Nullable String characterEncoding) {
403403

404404
private void updateContentTypeHeader() {
405405
if (StringUtils.hasLength(this.contentType)) {
406-
StringBuilder sb = new StringBuilder(this.contentType);
407-
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) &&
408-
StringUtils.hasLength(this.characterEncoding)) {
409-
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
406+
String value = this.contentType;
407+
if (StringUtils.hasLength(this.characterEncoding) && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
408+
value += ';' + CHARSET_PREFIX + this.characterEncoding;
410409
}
411-
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
410+
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
412411
}
413412
}
414413

@@ -780,7 +779,7 @@ public void clearAttributes() {
780779
*/
781780
public void addPreferredLocale(Locale locale) {
782781
Assert.notNull(locale, "Locale must not be null");
783-
this.locales.add(0, locale);
782+
this.locales.addFirst(locale);
784783
updateAcceptLanguageHeader();
785784
}
786785

@@ -818,7 +817,7 @@ private void updateAcceptLanguageHeader() {
818817
*/
819818
@Override
820819
public Locale getLocale() {
821-
return this.locales.get(0);
820+
return this.locales.getFirst();
822821
}
823822

824823
/**
@@ -1016,6 +1015,9 @@ else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name) &&
10161015
List<Locale> locales = headers.getAcceptLanguageAsLocales();
10171016
this.locales.clear();
10181017
this.locales.addAll(locales);
1018+
if (this.locales.isEmpty()) {
1019+
this.locales.add(Locale.ENGLISH);
1020+
}
10191021
}
10201022
catch (IllegalArgumentException ex) {
10211023
// Invalid Accept-Language format -> just store plain header

0 commit comments

Comments
 (0)