How to test client-side Akka HTTP
<p>I've just started testing out the <a href="http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.1/scala/http/client-side/request-level.html#Future-Based_Variant" rel="noreferrer">Akka HTTP Request-Level Client-Side API (Future-Based)</a>. One thing I've been struggling to figure out is how to write a unit test for this. Is there a way to mock the response and have the future completed without having to actually do an HTTP request?</p>

<p>I was looking at the API and the testkit package, trying to see how I could use that, only to find in the docs that it actually says:</p>

<blockquote>
  <p><strong>akka-http-testkit</strong> A test harness and set of utilities for verifying server-side service implementations</p>
</blockquote>

<p>I was thinking something <code>TestServer</code> (kinda like the <code>TestSource</code> for Akka Streams) and use the server side routing DSL to create the expected response and somehow hook this up the the <code>Http</code> object.</p>

<p>Here is a simplified example of what the function does that I want to test:</p>

<pre><code>object S3Bucket {

  def sampleTextFile(uri: Uri)(
    implicit akkaSystem: ActorSystem,
    akkaMaterializer: ActorMaterializer
  ): Future[String] = {
    val request = Http().singleRequest(HttpRequest(uri = uri))
    request.map { response =&gt; Unmarshal(response.entity).to[String] }
  }
}
</code></pre>