Skip to content

Commit

Permalink
Merge pull request #1567 from softwaremill/empty-response-body
Browse files Browse the repository at this point in the history
Add status text resolver
  • Loading branch information
adamw authored Sep 16, 2022
2 parents eaeee8c + f490e70 commit 332b118
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/src/main/scala/sttp/client3/Response.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.client3

import sttp.model.{Header, HeaderNames, Headers, Method, RequestMetadata, ResponseMetadata, StatusCode, Uri}
import sttp.model.{Header, HeaderNames, Headers, Method, RequestMetadata, ResponseMetadata, StatusCode, StatusText, Uri}

import scala.collection.immutable.Seq

Expand Down Expand Up @@ -45,22 +45,26 @@ object Response {
* [[sttp.client3.testing.SttpBackendStub]] and partial matchers.
*/
def apply[T](body: T, code: StatusCode): Response[T] =
Response(body, code, "", Nil, Nil, ExampleGet)
Response(body, code, resolveStatusText(code), Nil, Nil, ExampleGet)

/** Convenience method to create a Response instance, mainly useful in tests using
* [[sttp.client3.testing.SttpBackendStub]] and partial matchers.
*/
def apply[T](body: T, code: StatusCode, statusText: String): Response[T] =
Response(body, code, statusText, Nil, Nil, ExampleGet)
Response(body, code, resolveStatusText(code, statusText), Nil, Nil, ExampleGet)

/** Convenience method to create a Response instance, mainly useful in tests using
* [[sttp.client3.testing.SttpBackendStub]] and partial matchers.
*/
def apply[T](body: T, code: StatusCode, statusText: String, headers: Seq[Header]): Response[T] =
Response(body, code, statusText, headers, Nil, ExampleGet)
Response(body, code, resolveStatusText(code, statusText), headers, Nil, ExampleGet)

/** Convenience method to create a Response instance, mainly useful in tests using
* [[sttp.client3.testing.SttpBackendStub]] and partial matchers.
*/
def ok[T](body: T): Response[T] = apply(body, StatusCode.Ok, "OK")
def ok[T](body: T): Response[T] = apply(body, StatusCode.Ok)

private def resolveStatusText(statusCode: StatusCode, provided: String = ""): String =
if (provided.isEmpty) StatusText.default(statusCode).getOrElse(provided)
else provided
}

0 comments on commit 332b118

Please sign in to comment.