1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34 import java.util.zip.CRC32;
35 import java.util.zip.CheckedInputStream;
36 import java.util.zip.Checksum;
37
38 import org.apache.hc.core5.http.ContentType;
39 import org.apache.hc.core5.http.HttpEntity;
40 import org.apache.hc.core5.http.io.entity.EntityUtils;
41 import org.apache.hc.core5.http.io.entity.InputStreamEntity;
42 import org.apache.hc.core5.http.io.entity.StringEntity;
43 import org.junit.jupiter.api.Assertions;
44 import org.junit.jupiter.api.Test;
45
46 public class TestDecompressingEntity {
47
48 @Test
49 public void testNonStreaming() throws Exception {
50 final CRC32 crc32 = new CRC32();
51 final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
52 final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
53 Assertions.assertFalse(entity.isStreaming());
54 final String s = EntityUtils.toString(entity);
55 Assertions.assertEquals("1234567890", s);
56 Assertions.assertEquals(639479525L, crc32.getValue());
57 final InputStream in1 = entity.getContent();
58 final InputStream in2 = entity.getContent();
59 Assertions.assertNotSame(in1, in2);
60 }
61
62 @Test
63 public void testStreaming() throws Exception {
64 final CRC32 crc32 = new CRC32();
65 final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(StandardCharsets.US_ASCII));
66 final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.DEFAULT_TEXT);
67 final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
68 Assertions.assertTrue(entity.isStreaming());
69 final String s = EntityUtils.toString(entity);
70 Assertions.assertEquals("1234567890", s);
71 Assertions.assertEquals(639479525L, crc32.getValue());
72 final InputStream in1 = entity.getContent();
73 final InputStream in2 = entity.getContent();
74 Assertions.assertSame(in1, in2);
75 EntityUtils.consume(entity);
76 EntityUtils.consume(entity);
77 }
78
79 @Test
80 public void testWriteToStream() throws Exception {
81 final CRC32 crc32 = new CRC32();
82 final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
83 try (final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32)) {
84 Assertions.assertFalse(entity.isStreaming());
85
86 final ByteArrayOutputStream out = new ByteArrayOutputStream();
87 entity.writeTo(out);
88
89 final String s = new String(out.toByteArray(), StandardCharsets.US_ASCII);
90 Assertions.assertEquals("1234567890", s);
91 Assertions.assertEquals(639479525L, crc32.getValue());
92 }
93 }
94
95 static class ChecksumEntity extends DecompressingEntity {
96
97 public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
98 super(wrapped, inStream -> new CheckedInputStream(inStream, checksum));
99 }
100
101 }
102
103 }