/akka-http2-support/src/test/scala/akka/http/scaladsl/Http2BindingViaConfigSpec.scala

https://github.com/akka/akka-http · Scala · 69 lines · 48 code · 13 blank · 8 comment · 2 complexity · e9e84b084194b185fa9f87d1f1be7105 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package akka.http.scaladsl
  5. import akka.event.Logging
  6. import akka.http.impl.util.ExampleHttpContexts
  7. import akka.http.scaladsl.model._
  8. import akka.stream._
  9. import akka.testkit.{ AkkaSpec, TestProbe }
  10. import scala.concurrent.Future
  11. import scala.concurrent.duration.{ Duration, _ }
  12. class Http2BindingViaConfigSpec extends AkkaSpec("""
  13. akka.http.server.preview.enable-http2 = on
  14. akka.loglevel = DEBUG
  15. akka.log-dead-letters = off
  16. """) {
  17. implicit val mat = ActorMaterializer()
  18. import system.dispatcher
  19. var binding: Future[Http.ServerBinding] = _ // initialized atStartup
  20. val helloWorldHandler: HttpRequest => Future[HttpResponse] =
  21. _ => Future(HttpResponse(entity = "Hello!"))
  22. "akka.http.server.enable-http2" should {
  23. "bind using HTTP/2 with HttpsConnectionContext provided" in {
  24. // since we're in akka-http core here, the http2 support is not available on classpath,
  25. // so the setting + binding should fail
  26. var binding: Future[Http.ServerBinding] = null
  27. try {
  28. val p = TestProbe()
  29. system.eventStream.subscribe(p.ref, classOf[Logging.Debug])
  30. val connectionContext = ExampleHttpContexts.exampleServerContext
  31. binding =
  32. Http().newServerAt("localhost", 0)
  33. .enableHttps(connectionContext)
  34. .bind(helloWorldHandler)
  35. // TODO we currently don't verify it really bound as h2, since we don't have a client lib
  36. fishForDebugMessage(p, "Binding server using HTTP/2")
  37. } finally if (binding ne null) binding.map(_.unbind())
  38. }
  39. }
  40. "Http2Shadow" should {
  41. "have ShadowHttp2Ext type aligned with real Http2Ext" in {
  42. val ext: Http2Ext = Http2()
  43. // the Shadow is a structural type, so this assignment only works
  44. // if all the methods it requires exist on the real class
  45. val typed: akka.http.impl.engine.Http2Shadow.ShadowHttp2Ext = ext
  46. val _ = typed.bindAndHandleAsync _ // to avoid not-used warnings
  47. }
  48. }
  49. private def fishForDebugMessage(a: TestProbe, messagePrefix: String, max: Duration = 3.seconds): Unit = {
  50. a.fishForMessage(max, hint = "expected debug message part: " + messagePrefix) {
  51. case Logging.Debug(_, _, msg: String) if msg contains messagePrefix => true
  52. case _ => false
  53. }
  54. }
  55. }