diff options
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala | 6 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 56 |
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"))) |