Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add compression test: gzip #1448

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.netflix.netty.common.metrics.CustomLeakDetector;
import com.netflix.zuul.integration.server.Bootstrap;
import com.netflix.zuul.integration.server.HeaderNames;
import com.netflix.zuul.integration.server.TestUtil;
import io.netty.util.ResourceLeakDetector;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand All @@ -34,16 +35,21 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -52,6 +58,8 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;

import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
Expand All @@ -64,6 +72,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.netflix.netty.common.metrics.CustomLeakDetector.assertZeroLeaks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class IntegrationTest {
Expand Down Expand Up @@ -319,6 +328,95 @@ void httpGet_ServerChunkedDribbleDelay(final String description, final OkHttpCli
response.close();
}

@Test
void deflateOnly() throws Exception {
final String expectedResponseBody = TestUtil.COMPRESSIBLE_CONTENT;
final WireMock wireMock = wmRuntimeInfo.getWireMock();
wireMock.register(
get(anyUrl())
.willReturn(
aResponse()
.withStatus(200)
.withBody(expectedResponseBody)
.withHeader("Content-Type", TestUtil.COMPRESSIBLE_CONTENT_TYPE)));
URL url = new URL(zuulBaseUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Accept-Encoding", "deflate");
InputStream inputStream = connection.getInputStream();
assertEquals(200, connection.getResponseCode());
assertEquals("text/plain", connection.getHeaderField("Content-Type"));
assertEquals("deflate", connection.getHeaderField("Content-Encoding"));
byte[] compressedData = IOUtils.toByteArray(inputStream);
Inflater inflater = new Inflater();
inflater.setInput(compressedData);
byte[] result = new byte[1000];
int nBytes = inflater.inflate(result);
String text = new String(result, 0, nBytes, TestUtil.CHARSET);
assertEquals(expectedResponseBody, text);
inputStream.close();
connection.disconnect();
}

@Test
void gzipOnly() throws Exception {
final String expectedResponseBody = TestUtil.COMPRESSIBLE_CONTENT;
final WireMock wireMock = wmRuntimeInfo.getWireMock();
wireMock.register(
get(anyUrl())
.willReturn(
aResponse()
.withStatus(200)
.withBody(expectedResponseBody)
.withHeader("Content-Type", TestUtil.COMPRESSIBLE_CONTENT_TYPE)));

URL url = new URL(zuulBaseUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Accept-Encoding", "gzip");
InputStream inputStream = connection.getInputStream();
assertEquals(200, connection.getResponseCode());
assertEquals("text/plain", connection.getHeaderField("Content-Type"));
assertEquals("gzip", connection.getHeaderField("Content-Encoding"));
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
byte[] data = IOUtils.toByteArray(gzipInputStream);
String text = new String(data, TestUtil.CHARSET);
assertEquals(expectedResponseBody, text);
inputStream.close();
gzipInputStream.close();
connection.disconnect();
}

@Test
void noCompression() throws Exception {
final String expectedResponseBody = TestUtil.COMPRESSIBLE_CONTENT;
final WireMock wireMock = wmRuntimeInfo.getWireMock();
wireMock.register(
get(anyUrl())
.willReturn(
aResponse()
.withStatus(200)
.withBody(expectedResponseBody)
.withHeader("Content-Type", TestUtil.COMPRESSIBLE_CONTENT_TYPE)));

URL url = new URL(zuulBaseUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Accept-Encoding", ""); // no compression
InputStream inputStream = connection.getInputStream();
assertEquals(200, connection.getResponseCode());
assertEquals("text/plain", connection.getHeaderField("Content-Type"));
assertNull(connection.getHeaderField("Content-Encoding"));
byte[] data = IOUtils.toByteArray(inputStream);
String text = new String(data, TestUtil.CHARSET);
assertEquals(expectedResponseBody, text);
inputStream.close();
connection.disconnect();
}

private static String randomPathSegment() {
return UUID.randomUUID().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import com.netflix.zuul.netty.server.push.PushConnectionRegistry;
import com.netflix.zuul.netty.ssl.BaseSslContextFactory;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.compression.CompressionOptions;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.ssl.ClientAuth;

import javax.inject.Inject;
Expand Down Expand Up @@ -123,7 +126,14 @@ protected Map<NamedSocketAddress, ChannelInitializer<?>> chooseAddrsAndChannels(
addrsToChannels.put(
new NamedSocketAddress("http", sockAddr),
new ZuulServerChannelInitializer(
metricId, channelConfig, channelDependencies, clientChannels));
metricId, channelConfig, channelDependencies, clientChannels) {
@Override
protected void addHttp1Handlers(ChannelPipeline pipeline)
{
super.addHttp1Handlers(pipeline);
pipeline.addLast(new HttpContentCompressor((CompressionOptions[]) null));
}
});
logAddrConfigured(sockAddr);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
import com.netflix.appinfo.InstanceInfo;
import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class TestUtil {
private TestUtil() { }

public static final Charset CHARSET = StandardCharsets.UTF_8;

public static final String COMPRESSIBLE_CONTENT = "Hello Hello Hello Hello Hello";
public static final String COMPRESSIBLE_CONTENT_TYPE = "text/plain";

public static DiscoveryEnabledServer makeDiscoveryEnabledServer(final String appName, final String ipAddress, final int port) {
InstanceInfo instanceInfo = new InstanceInfo(
UUID.randomUUID().toString(),
Expand Down