Skip to content

Commit

Permalink
fix: Decompression lenient mode fails on unknown directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Jan 8, 2023
1 parent 22eefb4 commit e816aa0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/nstdio/http/ext/BodyHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public DecompressingBodyHandlerBuilder failOnUnknownDirectives(boolean failOnUnk
*/
public DecompressingBodyHandlerBuilder lenient(boolean lenient) {
return failOnUnsupportedDirectives(!lenient)
.failOnUnsupportedDirectives(!lenient);
.failOnUnknownDirectives(!lenient);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyNoMoreInteractions
import org.mockito.junit.jupiter.MockitoExtension
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.InputStream
import java.net.http.HttpHeaders
import java.net.http.HttpResponse.BodyHandler
Expand Down Expand Up @@ -118,9 +117,6 @@ internal class DecompressingBodyHandlerTest {

@ParameterizedTest
@ValueSource(strings = ["gzip", "x-gzip"])
@Throws(
IOException::class
)
fun shouldReturnGzipInputStream(directive: String?) {
val gzipContent = ByteArrayInputStream(Compression.gzip("abc"))

Expand Down Expand Up @@ -153,8 +149,7 @@ internal class DecompressingBodyHandlerTest {
fun shouldThrowUnsupportedOperationException(directive: String?) {
//given
val handler = BodyHandlers.decompressingBuilder()
.failOnUnsupportedDirectives(true)
.failOnUnknownDirectives(true)
.lenient(false)
.build(mockHandler) as DecompressingBodyHandler

//when + then
Expand All @@ -167,8 +162,7 @@ internal class DecompressingBodyHandlerTest {
@ValueSource(strings = ["", "abc", "gz", "a"])
fun shouldThrowIllegalArgumentException(directive: String?) {
val handler = BodyHandlers.decompressingBuilder()
.failOnUnsupportedDirectives(true)
.failOnUnknownDirectives(true)
.lenient(false)
.build(mockHandler) as DecompressingBodyHandler
//when + then
assertThatIllegalArgumentException()
Expand All @@ -178,8 +172,7 @@ internal class DecompressingBodyHandlerTest {

@ParameterizedTest
@ValueSource(strings = ["compress", "br"])
@DisplayName("Should not throw exception when 'failOnUnsupportedDirectives' is 'false'")
fun shouldNotThrowUnsupportedOperationException(directive: String?) {
fun `Should not throw exception when 'failOnUnsupportedDirectives' is 'false'`(directive: String?) {
//given
val handler = BodyHandlers.decompressingBuilder()
.failOnUnsupportedDirectives(false)
Expand Down Expand Up @@ -213,5 +206,22 @@ internal class DecompressingBodyHandlerTest {
//then
actual shouldBeSameInstanceAs s
}

@ParameterizedTest
@ValueSource(strings = ["", "abc", "gz", "a"])
fun `Should not throw exception in lenient mode`(directive: String?) {
//given
val handler = BodyHandlers.decompressingBuilder()
.lenient(true)
.build(mockHandler) as DecompressingBodyHandler
val s = InputStream.nullInputStream()

//when
val fn = handler.decompressionFn(directive)
val actual = fn.apply(s)

//then
actual shouldBeSameInstanceAs s
}
}
}

0 comments on commit e816aa0

Please sign in to comment.