blob: 01784226c9e2e134a6411f873ae950424fcd3385 (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.server;
import com.yahoo.container.jdisc.HttpRequest;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FeedHandlerCompressionTest {
public static byte[] compress(final String dataToBrCompressed) throws IOException {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(dataToBrCompressed.length());
final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(dataToBrCompressed.getBytes());
gzipOutputStream.close();
byte[] compressedBytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return compressedBytes;
}
@Test
public void testUnzipStreamIfNeeded() throws Exception {
final String testData = "foo bar";
InputStream inputStream = new ByteArrayInputStream(compress(testData));
HttpRequest httpRequest = mock(HttpRequest.class);
when(httpRequest.getHeader("content-encoding")).thenReturn("gzip");
InputStream decompressedStream = FeedHandler.unzipStreamIfNeeded(inputStream, httpRequest);
final StringBuilder processedInput = new StringBuilder();
while (true) {
int readValue = decompressedStream.read();
if (readValue < 0) {
break;
}
processedInput.append((char)readValue);
}
assertThat(processedInput.toString(), is(testData));
}
/**
* Test by setting encoding, but not compressing data.
* @throws Exception
*/
@Test
public void testUnzipFails() throws Exception {
final String testData = "foo bar";
InputStream inputStream = new ByteArrayInputStream(testData.getBytes());
HttpRequest httpRequest = mock(HttpRequest.class);
when(httpRequest.getHeader("Content-Encoding")).thenReturn("gzip");
InputStream decompressedStream = FeedHandler.unzipStreamIfNeeded(inputStream, httpRequest);
final StringBuilder processedInput = new StringBuilder();
while (true) {
int readValue = decompressedStream.read();
if (readValue < 0) {
break;
}
processedInput.append((char)readValue);
}
assertThat(processedInput.toString(), is(testData));
}
}
|