Skip to content

Commit e816aa0

Browse files
committed
fix: Decompression lenient mode fails on unknown directives.
1 parent 22eefb4 commit e816aa0

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/main/java/io/github/nstdio/http/ext/BodyHandlers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public DecompressingBodyHandlerBuilder failOnUnknownDirectives(boolean failOnUnk
112112
*/
113113
public DecompressingBodyHandlerBuilder lenient(boolean lenient) {
114114
return failOnUnsupportedDirectives(!lenient)
115-
.failOnUnsupportedDirectives(!lenient);
115+
.failOnUnknownDirectives(!lenient);
116116
}
117117

118118
/**

src/test/kotlin/io/github/nstdio/http/ext/DecompressingBodyHandlerTest.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import org.mockito.Mockito.verify
3737
import org.mockito.Mockito.verifyNoMoreInteractions
3838
import org.mockito.junit.jupiter.MockitoExtension
3939
import java.io.ByteArrayInputStream
40-
import java.io.IOException
4140
import java.io.InputStream
4241
import java.net.http.HttpHeaders
4342
import java.net.http.HttpResponse.BodyHandler
@@ -118,9 +117,6 @@ internal class DecompressingBodyHandlerTest {
118117

119118
@ParameterizedTest
120119
@ValueSource(strings = ["gzip", "x-gzip"])
121-
@Throws(
122-
IOException::class
123-
)
124120
fun shouldReturnGzipInputStream(directive: String?) {
125121
val gzipContent = ByteArrayInputStream(Compression.gzip("abc"))
126122

@@ -153,8 +149,7 @@ internal class DecompressingBodyHandlerTest {
153149
fun shouldThrowUnsupportedOperationException(directive: String?) {
154150
//given
155151
val handler = BodyHandlers.decompressingBuilder()
156-
.failOnUnsupportedDirectives(true)
157-
.failOnUnknownDirectives(true)
152+
.lenient(false)
158153
.build(mockHandler) as DecompressingBodyHandler
159154

160155
//when + then
@@ -167,8 +162,7 @@ internal class DecompressingBodyHandlerTest {
167162
@ValueSource(strings = ["", "abc", "gz", "a"])
168163
fun shouldThrowIllegalArgumentException(directive: String?) {
169164
val handler = BodyHandlers.decompressingBuilder()
170-
.failOnUnsupportedDirectives(true)
171-
.failOnUnknownDirectives(true)
165+
.lenient(false)
172166
.build(mockHandler) as DecompressingBodyHandler
173167
//when + then
174168
assertThatIllegalArgumentException()
@@ -178,8 +172,7 @@ internal class DecompressingBodyHandlerTest {
178172

179173
@ParameterizedTest
180174
@ValueSource(strings = ["compress", "br"])
181-
@DisplayName("Should not throw exception when 'failOnUnsupportedDirectives' is 'false'")
182-
fun shouldNotThrowUnsupportedOperationException(directive: String?) {
175+
fun `Should not throw exception when 'failOnUnsupportedDirectives' is 'false'`(directive: String?) {
183176
//given
184177
val handler = BodyHandlers.decompressingBuilder()
185178
.failOnUnsupportedDirectives(false)
@@ -213,5 +206,22 @@ internal class DecompressingBodyHandlerTest {
213206
//then
214207
actual shouldBeSameInstanceAs s
215208
}
209+
210+
@ParameterizedTest
211+
@ValueSource(strings = ["", "abc", "gz", "a"])
212+
fun `Should not throw exception in lenient mode`(directive: String?) {
213+
//given
214+
val handler = BodyHandlers.decompressingBuilder()
215+
.lenient(true)
216+
.build(mockHandler) as DecompressingBodyHandler
217+
val s = InputStream.nullInputStream()
218+
219+
//when
220+
val fn = handler.decompressionFn(directive)
221+
val actual = fn.apply(s)
222+
223+
//then
224+
actual shouldBeSameInstanceAs s
225+
}
216226
}
217227
}

0 commit comments

Comments
 (0)