1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-12 10:23:16 +03:00

Rename compression algorithms to be consistent. (#8984)

* Rename compression algorithms to be consistent.

Nice API for both Java and Kotlin devs.
With a test showing each.

Co-authored-by: Jake Wharton <github@jakewharton.com>

---------

Co-authored-by: Jake Wharton <github@jakewharton.com>
This commit is contained in:
Yuri Schimke
2025-07-31 18:34:32 +00:00
committed by GitHub
parent 2fd8fad20b
commit eda4064d6a
11 changed files with 66 additions and 26 deletions

View File

@@ -1,8 +1,10 @@
public final class okhttp3/brotli/Brotli : okhttp3/CompressionInterceptor$DecompressionAlgorithm {
public static final field INSTANCE Lokhttp3/brotli/Brotli;
public fun decompress (Lokio/BufferedSource;)Lokio/Source;
public fun getEncoding ()Ljava/lang/String;
}
public final class okhttp3/brotli/BrotliInterceptor : okhttp3/CompressionInterceptor {
public static final field INSTANCE Lokhttp3/brotli/BrotliInterceptor;
}
public final class okhttp3/brotli/BrotliInterceptorKt {
public static final fun getBrotli ()Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
}

View File

@@ -29,9 +29,8 @@ import org.brotli.dec.BrotliInputStream
*/
object BrotliInterceptor : CompressionInterceptor(Brotli, Gzip)
val Brotli =
object : CompressionInterceptor.DecompressionAlgorithm {
override val encoding: String = "br"
object Brotli : CompressionInterceptor.DecompressionAlgorithm {
override val encoding: String get() = "br"
override fun decompress(compressedSource: BufferedSource): Source = BrotliInputStream(compressedSource.inputStream()).source()
}
override fun decompress(compressedSource: BufferedSource): Source = BrotliInputStream(compressedSource.inputStream()).source()
}

View File

@@ -22,6 +22,8 @@ import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import java.io.IOException
import kotlin.test.assertFailsWith
import okhttp3.CompressionInterceptor
import okhttp3.CompressionInterceptor.Companion.Gzip
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Protocol
import okhttp3.Request
@@ -34,6 +36,8 @@ import okio.ByteString.Companion.encodeUtf8
import org.junit.jupiter.api.Test
class BrotliInterceptorTest {
val brotliInterceptor = CompressionInterceptor(Brotli, Gzip)
@Test
fun testUncompressBrotli() {
val s =
@@ -47,7 +51,7 @@ class BrotliInterceptorTest {
header("Content-Encoding", "br")
}
val uncompressed = BrotliInterceptor.decompress(response)
val uncompressed = brotliInterceptor.decompress(response)
val responseString = uncompressed.body.string()
assertThat(responseString).contains("\"brotli\": true,")
@@ -68,7 +72,7 @@ class BrotliInterceptorTest {
header("Content-Encoding", "gzip")
}
val uncompressed = BrotliInterceptor.decompress(response)
val uncompressed = brotliInterceptor.decompress(response)
val responseString = uncompressed.body.string()
assertThat(responseString).contains("\"gzipped\": true,")
@@ -79,7 +83,7 @@ class BrotliInterceptorTest {
fun testNoUncompress() {
val response = response("https://httpbin.org/brotli", "XXXX".encodeUtf8())
val same = BrotliInterceptor.decompress(response)
val same = brotliInterceptor.decompress(response)
val responseString = same.body.string()
assertThat(responseString).isEqualTo("XXXX")
@@ -93,7 +97,7 @@ class BrotliInterceptorTest {
}
assertFailsWith<IOException> {
val failingResponse = BrotliInterceptor.decompress(response)
val failingResponse = brotliInterceptor.decompress(response)
failingResponse.body.string()
}.also { ioe ->
assertThat(ioe).hasMessage("Brotli stream decoding failed")
@@ -110,7 +114,7 @@ class BrotliInterceptorTest {
message("NO CONTENT")
}
val same = BrotliInterceptor.decompress(response)
val same = brotliInterceptor.decompress(response)
val responseString = same.body.string()
assertThat(responseString).isEmpty()

View File

@@ -1,4 +1,6 @@
public final class okhttp3/zstd/ZstdInterceptorKt {
public static final fun getZstd ()Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
public final class okhttp3/zstd/Zstd : okhttp3/CompressionInterceptor$DecompressionAlgorithm {
public static final field INSTANCE Lokhttp3/zstd/Zstd;
public fun decompress (Lokio/BufferedSource;)Lokio/Source;
public fun getEncoding ()Ljava/lang/String;
}

View File

@@ -18,6 +18,7 @@ dependencies {
"friendsApi"(projects.okhttp)
implementation(libs.zstd.kmp.okio)
testImplementation(projects.okhttpBrotli)
testImplementation(projects.okhttpTestingSupport)
testImplementation(libs.kotlin.test.common)
testImplementation(libs.kotlin.test.junit)

View File

@@ -23,9 +23,8 @@ import okio.Source
/**
* Support for Zstandard encoding. Use via the [CompressionInterceptor].
*/
val Zstd =
object : CompressionInterceptor.DecompressionAlgorithm {
override val encoding: String = "zstd"
object Zstd : CompressionInterceptor.DecompressionAlgorithm {
override val encoding: String get() = "zstd"
override fun decompress(compressedSource: BufferedSource): Source = compressedSource.zstdDecompress()
}
override fun decompress(compressedSource: BufferedSource): Source = compressedSource.zstdDecompress()
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.zstd;
import okhttp3.CompressionInterceptor;
import okhttp3.brotli.Brotli;
import org.junit.jupiter.api.Test;
class ZstdInterceptorJavaTest {
@Test
public void testConstructor() {
CompressionInterceptor interceptor = new CompressionInterceptor(
Zstd.INSTANCE,
CompressionInterceptor.Gzip,
Brotli.INSTANCE
);
}
}

View File

@@ -24,6 +24,7 @@ import com.squareup.zstd.okio.zstdCompress
import java.io.IOException
import kotlin.test.assertFailsWith
import okhttp3.CompressionInterceptor
import okhttp3.CompressionInterceptor.Companion.Gzip
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Protocol
import okhttp3.Request
@@ -39,7 +40,7 @@ import okio.gzip
import org.junit.jupiter.api.Test
class ZstdInterceptorTest {
val zstdInterceptor = CompressionInterceptor(Zstd, CompressionInterceptor.Gzip)
val zstdInterceptor = CompressionInterceptor(Zstd, Gzip)
@Test
fun testDecompressZstd() {

View File

@@ -328,13 +328,13 @@ public final class okhttp3/CipherSuite$Companion {
public class okhttp3/CompressionInterceptor : okhttp3/Interceptor {
public static final field Companion Lokhttp3/CompressionInterceptor$Companion;
public static final field Gzip Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
public fun <init> ([Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;)V
public final fun getAlgorithms ()[Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response;
}
public final class okhttp3/CompressionInterceptor$Companion {
public final fun getGzip ()Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
}
public abstract interface class okhttp3/CompressionInterceptor$DecompressionAlgorithm {

View File

@@ -328,13 +328,13 @@ public final class okhttp3/CipherSuite$Companion {
public class okhttp3/CompressionInterceptor : okhttp3/Interceptor {
public static final field Companion Lokhttp3/CompressionInterceptor$Companion;
public static final field Gzip Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
public fun <init> ([Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;)V
public final fun getAlgorithms ()[Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response;
}
public final class okhttp3/CompressionInterceptor$Companion {
public final fun getGzip ()Lokhttp3/CompressionInterceptor$DecompressionAlgorithm;
}
public abstract interface class okhttp3/CompressionInterceptor$DecompressionAlgorithm {

View File

@@ -98,9 +98,10 @@ open class CompressionInterceptor(
/**
* Request "gzip" compression.
*/
val Gzip =
@JvmField
public val Gzip =
object : DecompressionAlgorithm {
override val encoding: String = "gzip"
override val encoding: String get() = "gzip"
override fun decompress(compressedSource: BufferedSource): Source = GzipSource(compressedSource)
}