@Test public void testParseUTF8Entity() throws Exception { final String ru_hello = constructString(RUSSIAN_HELLO); final String ch_hello = constructString(SWISS_GERMAN_HELLO); final List <NameValuePair> parameters = new ArrayList<>(); parameters.add(new BasicNameValuePair("russian", ru_hello)); parameters.add(new BasicNameValuePair("swiss", ch_hello)); final String s = URLEncodedUtils.format(parameters, StandardCharsets.UTF_8); Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" + "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s); try (final StringEntity entity = new StringEntity(s, ContentType.create(URLEncodedUtils.CONTENT_TYPE, StandardCharsets.UTF_8))) { final List<NameValuePair> result = EntityUtils.parse(entity); Assert.assertEquals(2, result.size()); assertNameValuePair(result.get(0), "russian", ru_hello); assertNameValuePair(result.get(1), "swiss", ch_hello); } }
@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 testParseEntity() throws Exception { try (final StringEntity entity = new StringEntity("Name1=Value1")) { entity.setContentType(URLEncodedUtils.CONTENT_TYPE); final List<NameValuePair> result = EntityUtils.parse(entity); Assert.assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name1", "Value1"); entity.setContentType("text/test"); Assert.assertTrue(EntityUtils.parse(entity).isEmpty()); }}
@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); }}