WSController implements IWSController { Locale getLocale(HandshakeRequest request) { if(null != request) { Map<String, List<String>> headers = request.getHeaders(); if(null != headers) { List<String> accepts = headers.get(HttpHeaders.ACCEPT_LANGUAGE); logger.debug("Get accept-language from client headers : {}", accepts); if (null != accepts) { for (String accept : accepts) { try { return localeExtractor.extractFromAccept(accept); } catch (LocaleNotFoundException ex) { } } } } } return Locale.US; }  @Override void handleOpenConnexion(Session session, EndpointConfig config); @Override void onError(Session session, Throwable t); @Override void handleClosedConnexion(Session session, CloseReason closeReason); @Override void receiveCommandMessage(Session client, String json);  }
@Test public void testGetLocaleNotFound() throws LocaleNotFoundException { Locale result = instance.getLocale(null); assertThat(result).isEqualTo(Locale.US); HandshakeRequest request = mock(HandshakeRequest.class); when(request.getHeaders()).thenReturn(null); result = instance.getLocale(request); assertThat(result).isEqualTo(Locale.US); Map<String, List<String>> headers = new HashMap<>(); when(request.getHeaders()).thenReturn(headers); assertThat(result).isEqualTo(Locale.US); }
@Test public void testGetLocale() throws LocaleNotFoundException { System.out.println("getLocale"); HandshakeRequest request = mock(HandshakeRequest.class); Map<String, List<String>> headers = new HashMap<>(); when(request.getHeaders()).thenReturn(headers); when(localeExtractor.extractFromAccept(any(String.class))).thenThrow(LocaleNotFoundException.class).thenReturn(Locale.FRANCE).thenReturn(Locale.CHINA); headers.put(HttpHeaders.ACCEPT_LANGUAGE, Arrays.asList(null, "acceptFrance", "acceptChina")); Locale result = instance.getLocale(request); assertThat(result).isEqualTo(Locale.FRANCE); }