From 7602a36fae19e2f33847e1d4a361a2e035dc5b8b Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Sat, 30 Jul 2022 12:12:16 +0530 Subject: [PATCH] JAVA-13614 Look into How to Read HTTP Headers in Spring REST Controllers article --- .../controller/ReadHeaderRestController.java | 7 +++++-- ...ReadHeaderRestControllerIntegrationTest.java | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) 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 {