Skip to content

Commit

Permalink
Merge pull request #52 from dpovey/master
Browse files Browse the repository at this point in the history
#51 When stubbing using with thenRespondWithCode, treat 2xx as a success and return Right(body)
  • Loading branch information
adamw committed Nov 29, 2017
2 parents 6b7edb9 + 3b1b7d8 commit e427a50
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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")))
Expand Down

0 comments on commit e427a50

Please sign in to comment.