aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-11-13 15:39:31 +0100
committeradamw <adam@warski.org>2017-11-13 15:39:31 +0100
commitd7d26fbda8559adad2737c2df73076f002c194f9 (patch)
tree04ff4bfa0b9d0806c32f20033f6c8536cf47a260 /core/src/test/scala
parenta0fdfa6f6114aec289e46c4261522244b160e522 (diff)
downloadsttp-d7d26fbda8559adad2737c2df73076f002c194f9.tar.gz
sttp-d7d26fbda8559adad2737c2df73076f002c194f9.tar.bz2
sttp-d7d26fbda8559adad2737c2df73076f002c194f9.zip
#44: supporting type conversion in the backend stub
Diffstat (limited to 'core/src/test/scala')
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala46
1 files changed, 43 insertions, 3 deletions
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 292d324..7e6f15f 100644
--- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala
+++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala
@@ -1,15 +1,15 @@
package com.softwaremill.sttp.testing
+import java.io.ByteArrayInputStream
import java.util.concurrent.TimeoutException
import scala.concurrent.ExecutionContext.Implicits.global
-
import com.softwaremill.sttp._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{FlatSpec, Matchers}
class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
- val testingStub = SttpBackendStub(HttpURLConnectionBackend())
+ private val testingStub = SttpBackendStub(HttpURLConnectionBackend())
.whenRequestMatches(_.uri.path.startsWith(List("a", "b")))
.thenRespondOk()
.whenRequestMatches(_.uri.paramsMap.get("p").contains("v"))
@@ -93,7 +93,21 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
result.failed.futureValue shouldBe a[TimeoutException]
}
- val testingStubWithFallback = SttpBackendStub
+ it should "try to convert a basic response to a mapped one" in {
+ implicit val s = SttpBackendStub(HttpURLConnectionBackend())
+ .whenRequestMatches(_ => true)
+ .thenRespond("10")
+
+ val result = sttp
+ .get(uri"http://example.org")
+ .mapResponse(_.toInt)
+ .mapResponse(_ * 2)
+ .send()
+
+ result.body should be(Right(20))
+ }
+
+ private val testingStubWithFallback = SttpBackendStub
.withFallback(testingStub)
.whenRequestMatches(_.uri.path.startsWith(List("c")))
.thenRespond("ok")
@@ -111,4 +125,30 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures {
val r = sttp.post(uri"http://example.org/a/b").send()
r.is200 should be(true)
}
+
+ private val s = "Hello, world!"
+ private val adjustTestData = List[(Any, ResponseAs[_, _], Any)](
+ (s, IgnoreResponse, Some(())),
+ (s, ResponseAsString(Utf8), Some(s)),
+ (s.getBytes(Utf8), ResponseAsString(Utf8), Some(s)),
+ (new ByteArrayInputStream(s.getBytes(Utf8)),
+ ResponseAsString(Utf8),
+ Some(s)),
+ (10, ResponseAsString(Utf8), None),
+ ("10",
+ MappedResponseAs(ResponseAsString(Utf8), (_: String).toInt),
+ Some(10)),
+ (10, MappedResponseAs(ResponseAsString(Utf8), (_: String).toInt), None)
+ )
+
+ behavior of "tryAdjustResponseBody"
+
+ for {
+ (body, responseAs, expectedResult) <- adjustTestData
+ } {
+ it should s"adjust $body to $expectedResult when specified as $responseAs" in {
+ SttpBackendStub.tryAdjustResponseBody(responseAs, body) should be(
+ expectedResult)
+ }
+ }
}