From da85f559573c9353185cb553290082c0bbdaf6d0 Mon Sep 17 00:00:00 2001 From: intracer Date: Tue, 9 Jan 2018 23:12:34 +0200 Subject: add status message to Response #59 --- .../main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala | 3 ++- .../main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala | 2 +- core/src/main/scala/com/softwaremill/sttp/Response.scala | 1 + .../main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala | 5 +++-- core/src/test/scala/com/softwaremill/sttp/RequestTests.scala | 1 + .../scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 4 ++-- 6 files changed, 10 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala b/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala index accd6b5..3b88850 100644 --- a/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala +++ b/core/src/main/scala/com/softwaremill/sttp/FollowRedirectsBackend.scala @@ -32,7 +32,8 @@ class FollowRedirectsBackend[R[_], S](delegate: SttpBackend[R, S]) response.header(LocationHeader).fold(responseMonad.unit(response)) { loc => if (redirects >= FollowRedirectsBackend.MaxRedirects) { - responseMonad.unit(Response(Left("Too many redirects"), 0, Nil, Nil)) + responseMonad.unit( + Response(Left("Too many redirects"), 0, "", Nil, Nil)) } else { followRedirect(request, response, redirects, loc) } diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala index 65b1a0c..113ce48 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionBackend.scala @@ -226,7 +226,7 @@ class HttpURLConnectionBackend private ( Left(readResponseBody(wrappedIs, asString, charsetFromHeaders)) } - Response(body, code, headers, Nil) + Response(body, code, c.getResponseMessage, headers, Nil) } private def readResponseBody[T](is: InputStream, diff --git a/core/src/main/scala/com/softwaremill/sttp/Response.scala b/core/src/main/scala/com/softwaremill/sttp/Response.scala index 4d9c385..d01645a 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Response.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Response.scala @@ -21,6 +21,7 @@ import scala.util.Try */ case class Response[T](body: Either[String, T], code: Int, + message: String, headers: Seq[(String, String)], history: List[Response[Unit]]) { def is200: Boolean = code == 200 diff --git a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala index 1d5b3c6..9b4e93e 100644 --- a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala +++ b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala @@ -73,6 +73,7 @@ class SttpBackendStub[R[_], S] private ( wrapResponse( Response[Nothing](Left("Not Found: " + request.uri), 404, + "Not Found", Nil, Nil)) case Some(fb) => fb.send(request) @@ -98,10 +99,10 @@ class SttpBackendStub[R[_], S] private ( def thenRespondWithCode(code: Int, msg: String = ""): SttpBackendStub[R, S] = { val body = if (code >= 200 && code < 300) Right(msg) else Left(msg) - thenRespond(Response(body, code, Nil, Nil)) + thenRespond(Response(body, code, msg, Nil, Nil)) } def thenRespond[T](body: T): SttpBackendStub[R, S] = - thenRespond(Response[T](Right(body), 200, Nil, Nil)) + thenRespond(Response[T](Right(body), 200, "OK", Nil, Nil)) def thenRespond[T](resp: => Response[T]): SttpBackendStub[R, S] = { val m: PartialFunction[Request[_, _], Response[_]] = { case r if p(r) => resp diff --git a/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala b/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala index e62112a..5fc9d50 100644 --- a/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala @@ -33,6 +33,7 @@ class RequestTests extends FlatSpec with Matchers { val response = Response(Right(()), 0, + "", List((SetCookieHeader, "k1=v1"), (SetCookieHeader, "k2=v2")), Nil) sttp diff --git a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala index c9b94c0..f3d2cd4 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -20,11 +20,11 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { case r if r.method == Method.POST && r.uri.path.endsWith( List("partial10")) => - Response(Right(10), 200, Nil, Nil) + Response(Right(10), 200, "OK", Nil, Nil) case r if r.method == Method.POST && r.uri.path.endsWith( List("partialAda")) => - Response(Right("Ada"), 200, Nil, Nil) + Response(Right("Ada"), 200, "OK", Nil, Nil) }) "backend stub" should "use the first rule if it matches" in { -- cgit v1.2.3