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.http.client.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.OutputStream;
34
35 import org.apache.http.Consts;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.entity.ByteArrayEntity;
38 import org.apache.http.entity.ContentType;
39 import org.apache.http.entity.InputStreamEntity;
40 import org.apache.http.entity.StringEntity;
41 import org.apache.http.util.EntityUtils;
42 import org.junit.Assert;
43 import org.junit.Test;
44 import org.mockito.Matchers;
45 import org.mockito.Mockito;
46
47 public class TestGZip {
48
49 @Test
50 public void testBasic() throws Exception {
51 final String s = "some kind of text";
52 final StringEntity e = new StringEntity(s, ContentType.TEXT_PLAIN);
53 e.setChunked(false);
54 final GzipCompressingEntity gzipe = new GzipCompressingEntity(e);
55 Assert.assertTrue(gzipe.isChunked());
56 Assert.assertEquals(-1, gzipe.getContentLength());
57 Assert.assertNotNull(gzipe.getContentEncoding());
58 Assert.assertEquals("gzip", gzipe.getContentEncoding().getValue());
59 }
60
61 @Test
62 public void testCompressionDecompression() throws Exception {
63 final StringEntity in = new StringEntity("some kind of text", ContentType.TEXT_PLAIN);
64 final GzipCompressingEntity gzipe = new GzipCompressingEntity(in);
65 final ByteArrayOutputStream buf = new ByteArrayOutputStream();
66 gzipe.writeTo(buf);
67 final ByteArrayEntity out = new ByteArrayEntity(buf.toByteArray());
68 final GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(out);
69 Assert.assertEquals("some kind of text", EntityUtils.toString(gunzipe, Consts.ASCII));
70 }
71
72 @Test
73 public void testCompressionIOExceptionLeavesOutputStreamOpen() throws Exception {
74 final HttpEntity in = Mockito.mock(HttpEntity.class);
75 Mockito.doThrow(new IOException("Ooopsie")).when(in).writeTo(Matchers.<OutputStream>any());
76 final GzipCompressingEntity gzipe = new GzipCompressingEntity(in);
77 final OutputStream out = Mockito.mock(OutputStream.class);
78 try {
79 gzipe.writeTo(out);
80 } catch (final IOException ex) {
81 Mockito.verify(out, Mockito.never()).close();
82 }
83 }
84
85 @Test
86 public void testDecompressionWithMultipleGZipStream() throws IOException {
87 final int[] data = new int[] {
88 0x1f, 0x8b, 0x08, 0x08, 0x03, 0xf1, 0x55, 0x5a, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x31, 0x00,
89 0x2b, 0x2e, 0x29, 0x4a, 0x4d, 0xcc, 0xd5, 0x35, 0xe4, 0x02, 0x00, 0x03, 0x61, 0xf0, 0x5f, 0x09,
90 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x08, 0x08, 0xf1, 0x55, 0x5a, 0x00, 0x03, 0x74, 0x65, 0x73,
91 0x74, 0x32, 0x00, 0x2b, 0x2e, 0x29, 0x4a, 0x4d, 0xcc, 0xd5, 0x35, 0xe2, 0x02, 0x00, 0xc0, 0x32,
92 0xdd, 0x74, 0x09, 0x00, 0x00, 0x00
93 };
94 final byte[] bytes = new byte[data.length];
95 for (int i = 0; i < data.length; i++) {
96 bytes[i] = (byte) (data[i] & 0xff);
97 }
98
99 final InputStreamEntity out = new InputStreamEntity(new ByteArrayInputStream(bytes));
100 final GzipDecompressingEntity gunZipEntity = new GzipDecompressingEntity(out);
101 Assert.assertEquals("stream-1\nstream-2\n", EntityUtils.toString(gunZipEntity, Consts.ASCII));
102 }
103
104 }