Created
May 29, 2018 15:28
-
-
Save chbatey/964b80adc2cd124fa4bf4624927b5be0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.batey.akka.http | |
import akka.actor.Scheduler | |
import akka.actor.testkit.typed.scaladsl.TestProbe | |
import akka.actor.typed.ActorRef | |
import akka.actor.typed.scaladsl.AskPattern._ | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.testkit.ScalatestRouteTest | |
import akka.util.Timeout | |
import info.batey.akka.http.RouteUnderTest.Ping | |
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec} | |
import scala.concurrent.duration._ | |
import scala.util.{Failure, Success} | |
object RouteUnderTest { | |
case class Ping(replyTo: ActorRef[String]) | |
// Your route under test, scheduler is only needed as ask is used | |
def route(someActor: ActorRef[Ping])(implicit scheduler: Scheduler, timeout: Timeout) = get { | |
path("ping") { | |
onComplete(someActor ? Ping) { | |
case Success(s) => complete(s) | |
case Failure(t) => complete(t.toString) | |
} | |
} | |
} | |
} | |
class TypedHttpSpec extends WordSpec with ScalatestRouteTest with Matchers with BeforeAndAfterAll { | |
import akka.actor.typed.scaladsl.adapter._ | |
implicit val typedSystem = system.toTyped | |
implicit val timeout = Timeout(500.milliseconds) | |
implicit val scheduler = system.scheduler | |
"The service" should { | |
"return a 'PONG!' response for GET requests to /ping" in { | |
val probe = TestProbe[Ping]() | |
val test = Get("/ping") ~> RouteUnderTest.route(probe.ref) | |
val ping = probe.expectMessageType[Ping] | |
ping.replyTo ! "PONG!" | |
test ~> check { | |
responseAs[String] shouldEqual "PONG!" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment