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.IOException;
33  import java.io.InputStream;
34  import java.nio.charset.StandardCharsets;
35  import java.util.zip.CRC32;
36  import java.util.zip.CheckedInputStream;
37  import java.util.zip.Checksum;
38  
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.http.HttpEntity;
41  import org.apache.hc.core5.http.io.entity.EntityUtils;
42  import org.apache.hc.core5.http.io.entity.InputStreamEntity;
43  import org.apache.hc.core5.http.io.entity.StringEntity;
44  import org.junit.Assert;
45  import org.junit.Test;
46  
47  public class TestDecompressingEntity {
48  
49      @Test
50      public void testNonStreaming() throws Exception {
51          final CRC32 crc32 = new CRC32();
52          final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
53          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
54          Assert.assertFalse(entity.isStreaming());
55          final String s = EntityUtils.toString(entity);
56          Assert.assertEquals("1234567890", s);
57          Assert.assertEquals(639479525L, crc32.getValue());
58          final InputStream in1 = entity.getContent();
59          final InputStream in2 = entity.getContent();
60          Assert.assertTrue(in1 != in2);
61      }
62  
63      @Test
64      public void testStreaming() throws Exception {
65          final CRC32 crc32 = new CRC32();
66          final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(StandardCharsets.US_ASCII));
67          final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.DEFAULT_TEXT);
68          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
69          Assert.assertTrue(entity.isStreaming());
70          final String s = EntityUtils.toString(entity);
71          Assert.assertEquals("1234567890", s);
72          Assert.assertEquals(639479525L, crc32.getValue());
73          final InputStream in1 = entity.getContent();
74          final InputStream in2 = entity.getContent();
75          Assert.assertTrue(in1 == in2);
76          EntityUtils.consume(entity);
77          EntityUtils.consume(entity);
78      }
79  
80      @Test
81      public void testWriteToStream() throws Exception {
82          final CRC32 crc32 = new CRC32();
83          final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
84          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
85          Assert.assertFalse(entity.isStreaming());
86  
87          final ByteArrayOutputStream out = new ByteArrayOutputStream();
88          entity.writeTo(out);
89  
90          final String s = new String(out.toByteArray(), StandardCharsets.US_ASCII);
91          Assert.assertEquals("1234567890", s);
92          Assert.assertEquals(639479525L, crc32.getValue());
93      }
94  
95      static class ChecksumEntity extends DecompressingEntity {
96  
97          public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
98              super(wrapped, new InputStreamFactory() {
99  
100                 @Override
101                 public InputStream create(final InputStream inStream) throws IOException {
102                     return new CheckedInputStream(inStream, checksum);
103                 }
104 
105             });
106         }
107 
108     }
109 
110 }