Browse Source

MockHttpServletRequest preserves original Accept-Language header value

Issue: SPR-17566
pull/2040/head
Juergen Hoeller 6 years ago
parent
commit
9efea7eb73
  1. 8
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 1
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
  3. 8
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

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

@ -1013,12 +1013,14 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -1013,12 +1013,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
try {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT_LANGUAGE, value.toString());
setPreferredLocales(headers.getAcceptLanguageAsLocales());
List<Locale> locales = headers.getAcceptLanguageAsLocales();
this.locales.clear();
this.locales.addAll(locales);
}
catch (IllegalArgumentException ex) {
// Invalid Accept-Language format -> store plain header instead
doAddHeaderValue(name, value, true);
// Invalid Accept-Language format -> just store plain header
}
doAddHeaderValue(name, value, true);
}
else {
doAddHeaderValue(name, value, false);

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

@ -341,6 +341,7 @@ public class MockHttpServletRequestTests { @@ -341,6 +341,7 @@ public class MockHttpServletRequestTests {
List<Locale> actual = Collections.list(request.getLocales());
assertEquals(Arrays.asList(Locale.forLanguageTag("fr-ch"), Locale.forLanguageTag("fr"),
Locale.forLanguageTag("en"), Locale.forLanguageTag("de")), actual);
assertEquals(headerValue, request.getHeader("Accept-Language"));
}
@Test

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

@ -1013,12 +1013,14 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -1013,12 +1013,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
try {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT_LANGUAGE, value.toString());
setPreferredLocales(headers.getAcceptLanguageAsLocales());
List<Locale> locales = headers.getAcceptLanguageAsLocales();
this.locales.clear();
this.locales.addAll(locales);
}
catch (IllegalArgumentException ex) {
// Invalid Accept-Language format -> store plain header instead
doAddHeaderValue(name, value, true);
// Invalid Accept-Language format -> just store plain header
}
doAddHeaderValue(name, value, true);
}
else {
doAddHeaderValue(name, value, false);

Loading…
Cancel
Save