aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorDean Povey <dpovey@skedulo.com>2017-11-29 16:57:51 +1000
committerDean Povey <dpovey@skedulo.com>2017-11-29 16:57:51 +1000
commit3b1b7d8389a503bd28a99d84fdbfd1fc9857a2e9 (patch)
tree141f4b680df7156747876911e74d82e16c625b9b /core
parent6b7edb9bec49b4070acda2b75a3d91ef68074547 (diff)
downloadsttp-3b1b7d8389a503bd28a99d84fdbfd1fc9857a2e9.tar.gz
sttp-3b1b7d8389a503bd28a99d84fdbfd1fc9857a2e9.tar.bz2
sttp-3b1b7d8389a503bd28a99d84fdbfd1fc9857a2e9.zip
When stubbing using with thenRespondWithCode, treat 2xx as a success and return Right(body)
Diffstat (limited to 'core')
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala6
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala56
2 files changed, 58 insertions, 4 deletions
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 9bd3120..fed2acc 100644
--- a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala
@@ -91,8 +91,10 @@ class SttpBackendStub[R[_], S] private (
def thenRespondServerError(): SttpBackendStub[R, S] =
thenRespondWithCode(500, "Internal server error")
def thenRespondWithCode(code: Int,
- msg: String = ""): SttpBackendStub[R, S] =
- thenRespond(Response[Nothing](Left(msg), code, Nil, Nil))
+ msg: String = ""): SttpBackendStub[R, S] = {
+ val body = if (code >= 200 && code < 300) Right(msg) else Left(msg)
+ thenRespond(Response(body, code, Nil, Nil))
+ }
def thenRespond[T](body: T): SttpBackendStub[R, S] =
thenRespond(Response[T](Right(body), 200, Nil, Nil))
def thenRespond[T](resp: => Response[T]): SttpBackendStub[R, S] = {
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 7e6f15f..1cab218 100644
--- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala
+++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala
@@ -31,7 +31,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
implicit val b = testingStub
val r = sttp.get(uri"http://example.org/a/b/c").send()
r.is200 should be(true)
- r.body should be('left)
+ r.body should be('right)
}
it should "use subsequent rules if the first doesn't match" in {
@@ -47,7 +47,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
implicit val b = testingStub
val r = sttp.get(uri"http://example.org/a/b/c?p=v").send()
r.is200 should be(true)
- r.body should be('left)
+ r.body should be('right)
}
it should "use the default response if no rule matches" in {
@@ -107,6 +107,58 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
result.body should be(Right(20))
}
+ it should "handle a 201 as a success" in {
+ implicit val s = SttpBackendStub(HttpURLConnectionBackend())
+ .whenAnyRequest
+ .thenRespondWithCode(201)
+
+ val result = sttp
+ .get(uri"http://example.org")
+ .send()
+
+ result.body should be(Right(""))
+
+ }
+
+ it should "handle a 300 as a failure" in {
+ implicit val s = SttpBackendStub(HttpURLConnectionBackend())
+ .whenAnyRequest
+ .thenRespondWithCode(300)
+
+ val result = sttp
+ .get(uri"http://example.org")
+ .send()
+
+ result.body should be(Left(""))
+
+ }
+
+ it should "handle a 400 as a failure" in {
+ implicit val s = SttpBackendStub(HttpURLConnectionBackend())
+ .whenAnyRequest
+ .thenRespondWithCode(400)
+
+ val result = sttp
+ .get(uri"http://example.org")
+ .send()
+
+ result.body should be(Left(""))
+
+ }
+
+ it should "handle a 500 as a failure" in {
+ implicit val s = SttpBackendStub(HttpURLConnectionBackend())
+ .whenAnyRequest
+ .thenRespondWithCode(500)
+
+ val result = sttp
+ .get(uri"http://example.org")
+ .send()
+
+ result.body should be(Left(""))
+
+ }
+
private val testingStubWithFallback = SttpBackendStub
.withFallback(testingStub)
.whenRequestMatches(_.uri.path.startsWith(List("c")))