Skip to content

Commit

Permalink
Make gzip optional (#221)
Browse files Browse the repository at this point in the history
* make gzip optional

* update unit test

* add more unit test

* unit test bug fix

---------

Co-authored-by: Wenxi Zeng <[email protected]>
  • Loading branch information
wenxi-zeng and Wenxi Zeng authored May 15, 2024
1 parent bfd0418 commit 515d249
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ internal fun HttpURLConnection.createGetConnection(): Connection {
}

internal fun HttpURLConnection.createPostConnection(): Connection {
val outputStream: OutputStream
setRequestProperty("Content-Encoding", "gzip")
outputStream = GZIPOutputStream(this.outputStream)
val encoding = getRequestProperty("Content-Encoding") ?: ""
val outputStream: OutputStream =
if (encoding.contains("gzip")) {
GZIPOutputStream(this.outputStream)
}
else {
this.outputStream
}
return object : Connection(this, null, outputStream) {
@Throws(IOException::class)
override fun close() {
Expand Down Expand Up @@ -148,6 +153,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -45,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()
Expand Down Expand Up @@ -129,11 +133,28 @@ 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()
)
}
}

@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
}
})

assertFalse(httpClient.upload("api.segment.io/v1").outputStream is GZIPOutputStream)
}
}

0 comments on commit 515d249

Please sign in to comment.