aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/http/GzipDecompressingEntityTestCase.java
blob: 6babba5a36aab9a82ab8a164f2e7981ff299a290 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.http;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Random;
import java.util.zip.GZIPOutputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicHeader;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.text.Utf8;

/**
 * Test GZip support for the HTTP integration introduced in 4.2.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class GzipDecompressingEntityTestCase {
    private static final String STREAM_CONTENT = "00000000000000000000000000000000000000000000000000";
    private static final byte[] CONTENT_AS_BYTES = Utf8.toBytes(STREAM_CONTENT);
    GzipDecompressingEntity testEntity;

    private static final class MockEntity implements HttpEntity {

        private final InputStream inStream;

        MockEntity(InputStream is) {
            inStream = is;
        }

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

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

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public Header getContentType() {
            return new BasicHeader("Content-Type", "text/plain");
        }

        @Override
        public Header getContentEncoding() {
            return new BasicHeader("Content-Encoding", "gzip");
        }

        @Override
        public InputStream getContent() throws IOException,
                IllegalStateException {
            return inStream;
        }

        @Override
        public void writeTo(OutputStream outstream) throws IOException {
        }

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

        @Override
        public void consumeContent() throws IOException {
        }
    }

    @Before
    public void setUp() throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(CONTENT_AS_BYTES);
        gzip.finish();
        gzip.close();
        byte[] compressed = out.toByteArray();
        InputStream inStream = new ByteArrayInputStream(compressed);
        testEntity = new GzipDecompressingEntity(new MockEntity(inStream));
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testGetContentLength() throws UnknownHostException {
        assertEquals(STREAM_CONTENT.length(), testEntity.getContentLength());
    }

    @Test
    public final void testGetContent() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        byte[] buffer = new byte[CONTENT_AS_BYTES.length];
        int read = in.read(buffer);
        assertEquals(CONTENT_AS_BYTES.length, read);
        assertArrayEquals(CONTENT_AS_BYTES, buffer);
    }

    @Test
    public final void testGetContentToBigArray() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        byte[] buffer = new byte[CONTENT_AS_BYTES.length * 2];
        in.read(buffer);
        byte[] expected = Arrays.copyOf(CONTENT_AS_BYTES, CONTENT_AS_BYTES.length * 2);
        assertArrayEquals(expected, buffer);
    }

    @Test
    public final void testGetContentAvailable() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        assertEquals(CONTENT_AS_BYTES.length, in.available());
    }

    @Test
    public final void testLargeZip() throws IOException {
        byte [] input = new byte [10000000];
        Random random = new Random(89);
        for (int i = 0; i < input.length; i++) {
            input[i] = (byte) random.nextInt();
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(input);
        gzip.finish();
        gzip.close();
        byte[] compressed = out.toByteArray();
        assertEquals(10003073, compressed.length);
        InputStream inStream = new ByteArrayInputStream(compressed);
        GzipDecompressingEntity gunzipper = new GzipDecompressingEntity(new MockEntity(inStream));
        assertEquals(input.length, gunzipper.getContentLength());
        byte[] buffer = new byte[input.length];
        InputStream content = gunzipper.getContent();
        assertEquals(input.length, content.available());
        int read = content.read(buffer);
        assertEquals(input.length, read);
        assertArrayEquals(input, buffer);
    }

    @Test
    public final void testGetContentReadByte() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        byte[] buffer = new byte[CONTENT_AS_BYTES.length * 2];
        int i = 0;
        while (i < buffer.length) {
            int r = in.read();
            if (r == -1) {
                break;
            } else {
                buffer[i++] = (byte) r;
            }
        }
        byte[] expected = Arrays.copyOf(CONTENT_AS_BYTES, CONTENT_AS_BYTES.length * 2);
        assertEquals(CONTENT_AS_BYTES.length, i);
        assertArrayEquals(expected, buffer);
    }

    @Test
    public final void testGetContentReadWithOffset() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        byte[] buffer = new byte[CONTENT_AS_BYTES.length * 2];
        int read = in.read(buffer, CONTENT_AS_BYTES.length, CONTENT_AS_BYTES.length);
        assertEquals(CONTENT_AS_BYTES.length, read);
        byte[] expected = new byte[CONTENT_AS_BYTES.length * 2];
        for (int i = 0; i < CONTENT_AS_BYTES.length; ++i) {
            expected[CONTENT_AS_BYTES.length + i] = CONTENT_AS_BYTES[i];
        }
        assertArrayEquals(expected, buffer);
        read = in.read(buffer, 0, CONTENT_AS_BYTES.length);
        assertEquals(-1, read);
    }

    @Test
    public final void testGetContentSkip() throws IllegalStateException, IOException {
        InputStream in = testEntity.getContent();
        final long n = 5L;
        long skipped = in.skip(n);
        assertEquals(n, skipped);
        int read = in.read();
        assertEquals(CONTENT_AS_BYTES[(int) n], read);
        skipped = in.skip(5000);
        assertEquals(CONTENT_AS_BYTES.length - n - 1, skipped);
        assertEquals(-1L, in.skip(1L));
    }


    @Test
    public final void testWriteToOutputStream() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        testEntity.writeTo(out);
        assertArrayEquals(CONTENT_AS_BYTES, out.toByteArray());
    }

}