use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class TestRestCompressionEcho method compressionEchoData.
@DataProvider
public Object[][] compressionEchoData() {
EncodingType[] encodings = new EncodingType[] { EncodingType.GZIP, EncodingType.SNAPPY, EncodingType.IDENTITY };
Object[][] args = new Object[4 * encodings.length * encodings.length][2];
int cur = 0;
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
args[cur][0] = http1Client;
args[cur][1] = LARGE_BYTES_NUM;
args[cur + 1][0] = http2Client;
args[cur + 1][1] = LARGE_BYTES_NUM;
cur += 2;
_clientFactories.add(factory);
_clients.add(http1Client);
_clients.add(http2Client);
}
}
// test data that won't trigger compression
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
args[cur][0] = http1Client;
args[cur][1] = SMALL_BYTES_NUM;
args[cur + 1][0] = http2Client;
args[cur + 1][1] = SMALL_BYTES_NUM;
cur += 2;
_clientFactories.add(factory);
_clients.add(http1Client);
_clients.add(http2Client);
}
}
return args;
}
use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
private void testResponseCompression(URI uri, long bytes, String acceptEncoding, final StreamingCompressor compressor) throws InterruptedException, TimeoutException, ExecutionException {
for (Client client : clients()) {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, uri)));
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
final FutureCallback<None> readerCallback = new FutureCallback<None>();
final BytesReader reader = new BytesReader(BYTE, readerCallback);
final EntityStream decompressedStream = compressor.inflate(response.getEntityStream());
decompressedStream.setReader(reader);
readerCallback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(reader.getTotalBytes(), bytes);
Assert.assertTrue(reader.allBytesCorrect());
}
}
use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class TestResponseCompression method testEncodingNotAcceptable.
public void testEncodingNotAcceptable(String acceptEncoding) throws TimeoutException, InterruptedException {
for (Client client : clients()) {
StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, SMALL_URI)));
if (acceptEncoding != null) {
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
}
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
client.streamRequest(request, callback);
try {
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.fail("Should have thrown exception when encoding is not acceptable");
} catch (ExecutionException e) {
Throwable t = e.getCause();
Assert.assertTrue(t instanceof StreamException);
StreamResponse response = ((StreamException) t).getResponse();
Assert.assertEquals(response.getStatus(), HttpConstants.NOT_ACCEPTABLE);
}
}
}
use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class TestStreamResponseCompression method testResponseCompression.
private void testResponseCompression(URI uri, long bytes, String acceptEncoding) throws InterruptedException, TimeoutException, ExecutionException {
StreamRequestBuilder builder = new StreamRequestBuilder((_clientProvider.createHttpURI(_port, uri)));
builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
StreamRequest request = builder.build(EntityStreams.emptyStream());
final FutureCallback<StreamResponse> callback = new FutureCallback<>();
_client.streamRequest(request, callback);
final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
final FutureCallback<None> readerCallback = new FutureCallback<>();
final BytesReader reader = new BytesReader(BYTE, readerCallback);
final EntityStream decompressedStream = response.getEntityStream();
decompressedStream.setReader(reader);
readerCallback.get(60, TimeUnit.SECONDS);
Assert.assertEquals(reader.getTotalBytes(), bytes);
Assert.assertTrue(reader.allBytesCorrect());
}
use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class TestRestCompressionEcho method compressionEchoData.
@DataProvider
public Object[][] compressionEchoData() throws Exception {
EncodingType[] encodings = new EncodingType[] { EncodingType.GZIP, EncodingType.SNAPPY, EncodingType.IDENTITY };
Object[][] args = new Object[2 * encodings.length * encodings.length][2];
int cur = 0;
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
Client client = _clientProvider.createClient(FilterChains.createRestChain(clientCompressionFilter), getHttpClientProperties());
args[cur][0] = client;
args[cur][1] = LARGE_BYTES_NUM;
cur++;
_clients.add(client);
}
}
// test data that won't trigger compression
for (EncodingType requestEncoding : encodings) {
for (EncodingType acceptEncoding : encodings) {
RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
Client client = _clientProvider.createClient(FilterChains.createRestChain(clientCompressionFilter), getHttpClientProperties());
args[cur][0] = client;
args[cur][1] = SMALL_BYTES_NUM;
cur++;
_clients.add(client);
}
}
return args;
}
Aggregations