From 9efea7eb73d1437f5556251c43417f50037c7c81 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 4 Dec 2018 22:07:43 +0100 Subject: [PATCH] MockHttpServletRequest preserves original Accept-Language header value Issue: SPR-17566 --- .../springframework/mock/web/MockHttpServletRequest.java | 8 +++++--- .../mock/web/MockHttpServletRequestTests.java | 1 + .../mock/web/test/MockHttpServletRequest.java | 8 +++++--- 3 files changed, 11 insertions(+), 6 deletions(-) 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 46e1ebf440..69f41ea1b7 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 @@ -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 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); 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 d9fc351a6b..44386d4ef0 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 @@ -341,6 +341,7 @@ public class MockHttpServletRequestTests { List 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 diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 3b64054a1a..566560a2fa 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -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 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);