sourcedef extractHostPort:PartialFunction[HttpHeader,Int]={case h:`Host`=> h.port
}
val route =
headerValuePF(extractHostPort){ port =>
complete(s"The port was $port")}// tests:Get("/")~>Host("example.com",5043)~> route ~> check {
responseAs[String] shouldEqual "The port was 5043"}Get("/")~>Route.seal(route)~> check {
status shouldEqual NotFound
responseAs[String] shouldEqual "The requested resource could not be found."}
sourceimport static akka.http.javadsl.server.Directives.complete;importstatic akka.http.javadsl.server.Directives.headerValuePF;finalPartialFunction<HttpHeader,Integer> extractHostPort =newJavaPartialFunction<HttpHeader,Integer>(){@OverridepublicInteger apply(HttpHeader x,boolean isCheck)throwsException{if(x instanceofHost){if(isCheck){returnnull;}else{return((Host) x).port();}}else{throw noMatch();}}};finalRoute route = headerValuePF(extractHostPort, port ->
complete("The port was "+ port));// tests:
testRoute(route).run(HttpRequest.GET("/").addHeader(Host.create("example.com",5043))).assertEntity("The port was 5043");
testRoute(route).run(HttpRequest.GET("/")).assertStatusCode(StatusCodes.NOT_FOUND).assertEntity("The requested resource could not be found.");