@Test(expected = UnsupportedCharsetException.class) public void testExtractInvalidCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = stuff"); EntityUtils.getContentType(httpentity); }}
@Test public void testDefaultCharsetContentToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(Charset.forName("KOI8-R")); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain"); final String s = EntityUtils.toString(httpentity, "KOI8-R"); Assert.assertEquals(content, s); }}
@Test public void testContentWithContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain; charset=UTF-8"); final String s = EntityUtils.toString(httpentity, "ISO-8859-1"); Assert.assertEquals(content, s); }}
@Test public void testNoCharsetContentToString() throws Exception { final String content = constructString(SWISS_GERMAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain"); final String s = EntityUtils.toString(httpentity); Assert.assertEquals(content, s); }}
@Test public void testContentWithInvalidContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes("UTF-8"); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain; charset=nosuchcharset"); final String s = EntityUtils.toString(httpentity, "UTF-8"); Assert.assertEquals(content, s); }}
@Test public void testExtractNullContentType() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType(null); Assert.assertNull(EntityUtils.getContentType(httpentity)); }}
@Test public void testExtractLenientNullContentType() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType(null); Assert.assertNull(EntityUtils.getContentTypeLenient(httpentity)); }}
@Test public void testToString() throws Exception { final BasicHttpEntity httpentity = new BasicHttpEntity(); httpentity.setContentType("blah"); httpentity.setContentEncoding("yada"); httpentity.setContentLength(10); httpentity.setChunked(true); Assert.assertEquals("[Content-Type: blah,Content-Encoding: yada,Content-Length: 10,Chunked: true]", httpentity.toString()); }
@Test public void testLenientExtractInvalidCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = stuff"); final ContentType contentType = EntityUtils.getContentTypeLenient(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(null, contentType.getCharset()); }}
@Test public void testExtract() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = UTF-8"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); }}
@Test public void testExtractNoCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; param=yadayada"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertNull(contentType.getCharset()); }}
@Test public void testResponseContentEntityContentTypeAndEncoding() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentEncoding("whatever"); entity.setContentType("whatever"); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNotNull(h1); Assert.assertEquals("whatever", h1.getValue()); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING); Assert.assertNotNull(h2); Assert.assertEquals("whatever", h2.getValue()); }
@Test public void testRequestContentTypeAndEncoding() throws Exception { final HttpContext context = new BasicHttpContext(null); final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentType("whatever"); entity.setContentEncoding("whatever"); request.setEntity(entity); final RequestContent interceptor = new RequestContent(); interceptor.process(request, request.getEntity(), context); final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNotNull(h1); Assert.assertEquals("whatever", h1.getValue()); final Header h2 = request.getFirstHeader(HttpHeaders.CONTENT_ENCODING); Assert.assertNotNull(h2); Assert.assertEquals("whatever", h2.getValue()); }