Skip to content

Commit

Permalink
Merge pull request #60 from intracer/add_status_message
Browse files Browse the repository at this point in the history
add status message to Response #59
  • Loading branch information
adamw committed Jan 11, 2018
2 parents 200dc81 + 2a3c7c3 commit 7999c22
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AkkaHttpBackend private (
log = customLog.getOrElse(actorSystem.log)))
.flatMap { hr =>
val code = hr.status.intValue()
val statusText = hr.status.reason()

val headers = headersFromAkka(hr)
val charsetFromHeaders = headers
Expand All @@ -90,7 +91,7 @@ class AkkaHttpBackend private (
.map(Left(_))
}

body.map(Response(_, code, headers, Nil))
body.map(Response(_, code, statusText, headers, Nil))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient,
private def readResponseNoBody(response: AsyncResponse): Response[Unit] = {
Response(Right(()),
response.getStatusCode,
response.getStatusText,
response.getHeaders
.iterator()
.asScala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/com/softwaremill/sttp/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import scala.util.Try
*/
case class Response[T](body: Either[String, T],
code: Int,
statusText: String,
headers: Seq[(String, String)],
history: List[Response[Unit]]) {
def is200: Boolean = code == 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RequestTests extends FlatSpec with Matchers {
val response =
Response(Right(()),
0,
"",
List((SetCookieHeader, "k1=v1"), (SetCookieHeader, "k2=v2")),
Nil)
sttp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ abstract class OkHttpBackend[R[_], S](client: OkHttpClient,
.asScala
.flatMap(name => res.headers().values(name).asScala.map((name, _)))

responseMonad.map(body)(Response(_, res.code(), headers.toList, Nil))
responseMonad.map(body)(
Response(_, res.code(), res.message(), headers.toList, Nil))
}

private def responseHandler(res: OkHttpResponse) =
Expand Down

0 comments on commit 7999c22

Please sign in to comment.