diff --git a/zipkin-junit/src/main/java/zipkin/junit/ZipkinRule.java b/zipkin-junit/src/main/java/zipkin/junit/ZipkinRule.java index 44e3a159cb0..07888a87703 100644 --- a/zipkin-junit/src/main/java/zipkin/junit/ZipkinRule.java +++ b/zipkin-junit/src/main/java/zipkin/junit/ZipkinRule.java @@ -21,6 +21,8 @@ import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; +import okio.Buffer; +import okio.GzipSink; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -51,7 +53,21 @@ public ZipkinRule() { public MockResponse dispatch(RecordedRequest request) throws InterruptedException { MockResponse maybeFailure = failureQueue.poll(); if (maybeFailure != null) return maybeFailure; - return successDispatch.dispatch(request); + MockResponse result = successDispatch.dispatch(request); + String encoding = request.getHeaders().get("Accept-Encoding"); + if (result.getBody() != null && encoding != null && encoding.contains("gzip")) { + try { + Buffer sink = new Buffer(); + GzipSink gzipSink = new GzipSink(sink); + gzipSink.write(result.getBody(), result.getBody().size()); + gzipSink.close(); + result.setBody(sink); + } catch (IOException e) { + throw new AssertionError(e); + } + result.setHeader("Content-Encoding", "gzip"); + } + return result; } @Override diff --git a/zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java b/zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java index f1b7adefb19..26a08cb60ae 100644 --- a/zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java +++ b/zipkin-junit/src/test/java/zipkin/junit/ZipkinRuleTest.java @@ -15,6 +15,8 @@ import java.io.IOException; import java.net.ConnectException; +import java.util.Arrays; +import java.util.List; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -26,6 +28,7 @@ import zipkin.Codec; import zipkin.Endpoint; import zipkin.Span; +import zipkin.internal.Util; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -151,4 +154,27 @@ public void gzippedSpans_invalidIs400() throws IOException { assertThat(response.code()).isEqualTo(400); } + + @Test + public void readSpans_gzippedResponse() throws Exception { + char[] annotation2K = new char[2048]; + Arrays.fill(annotation2K, 'a'); + + List trace = asList(new Span.Builder(span) + .addAnnotation(Annotation.create(ann.timestamp, new String(annotation2K), null)).build()); + + zipkin.storeSpans(trace); + + Response response = client.newCall(new Request.Builder() + .url(zipkin.httpUrl() + "/api/v1/trace/" + span.traceId) + .addHeader("Accept-Encoding", "gzip").build() + ).execute(); + + assertThat(response.code()).isEqualTo(200); + assertThat(response.body().contentLength()).isLessThan(annotation2K.length); + + byte[] unzipped = Util.gunzip(response.body().bytes()); + + assertThat(Codec.JSON.readSpans(unzipped)).isEqualTo(trace); + } } diff --git a/zipkin-server/src/main/resources/zipkin-server.yml b/zipkin-server/src/main/resources/zipkin-server.yml index a06350c0fea..d18f3380014 100644 --- a/zipkin-server/src/main/resources/zipkin-server.yml +++ b/zipkin-server/src/main/resources/zipkin-server.yml @@ -19,6 +19,8 @@ server: port: ${QUERY_PORT:9411} compression: enabled: true + # compresses any response over min-response-size (default is 2KiB) + mime-types: application/json spring: datasource: driver-class-name: org.mariadb.jdbc.Driver