From 4e10735e8f295db39301b3dd6f63dfb6e30b5342 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 3 May 2019 14:08:12 +0200 Subject: [PATCH] MockHttpServletRequest restores default locale for empty accept header Closes gh-22877 --- .../mock/web/MockHttpServletRequest.java | 20 ++++++++++--------- .../mock/web/MockHttpServletRequestTests.java | 9 ++++++++- .../mock/web/test/MockHttpServletRequest.java | 20 ++++++++++--------- 3 files changed, 30 insertions(+), 19 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 683f36e6c28..94e3a8f6cf2 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-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -199,7 +199,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private String remoteHost = DEFAULT_REMOTE_HOST; /** List of locales in descending order. */ - private final List locales = new LinkedList<>(); + private final LinkedList locales = new LinkedList<>(); private boolean secure = false; @@ -403,12 +403,11 @@ public class MockHttpServletRequest implements HttpServletRequest { private void updateContentTypeHeader() { if (StringUtils.hasLength(this.contentType)) { - StringBuilder sb = new StringBuilder(this.contentType); - if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && - StringUtils.hasLength(this.characterEncoding)) { - sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + String value = this.contentType; + if (StringUtils.hasLength(this.characterEncoding) && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + value += ';' + CHARSET_PREFIX + this.characterEncoding; } - doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true); + doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true); } } @@ -780,7 +779,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); - this.locales.add(0, locale); + this.locales.addFirst(locale); updateAcceptLanguageHeader(); } @@ -818,7 +817,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ @Override public Locale getLocale() { - return this.locales.get(0); + return this.locales.getFirst(); } /** @@ -1016,6 +1015,9 @@ public class MockHttpServletRequest implements HttpServletRequest { List locales = headers.getAcceptLanguageAsLocales(); this.locales.clear(); this.locales.addAll(locales); + if (this.locales.isEmpty()) { + this.locales.add(Locale.ENGLISH); + } } catch (IllegalArgumentException ex) { // Invalid Accept-Language format -> just store plain header 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 1857c7a9bdf..c28b95b3786 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-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -351,6 +351,13 @@ public class MockHttpServletRequestTests { assertEquals("en_US", request.getHeader("Accept-Language")); } + @Test + public void emptyAcceptLanguageHeader() { + request.addHeader("Accept-Language", ""); + assertEquals(Locale.ENGLISH, request.getLocale()); + assertEquals("", request.getHeader("Accept-Language")); + } + @Test public void getServerNameWithDefaultName() { assertEquals("localhost", request.getServerName()); 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 395122c883d..fbfa8721f0f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -199,7 +199,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private String remoteHost = DEFAULT_REMOTE_HOST; /** List of locales in descending order. */ - private final List locales = new LinkedList<>(); + private final LinkedList locales = new LinkedList<>(); private boolean secure = false; @@ -403,12 +403,11 @@ public class MockHttpServletRequest implements HttpServletRequest { private void updateContentTypeHeader() { if (StringUtils.hasLength(this.contentType)) { - StringBuilder sb = new StringBuilder(this.contentType); - if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && - StringUtils.hasLength(this.characterEncoding)) { - sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + String value = this.contentType; + if (StringUtils.hasLength(this.characterEncoding) && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + value += ';' + CHARSET_PREFIX + this.characterEncoding; } - doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true); + doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true); } } @@ -780,7 +779,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ public void addPreferredLocale(Locale locale) { Assert.notNull(locale, "Locale must not be null"); - this.locales.add(0, locale); + this.locales.addFirst(locale); updateAcceptLanguageHeader(); } @@ -818,7 +817,7 @@ public class MockHttpServletRequest implements HttpServletRequest { */ @Override public Locale getLocale() { - return this.locales.get(0); + return this.locales.getFirst(); } /** @@ -1016,6 +1015,9 @@ public class MockHttpServletRequest implements HttpServletRequest { List locales = headers.getAcceptLanguageAsLocales(); this.locales.clear(); this.locales.addAll(locales); + if (this.locales.isEmpty()) { + this.locales.add(Locale.ENGLISH); + } } catch (IllegalArgumentException ex) { // Invalid Accept-Language format -> just store plain header