Search in sources :

Example 1 with AbstractHttpEntity

use of org.apache.hc.core5.http.io.entity.AbstractHttpEntity in project httpcomponents-core by apache.

the class TestEntityUtils method testContentWithInvalidContentTypeToString.

@Test
public void testContentWithInvalidContentTypeToString() throws Exception {
    final String content = constructString(RUSSIAN_HELLO);
    final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
    final HttpEntity entity = new AbstractHttpEntity("text/plain; charset=nosuchcharset", null) {

        @Override
        public InputStream getContent() throws IOException, UnsupportedOperationException {
            return new ByteArrayInputStream(bytes);
        }

        @Override
        public boolean isStreaming() {
            return false;
        }

        @Override
        public long getContentLength() {
            return bytes.length;
        }

        @Override
        public void close() throws IOException {
        }
    };
    final String s = EntityUtils.toString(entity, "UTF-8");
    Assertions.assertEquals(content, s);
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 HttpEntity (org.apache.hc.core5.http.HttpEntity)1 Test (org.junit.jupiter.api.Test)1