Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save baldram/cfcb9203baaf968107f1d3da44ae1150 to your computer and use it in GitHub Desktop.
Save baldram/cfcb9203baaf968107f1d3da44ae1150 to your computer and use it in GitHub Desktop.
"Sttp test reproducing java.io.EOFException/GZIPInputStream bug"
//> using lib "org.scalatest::scalatest:3.2.15"
//> using lib "com.softwaremill.sttp.client3::core:3.8.14"
//> using lib "com.softwaremill.sttp.client3::okhttp-backend:3.8.14"
//> using lib "com.softwaremill.sttp.client3::async-http-client-backend-zio1:3.8.14"
//> using lib "com.softwaremill.sttp.client3::async-http-client-backend-cats:3.8.14"
//> using lib "org.typelevel::cats-effect:3.4.8"
//> using lib "com.softwaremill.sttp.client3::async-http-client-backend-future:3.8.14"
//> using lib "com.github.tomakehurst:wiremock:2.27.2"
//> using jvm "17"
// Notice: when using the latest client4 4.0.0-M1, the issue is also reproducible. Please adjust above imports to see.
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.*
import org.asynchttpclient.DefaultAsyncHttpClient
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.{BeforeAndAfterAll, TryValues}
import sttp.capabilities
import sttp.client3.*
import sttp.model.StatusCode
import scala.util.Try
class SttpSupportGzipEncodingWithEmptyResponseTest extends AnyFunSuite with BeforeAndAfterAll with TryValues {
val wireMockServer: WireMockServer = new WireMockServer(8089)
lazy val apiUrl = uri"${wireMockServer.baseUrl()}"
override protected def beforeAll(): Unit = {
wireMockServer.start()
mockIssue()
super.beforeAll()
}
private def mockIssue() =
wireMockServer.stubFor {
post(anyUrl())
.willReturn(
aResponse()
.withStatus(StatusCode.Accepted.code)
.withHeader("Content-Encoding", "gzip") // Notice: when commenting out this line, all tests succeed
)
}
override protected def afterAll(): Unit = {
super.afterAll()
wireMockServer.stop()
}
test("should succeed using simple synchronous client") {
import sttp.client3.quick.*
val response = simpleHttpClient.send(quickRequest.post(apiUrl))
assert(response.code == StatusCode.Accepted)
}
test("should succeed using synchronous backend") {
val backend = HttpClientSyncBackend()
val response = basicRequest
.body("Hello, world!")
.post(apiUrl)
.send(backend)
assert(response.code == StatusCode.Accepted)
}
test("should succeed using OK HTTP backend") {
import sttp.client3.okhttp.quick.*
val response = quickRequest.post(apiUrl).send(backend)
assert(response.code == StatusCode.Accepted)
}
test("should succeed using Future backend") {
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
val backend = HttpClientFutureBackend()
val futureCall = quickRequest.post(apiUrl).send(backend)
val response = Try(Await.result(futureCall, 5.seconds))
assert(response.success.value.code == StatusCode.Accepted)
}
test("should succeed using Cats backend") {
import cats.effect.*
import cats.effect.unsafe.implicits.global
import sttp.client3.httpclient.cats.HttpClientCatsBackend
object HelloWorld extends IOApp.Simple {
val run: IO[Unit] =
for {
backend <- HttpClientCatsBackend.resource[IO]().use(IO.pure)
response <- quickRequest.post(apiUrl).send(backend)
} yield assert(response.code == StatusCode.Accepted)
}
HelloWorld.run.unsafeRunSync()
}
test("should succeed using ZIO backend") {
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio.Runtime
val backend = AsyncHttpClientZioBackend.usingClient(Runtime.default, new DefaultAsyncHttpClient())
val futureCall = quickRequest.post(apiUrl).send(backend)
val response = Runtime.default.unsafeRunSync(futureCall)
assert(response.toEither.toTry.success.value.code == StatusCode.Accepted)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment