|
| 1 | +package io.getstream; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import io.getstream.services.framework.StreamSDKClient; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.util.zip.GZIPOutputStream; |
| 9 | +import okhttp3.HttpUrl; |
| 10 | +import okhttp3.OkHttpClient; |
| 11 | +import okhttp3.Request; |
| 12 | +import okhttp3.Response; |
| 13 | +import okhttp3.mockwebserver.MockResponse; |
| 14 | +import okhttp3.mockwebserver.MockWebServer; |
| 15 | +import okhttp3.mockwebserver.RecordedRequest; |
| 16 | +import okio.Buffer; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +/** |
| 22 | + * Verifies the SDK relies on OkHttp's BridgeInterceptor to auto-negotiate gzip, by exercising the |
| 23 | + * SDK's OkHttpClient directly against a MockWebServer. |
| 24 | + */ |
| 25 | +public class GzipTest { |
| 26 | + |
| 27 | + private static final String TEST_API_KEY = "test-api-key"; |
| 28 | + private static final String TEST_API_SECRET = "test-api-secret-must-be-32-bytes-long"; |
| 29 | + |
| 30 | + private MockWebServer server; |
| 31 | + private OkHttpClient sdkHttpClient; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() throws Exception { |
| 35 | + server = new MockWebServer(); |
| 36 | + server.start(); |
| 37 | + StreamSDKClient sdk = new StreamSDKClient(TEST_API_KEY, TEST_API_SECRET); |
| 38 | + sdkHttpClient = sdk.getHttpClient().getHttpClient(); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + void tearDown() throws Exception { |
| 43 | + if (server != null) { |
| 44 | + server.shutdown(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testRequestAdvertisesGzip() throws Exception { |
| 50 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); |
| 51 | + |
| 52 | + HttpUrl url = server.url("/test"); |
| 53 | + Request request = new Request.Builder().url(url).get().build(); |
| 54 | + try (Response resp = sdkHttpClient.newCall(request).execute()) { |
| 55 | + assertTrue(resp.isSuccessful()); |
| 56 | + } |
| 57 | + |
| 58 | + RecordedRequest recorded = server.takeRequest(); |
| 59 | + String acceptEncoding = recorded.getHeader("Accept-Encoding"); |
| 60 | + assertNotNull( |
| 61 | + acceptEncoding, |
| 62 | + "OkHttp's BridgeInterceptor must add Accept-Encoding when no upstream interceptor sets it"); |
| 63 | + assertTrue( |
| 64 | + acceptEncoding.toLowerCase().contains("gzip"), |
| 65 | + "Accept-Encoding must advertise gzip, got: " + acceptEncoding); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testResponseGzipDecoded() throws Exception { |
| 70 | + String jsonBody = "{\"hello\":\"world\"}"; |
| 71 | + |
| 72 | + ByteArrayOutputStream gzippedBytes = new ByteArrayOutputStream(); |
| 73 | + try (GZIPOutputStream gz = new GZIPOutputStream(gzippedBytes)) { |
| 74 | + gz.write(jsonBody.getBytes(StandardCharsets.UTF_8)); |
| 75 | + } |
| 76 | + |
| 77 | + Buffer body = new Buffer().write(gzippedBytes.toByteArray()); |
| 78 | + server.enqueue( |
| 79 | + new MockResponse() |
| 80 | + .setResponseCode(200) |
| 81 | + .setHeader("Content-Type", "application/json") |
| 82 | + .setHeader("Content-Encoding", "gzip") |
| 83 | + .setBody(body)); |
| 84 | + |
| 85 | + HttpUrl url = server.url("/test"); |
| 86 | + Request request = new Request.Builder().url(url).get().build(); |
| 87 | + try (Response resp = sdkHttpClient.newCall(request).execute()) { |
| 88 | + String decoded = resp.body().string(); |
| 89 | + assertEquals(jsonBody, decoded, "OkHttp must transparently decode the gzip response"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments