View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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          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      static class ChecksumEntity extends DecompressingEntity {
95  
96          public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
97              super(wrapped, inStream -> new CheckedInputStream(inStream, checksum));
98          }
99  
100     }
101 
102 }