diff --git a/spring-web-modules/spring-mvc-basics-3/src/main/java/com/baeldung/spring/headers/controller/ReadHeaderRestController.java b/spring-web-modules/spring-mvc-basics-3/src/main/java/com/baeldung/spring/headers/controller/ReadHeaderRestController.java index 6a0f3b6a0d..ff4d0eea58 100644 --- a/spring-web-modules/spring-mvc-basics-3/src/main/java/com/baeldung/spring/headers/controller/ReadHeaderRestController.java +++ b/spring-web-modules/spring-mvc-basics-3/src/main/java/com/baeldung/spring/headers/controller/ReadHeaderRestController.java @@ -1,6 +1,8 @@ package com.baeldung.spring.headers.controller; import java.net.InetSocketAddress; +import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; @@ -24,9 +26,10 @@ public class ReadHeaderRestController { } @GetMapping("/greeting") - public ResponseEntity greeting(@RequestHeader(value = "accept-language") String language) { + public ResponseEntity greeting(@RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE) String language) { String greeting = ""; - String firstLanguage = (language.length() > 1 ? language.substring(0, 2) : language); + List ranges = Locale.LanguageRange.parse(language); + String firstLanguage = ranges.get(0).getRange(); switch (firstLanguage) { case "es": greeting = "Hola!"; diff --git a/spring-web-modules/spring-mvc-basics-3/src/test/java/com/baeldung/headers/controller/ReadHeaderRestControllerIntegrationTest.java b/spring-web-modules/spring-mvc-basics-3/src/test/java/com/baeldung/headers/controller/ReadHeaderRestControllerIntegrationTest.java index 6f94004cc7..1641939b67 100644 --- a/spring-web-modules/spring-mvc-basics-3/src/test/java/com/baeldung/headers/controller/ReadHeaderRestControllerIntegrationTest.java +++ b/spring-web-modules/spring-mvc-basics-3/src/test/java/com/baeldung/headers/controller/ReadHeaderRestControllerIntegrationTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; @@ -51,10 +52,24 @@ public class ReadHeaderRestControllerIntegrationTest { @Test public void whenGetRequestSentToGreeting_thenStatusOKAndGreetingReturned() throws Exception { - mockMvc.perform(get("/greeting").header("accept-language", "de")) + mockMvc.perform(get("/greeting").header(HttpHeaders.ACCEPT_LANGUAGE, "de")) .andExpect(status().isOk()) .andExpect(content().string("Hallo!")); } + + @Test + public void whenPrioritizedListGetRequestSentToGreeting_thenStatusOKAndGreetingReturned() throws Exception { + mockMvc.perform(get("/greeting").header(HttpHeaders.ACCEPT_LANGUAGE, "fr,en,de")) + .andExpect(status().isOk()) + .andExpect(content().string("Bonjour!")); + } + + @Test + public void whenWeightedListGetRequestSentToGreeting_thenStatusOKAndGreetingReturned() throws Exception { + mockMvc.perform(get("/greeting").header(HttpHeaders.ACCEPT_LANGUAGE, "Accept-Language: es; q=1.0, de; q=0.5")) + .andExpect(status().isOk()) + .andExpect(content().string("Hola!")); + } @Test public void whenGetRequestSentToDouble_thenStatusOKAndCorrectResultReturned() throws Exception {