From 3ff29c9403154e442301fc06f5491b6316674098 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Tue, 14 May 2024 16:05:34 -0500 Subject: [PATCH 1/4] make gzip optional --- .../com/segment/analytics/kotlin/core/HTTPClient.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt b/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt index bddbc605..bebd4ce4 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt @@ -88,9 +88,13 @@ internal fun HttpURLConnection.createGetConnection(): Connection { } internal fun HttpURLConnection.createPostConnection(): Connection { - val outputStream: OutputStream - setRequestProperty("Content-Encoding", "gzip") - outputStream = GZIPOutputStream(this.outputStream) + val outputStream: OutputStream = + if (getRequestProperty("Content-Encoding") == "gzip") { + GZIPOutputStream(this.outputStream) + } + else { + this.outputStream + } return object : Connection(this, null, outputStream) { @Throws(IOException::class) override fun close() { @@ -148,6 +152,7 @@ open class RequestFactory { open fun upload(apiHost: String): HttpURLConnection { val connection: HttpURLConnection = openConnection("https://$apiHost/b") connection.setRequestProperty("Content-Type", "text/plain") + connection.setRequestProperty("Content-Encoding", "gzip") connection.doOutput = true connection.setChunkedStreamingMode(0) return connection From 1511b07fd327c28d97fed1933906eaeda159208f Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Tue, 14 May 2024 16:12:13 -0500 Subject: [PATCH 2/4] update unit test --- .../com/segment/analytics/kotlin/core/HTTPClientTests.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt index c582dfb3..42cfcecf 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt @@ -6,6 +6,7 @@ import io.mockk.every import io.mockk.mockk import io.mockk.spyk import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Assertions.fail import org.junit.jupiter.api.Test @@ -18,6 +19,7 @@ import java.net.HttpURLConnection import java.net.MalformedURLException import java.net.URL import java.net.http.HttpClient +import java.util.zip.GZIPOutputStream @TestInstance(TestInstance.Lifecycle.PER_CLASS) class HTTPClientTests { @@ -134,6 +136,7 @@ class HTTPClientTests { "https://api.test.com", it.url.toString() ) + assertFalse(it.outputStream is GZIPOutputStream) } } } \ No newline at end of file From e7292fe5850afda439be7d48f9e863f9ff61f746 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Tue, 14 May 2024 16:34:35 -0500 Subject: [PATCH 3/4] add more unit test --- .../segment/analytics/kotlin/core/HTTPClient.kt | 3 ++- .../analytics/kotlin/core/HTTPClientTests.kt | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt b/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt index bebd4ce4..753a5672 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/HTTPClient.kt @@ -88,8 +88,9 @@ internal fun HttpURLConnection.createGetConnection(): Connection { } internal fun HttpURLConnection.createPostConnection(): Connection { + val encoding = getRequestProperty("Content-Encoding") ?: "" val outputStream: OutputStream = - if (getRequestProperty("Content-Encoding") == "gzip") { + if (encoding.contains("gzip")) { GZIPOutputStream(this.outputStream) } else { diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt index 42cfcecf..493a9462 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt @@ -139,4 +139,21 @@ class HTTPClientTests { assertFalse(it.outputStream is GZIPOutputStream) } } + + @Test + fun `custom requestFactory can remove gzip`() { + val httpClient = HTTPClient("123", object : RequestFactory() { + override fun upload(apiHost: String): HttpURLConnection { + val connection: HttpURLConnection = openConnection("https://$apiHost/b") + connection.setRequestProperty("Content-Type", "text/plain") + connection.doOutput = true + connection.setChunkedStreamingMode(0) + return connection + } + }) + + httpClient.upload("api.segment.io/v1").connection.let { + assertFalse(it.outputStream is GZIPOutputStream) + } + } } \ No newline at end of file From 8524c54c0c4119b9b839f0376d9f1b9c524873b7 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Tue, 14 May 2024 17:15:46 -0500 Subject: [PATCH 4/4] unit test bug fix --- .../analytics/kotlin/core/HTTPClientTests.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt index 493a9462..2d05a083 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/HTTPClientTests.kt @@ -47,7 +47,9 @@ class HTTPClientTests { @Test fun `settings connection has correct configuration`() { - httpClient.upload("api.segment.io/v1").connection.let { + httpClient.upload("api.segment.io/v1").also { + assertTrue(it.outputStream is GZIPOutputStream) + }.connection.let { assertEquals( "https://api.segment.io/v1/b", it.url.toString() @@ -131,12 +133,13 @@ class HTTPClientTests { ) } - httpClient.upload("api.segment.io/v1").connection.let { + httpClient.upload("api.segment.io/v1").also { + assertFalse(it.outputStream is GZIPOutputStream) + }.connection.let { assertEquals( "https://api.test.com", it.url.toString() ) - assertFalse(it.outputStream is GZIPOutputStream) } } @@ -152,8 +155,6 @@ class HTTPClientTests { } }) - httpClient.upload("api.segment.io/v1").connection.let { - assertFalse(it.outputStream is GZIPOutputStream) - } + assertFalse(httpClient.upload("api.segment.io/v1").outputStream is GZIPOutputStream) } } \ No newline at end of file