|
| 1 | +package tech.httptoolkit.testapp.cases; |
| 2 | + |
| 3 | +import akka.NotUsed; |
| 4 | +import akka.actor.ActorSystem; |
| 5 | +import akka.actor.ExtendedActorSystem; |
| 6 | +import akka.http.javadsl.HostConnectionPool; |
| 7 | +import akka.http.javadsl.Http; |
| 8 | +import akka.http.javadsl.model.HttpRequest; |
| 9 | +import akka.http.javadsl.model.HttpResponse; |
| 10 | +import akka.japi.Pair; |
| 11 | +import akka.stream.javadsl.Flow; |
| 12 | +import akka.stream.javadsl.Sink; |
| 13 | +import akka.stream.javadsl.Source; |
| 14 | +import scala.util.Try; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.ExecutionException; |
| 20 | + |
| 21 | +// Here we use a host-based persistent Akka connection pool, just to make sure that that case is covered too, in |
| 22 | +// addition to the request client case. |
| 23 | +public class AkkaHostClientCase extends ClientCase< |
| 24 | + Flow<Pair<HttpRequest, NotUsed>, Pair<Try<HttpResponse>, NotUsed>, HostConnectionPool> |
| 25 | +> { |
| 26 | + |
| 27 | + private static final ActorSystem system = ExtendedActorSystem.create(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public Flow<Pair<HttpRequest, NotUsed>, Pair<Try<HttpResponse>, NotUsed>, HostConnectionPool> newClient(String url) throws Exception { |
| 31 | + URI uri = new URI(url); |
| 32 | + return new Http((ExtendedActorSystem) system) |
| 33 | + .cachedHostConnectionPool(uri.getHost()); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public int test( |
| 38 | + String url, |
| 39 | + Flow<Pair<HttpRequest, NotUsed>, Pair<Try<HttpResponse>, NotUsed>, HostConnectionPool> clientFlow |
| 40 | + ) throws URISyntaxException, ExecutionException, InterruptedException { |
| 41 | + Source<Pair<HttpRequest, NotUsed>, NotUsed> requestSource = Source.single( |
| 42 | + new Pair<>(HttpRequest.create(url), null) |
| 43 | + ); |
| 44 | + |
| 45 | + CompletableFuture<Pair<Try<HttpResponse>, NotUsed>> responseFuture = requestSource |
| 46 | + .via(clientFlow) |
| 47 | + .runWith(Sink.head(), system) |
| 48 | + .toCompletableFuture(); |
| 49 | + |
| 50 | + HttpResponse response = responseFuture.get().first().get(); |
| 51 | + response.discardEntityBytes(system); |
| 52 | + return response.status().intValue(); |
| 53 | + } |
| 54 | +} |
0 commit comments