diff options
author | Adam Warski <adam@warski.org> | 2017-09-25 15:28:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-25 15:28:19 +0200 |
commit | 45e2b62ceff751aa2a6446bd8472c284fe222337 (patch) | |
tree | 0e8ad1144f642f52eaa825489934beb7113d76ce | |
parent | e515b860a3fe33368d073ae0af419c0bc1928bed (diff) | |
parent | b0722d8def38e7f4a133b54d5b8d56558f6237c9 (diff) | |
download | sttp-45e2b62ceff751aa2a6446bd8472c284fe222337.tar.gz sttp-45e2b62ceff751aa2a6446bd8472c284fe222337.tar.bz2 sttp-45e2b62ceff751aa2a6446bd8472c284fe222337.zip |
Merge pull request #37 from cb372/fix-empty-response-npe
Fix NPE in HTTPUrlConnBackend in case of empty response
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala | 8 | ||||
-rw-r--r-- | tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | 24 |
2 files changed, 31 insertions, 1 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala index 62ef2bc..7e87795 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala @@ -204,7 +204,7 @@ class HttpURLConnectionBackend private ( .flatMap { case (k, vv) => vv.asScala.map((k, _)) } val contentEncoding = Option(c.getHeaderField(ContentEncodingHeader)) val code = c.getResponseCode - val wrappedIs = wrapInput(contentEncoding, is) + val wrappedIs = wrapInput(contentEncoding, handleNullInput(is)) val body = if (codeIsSuccess(code)) { Right(readResponseBody(wrappedIs, responseAs)) } else { @@ -247,6 +247,12 @@ class HttpURLConnectionBackend private ( } } + private def handleNullInput(is: InputStream): InputStream = + if (is == null) + new ByteArrayInputStream(Array.empty[Byte]) + else + is + private def wrapInput(contentEncoding: Option[String], is: InputStream): InputStream = contentEncoding.map(_.toLowerCase) match { diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index de033df..a2b9505 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -170,6 +170,17 @@ class BasicTests akka.pattern.after(1.second, using = actorSystem.scheduler)( Future.successful("Done")) } + } ~ path("empty_unauthorized_response") { + post { + import akka.http.scaladsl.model._ + complete( + HttpResponse( + status = StatusCodes.Unauthorized, + headers = Nil, + entity = HttpEntity.Empty, + protocol = HttpProtocols.`HTTP/1.1` + )) + } } override def port = 51823 @@ -220,6 +231,7 @@ class BasicTests multipartTests() redirectTests() timeoutTests() + emptyResponseTests() def parseResponseTests(): Unit = { name should "parse response as string" in { @@ -664,6 +676,18 @@ class BasicTests request.send().force().unsafeBody should be("Done") } } + + def emptyResponseTests(): Unit = { + val postEmptyResponse = sttp + .post(uri"$endpoint/empty_unauthorized_response") + .body("{}") + .contentType("application/json") + + name should "parse an empty error response as empty string" in { + val response = postEmptyResponse.send().force() + response.body should be(Left("")) + } + } } override protected def afterAll(): Unit = { |