HttpUtil { public static Charset getCharset(HttpMessage message) { return getCharset(message, CharsetUtil.ISO_8859_1); } private  HttpUtil(); static boolean isOriginForm(URI uri); static boolean isAsteriskForm(URI uri); static boolean isKeepAlive(HttpMessage message); static void setKeepAlive(HttpMessage message, boolean keepAlive); static void setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive); static long getContentLength(HttpMessage message); static long getContentLength(HttpMessage message, long defaultValue); static int getContentLength(HttpMessage message, int defaultValue); static void setContentLength(HttpMessage message, long length); static boolean isContentLengthSet(HttpMessage m); static boolean is100ContinueExpected(HttpMessage message); static void set100ContinueExpected(HttpMessage message, boolean expected); static boolean isTransferEncodingChunked(HttpMessage message); static void setTransferEncodingChunked(HttpMessage m, boolean chunked); static Charset getCharset(HttpMessage message); static Charset getCharset(CharSequence contentTypeValue); static Charset getCharset(HttpMessage message, Charset defaultCharset); static Charset getCharset(CharSequence contentTypeValue, Charset defaultCharset); @Deprecated static CharSequence getCharsetAsString(HttpMessage message); static CharSequence getCharsetAsSequence(HttpMessage message); static CharSequence getCharsetAsSequence(CharSequence contentTypeValue); static CharSequence getMimeType(HttpMessage message); static CharSequence getMimeType(CharSequence contentTypeValue);  }
@Test public void testGetCharset() { String NORMAL_CONTENT_TYPE = "text/html; charset=utf-8"; String UPPER_CASE_NORMAL_CONTENT_TYPE = "TEXT/HTML; CHARSET=UTF-8"; HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); message.headers().set(HttpHeaderNames.CONTENT_TYPE, NORMAL_CONTENT_TYPE); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message)); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(NORMAL_CONTENT_TYPE)); message.headers().set(HttpHeaderNames.CONTENT_TYPE, UPPER_CASE_NORMAL_CONTENT_TYPE); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message)); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(UPPER_CASE_NORMAL_CONTENT_TYPE)); }
@Test public void testGetCharset_defaultValue() { final String SIMPLE_CONTENT_TYPE = "text/html"; final String CONTENT_TYPE_WITH_INCORRECT_CHARSET = "text/html; charset=UTFFF"; HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); message.headers().set(HttpHeaderNames.CONTENT_TYPE, SIMPLE_CONTENT_TYPE); assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message)); assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(SIMPLE_CONTENT_TYPE)); message.headers().set(HttpHeaderNames.CONTENT_TYPE, SIMPLE_CONTENT_TYPE); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8)); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(SIMPLE_CONTENT_TYPE, StandardCharsets.UTF_8)); message.headers().set(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE_WITH_INCORRECT_CHARSET); assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message)); assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(CONTENT_TYPE_WITH_INCORRECT_CHARSET)); message.headers().set(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE_WITH_INCORRECT_CHARSET); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8)); assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(CONTENT_TYPE_WITH_INCORRECT_CHARSET, StandardCharsets.UTF_8)); }