/akka-http-tests/src/test/scala/akka/http/scaladsl/server/TestServer.scala

https://github.com/bwmcadams/akka · Scala · 68 lines · 56 code · 9 blank · 3 comment · 1 complexity · 28b440584fef7770ba4fd91fa10619d0 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
  3. */
  4. package akka.http.scaladsl.server
  5. import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport
  6. import akka.http.scaladsl.server.directives.Credentials
  7. import akka.stream.scaladsl._
  8. import com.typesafe.config.{ ConfigFactory, Config }
  9. import akka.actor.ActorSystem
  10. import akka.stream.ActorMaterializer
  11. import akka.http.scaladsl.Http
  12. object TestServer extends App {
  13. val testConf: Config = ConfigFactory.parseString("""
  14. akka.loglevel = INFO
  15. akka.log-dead-letters = off
  16. akka.stream.materializer.debug.fuzzing-mode = off
  17. """)
  18. implicit val system = ActorSystem("ServerTest", testConf)
  19. import system.dispatcher
  20. implicit val materializer = ActorMaterializer()
  21. import ScalaXmlSupport._
  22. import Directives._
  23. def auth: AuthenticatorPF[String] = {
  24. case p @ Credentials.Provided(name) if p.verify(name + "-password") name
  25. }
  26. val bindingFuture = Http().bindAndHandle({
  27. get {
  28. path("") {
  29. complete(index)
  30. } ~
  31. path("secure") {
  32. authenticateBasicPF("My very secure site", auth) { user
  33. complete(<html><body>Hello <b>{ user }</b>. Access has been granted!</body></html>)
  34. }
  35. } ~
  36. path("ping") {
  37. complete("PONG!")
  38. } ~
  39. path("crash") {
  40. complete(sys.error("BOOM!"))
  41. }
  42. } ~ pathPrefix("inner")(getFromResourceDirectory("someDir"))
  43. }, interface = "localhost", port = 8080)
  44. println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
  45. Console.readLine()
  46. bindingFuture.flatMap(_.unbind()).onComplete(_ system.terminate())
  47. lazy val index =
  48. <html>
  49. <body>
  50. <h1>Say hello to <i>akka-http-core</i>!</h1>
  51. <p>Defined resources:</p>
  52. <ul>
  53. <li><a href="/ping">/ping</a></li>
  54. <li><a href="/secure">/secure</a> Use any username and '&lt;username&gt;-password' as credentials</li>
  55. <li><a href="/crash">/crash</a></li>
  56. </ul>
  57. </body>
  58. </html>
  59. }