首页 > 解决方案 > 使用 SttpBackendStub 进行单元测试流请求

问题描述

我有一个返回要测试的源流的请求,如下所示:

/* class */
import sttp.client._

class HttpClient()
                (implicit sttpBackend: SttpBackend[Future, Source[ByteString, Any], Nothing]) {
  /** Start a download stream for large files */
  def downloadStream(url: String): Future[Response[Either[String, Source[ByteString, _]]]] = {
    request
      .get(uri"url")
      .response(asStream[Source[ByteString, Any]])
      .send()
  }
}
/* test */
import org.scalatest.FlatSpec
import org.scalatest.Matchers

class HttpClientSpec extends FlatSpec with Matchers {
  implicit val as = ActorSystem()
  implicit val mat = ActorMaterializer()
  implicit val ex = as

  "downloadStream" should "get a file stream from url" in {
    val url = s"http://example.com/api/v1/file/Test.txt"
    val body = "abcdefghijklmnopqrstuzwxyz"
    val expectedStream = Source.single[ByteString](body.getBytes)

    implicit val stubBackend = SttpBackendStub.asynchronousFuture
      .whenRequestMatches(req => req.method.method == "GET" && req.uri == uri"$url")
      .thenRespond(
        Response(Right(expectedStream), StatusCode.Ok)
      )

    val httpTest = new HttpClient()

    val response = Await.result(httpTest.downloadStream(url), 5.seconds)
    response.body.right.get should be(expectedStream)
  }
}

但它失败了,因为SttpBackendStub.asynchronousFuture 不处理流响应

type mismatch;
[error]  found   : sttp.client.testing.SttpBackendStub[scala.concurrent.Future,Nothing]
[error]  required: sttp.client.SttpBackend[scala.concurrent.Future,akka.stream.scaladsl.Source[akka.util.ByteString,Any],Nothing]

我怎样才能使用 SttpBackendStub 而不求助于类似的东西scalamock

标签: scalaakka-streamsttp

解决方案


只需要创建您自己的 Stub 类,extends即预期的响应类型:

class StreamHttpStub
    extends SttpBackendStub[Future, Source[ByteString, Any]](new FutureMonad(), PartialFunction.empty, None) {
}

implicit val stubBackend = new StreamHttpStub()
  .whenRequestMatches(req => req.method.method == "GET" && req.uri == uri"$url")
  .thenRespond(
    Response(Right(expectedStream), StatusCode.Ok)
  )

推荐阅读