How to test client-side Akka HTTP

前端 未结 3 1541
挽巷
挽巷 2021-02-07 09:55

I\'ve just started testing out the Akka HTTP Request-Level Client-Side API (Future-Based). One thing I\'ve been struggling to figure out is how to write a unit test for this. Is

3条回答
  •  醉梦人生
    2021-02-07 10:53

    I was hoping there might be a way to leverage some sort of test actor system but in the absence of that (or some other idiomatic way) I am probably going to do something like this:

    object S3Bucket {
    
      type HttpResponder = HttpRequest => Future[HttpResponse]
    
      def defaultResponder = Http().singleRequest(_)
    
      def sampleTextFile(uri: Uri)(
        implicit akkaSystem: ActorSystem,
        akkaMaterializer: ActorMaterializer,
        responder: HttpResponder = defaultResponder
      ): Future[String] = {
        val request = responder(HttpRequest(uri = uri))
        request.map { response => Unmarshal(response.entity).to[String] }
      }
    }
    

    Then in my test I can just provide a mock HttpResponder.

提交回复
热议问题