Custom spray rejection handler not working properly in tests - scala

I am building some JSON HTTP services using spray and I am having some problems testing a RejectionHandler. If I start the application running the command sbt run and make the request, the RejectionHandler process the MalformedRequestContentRejection as expected but I am getting an IllegalArgumentException when running the tests even with the route sealed. In the other hand, the MethodRejection works fine. The JSON validation is done using require
The next example is based in the spray-template repository branch on_spray-can_1.3_scala-2.11 with a POST endpoint and the new tests. I've made a fork with the entire example here
Notice the use of a case clase for deserialize JSONs, the use of the require method for validation and the declaration of an implicit RejectionHandler.
package com.example
import akka.actor.Actor
import spray.routing._
import spray.http._
import StatusCodes._
import MediaTypes._
import spray.httpx.SprayJsonSupport._
class MyServiceActor extends Actor with MyService {
def actorRefFactory = context
def receive = runRoute(myRoute)
}
case class SomeReq(field: String) {
require(!field.isEmpty, "field can not be empty")
}
object SomeReq {
import spray.json.DefaultJsonProtocol._
implicit val newUserReqFormat = jsonFormat1(SomeReq.apply)
}
trait MyService extends HttpService {
implicit val myRejectionHandler = RejectionHandler {
case MethodRejection(supported) :: _ => complete(MethodNotAllowed, supported.value)
case MalformedRequestContentRejection(message, cause) :: _ => complete(BadRequest, "requirement failed: field can not be empty")
}
val myRoute =
pathEndOrSingleSlash {
post {
entity(as[SomeReq]) { req =>
{
complete(Created, req)
}
}
}
}
}
This is are the test implemented using spray-testkit. The last one expects a BadRequest but the test fails with an IllegarArgumentException.
package com.example
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import StatusCodes._
import spray.httpx.SprayJsonSupport._
class MyServiceSpec extends Specification with Specs2RouteTest with MyService {
def actorRefFactory = system
"MyService" should {
"leave GET requests to other paths unhandled" in {
Get("/kermit") ~> myRoute ~> check {
handled must beFalse
}
}
"return a MethodNotAllowed error for PUT requests to the root path" in {
Put() ~> sealRoute(myRoute) ~> check {
status should be(MethodNotAllowed)
responseAs[String] === "POST"
}
}
"return Created for POST requests to the root path" in {
Post("/", new SomeReq("text")) ~> myRoute ~> check {
status should be(Created)
responseAs[SomeReq] === new SomeReq("text")
}
}
/* Failed test. Throws IllegalArgumentException */
"return BadRequest for POST requests to the root path without field" in {
Post("/", new SomeReq("")) ~> sealRoute(myRoute) ~> check {
status should be(BadRequest)
responseAs[String] === "requirement failed: field can not be empty"
}
}
}
}
I am missing something?
Thanks in advance!

Your SomeReq class is being eagerly instantiated in the Post("/", new SomeReq("")) request builder and the require method is invoked as soon as the class is instantiated.
To get around this try using the following instead:
import spray.json.DefaultJsonProtocol._
Post("/", JsObject("field" → JsString(""))) ~> sealRoute(myRoute) ~> check {
status should be(BadRequest)
responseAs[String] === "requirement failed: field can not be empty"
}

Related

Scala Akka Route Test - ActorRefFactory context must be defined

Akka Http version: "10.0.11"
I have the following route to test:
private def getAll: Route = pathPrefix("_all") {
get {
complete((todoRegistryActor ? GetAllTodos).mapTo[Todos].map(todosToTodoDtos))
}
}
And I have the following test:
class TodoRouteSpec extends WordSpec with Matchers
with ScalatestRouteTest with RouteManager with BeforeAndAfterAll with TestKitBase with ImplicitSender {
override implicit val system: ActorSystem = ActorSystem("TodoRouteSpec")
override val executionContext: ExecutionContext = system.dispatcher
private val todoRegistryProbe = TestProbe()
override implicit val todoRegistryActor: ActorRef = todoRegistryProbe.ref
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"The service" should {
"return a list of todos for GET _all request" in {
Get("/api/todo/_all").~>(todoRoute)(TildeArrow.injectIntoRoute).~>(check {
//todoRegistryProbe.expectMsg(GetAllTodos)
responseAs[TodosDto] shouldEqual TodosDto(Seq.empty)
status should ===(StatusCodes.OK)
})
}
}
}
When running the following test, I receive the error:
An exception or error caused a run to abort: ActorRefFactory context must be defined
java.lang.IllegalArgumentException: ActorRefFactory context must be defined
I was looking what raised this error, but I can't find a solution.
Does somebody know what raises this error?
TildeArrow.injectIntoRoute has to be explicitly passed to get the test running. Found the solution here: How can I fix the missing implicit value for parameter ta: TildeArrow in a test spec. Maybe someone knows another solution?
Thanks in advance
Solution
class TodoRouteSpec extends WordSpec with Matchers with ScalatestRouteTest with TodoRoute {
private lazy val routes = todoRoute
private val todoRegistryProbe = TestProbe()
todoRegistryProbe.setAutoPilot((sender: ActorRef, _: Any) => {
sender ! Todos(Seq.empty)
TestActor.KeepRunning
})
override implicit val todoRegistryActor: ActorRef = todoRegistryProbe.ref
"TodoRoute" should {
"return a list of todos for GET /todo/_all request" in {
Get("/todo/_all").~>(routes)(TildeArrow.injectIntoRoute).~>(check {
todoRegistryProbe.expectMsg(GetAllTodos)
status should ===(StatusCodes.OK)
contentType should ===(ContentTypes.`application/json`)
entityAs[TodosDto] shouldEqual TodosDto(Seq.empty)
})
}
}
}
You don't need actor system to do route tests.
See example in the documentation.
class FullTestKitExampleSpec extends WordSpec with Matchers with ScalatestRouteTest {
val smallRoute =
get {
path("ping") {
complete("PONG!")
}
}
"The service" should {
"return a 'PONG!' response for GET requests to /ping" in {
// tests:
Get("/ping") ~> smallRoute ~> check {
responseAs[String] shouldEqual "PONG!"
}
}
}
}

How to test a custom directive / extract value from akka.http.scaladsl.server.Directive?

I have a custom directive with a function like the following that returns a Directive1[ValidatedParameters], where ValidatedParameters is just a simple case class:
class MyCustomDirective {
def validateParameters(...): Directive1[ValidatedParameters] = {
...
provide(ValidatedParameters(...))
}
}
I'm using it like this in my route:
myCustomDirective.validateParameters(top, skip, modifiedDate) {
(validatedParameters: ValidatedParameters) => {
However, I have a unit test where I'd basically like to call the above function and verify that ValidatedParameters is what I expect:
val actualResult: Directive1[ValidatedParameters] = new MyCustomDirective().validateParameters(...)
So actualResult is a Directive1[ValidatedParameters], is there a way I can get access to the ValidatedParameters case class within this directive from a unit test?
Here is a solution for writing a test case for the custom directive as mentioned here:
REQUEST ~> ROUTE ~> check {
ASSERTIONS
}
For a custom directive to get page and per_page query paramters:
case class Paginate(page: Option[String], perPage: Option[String])
trait PaginationDirective extends ParameterDirectives with RespondWithDirectives {
def withPagination: Directive1[Paginate] = {
parameters(
(
"page".as[String].?,
"per_page".as[String].?
)
).tmap { case (pageOpt, perPageOpt) => Paginate(pageOpt, perPageOpt) }
}
def logCurrentPage(currentPage: Int): Directive0 = {
mapResponse(response => {
logger.info(s"currentPage is $currentPage")
response
})
}
}
Test case for the above custom directive:
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}
class PaginationDirectiveTest extends WordSpec with Matchers with PaginationDirective with ScalatestRouteTest {
"paginate directive" should {
"return page and per page parameters" in {
Get("/?page=1&per_page=25") ~> withPagination(i => complete(i.toString)) ~> check {
responseAs[String] shouldEqual "[Paginate(Some(1), Some(25))]"
}
}
"logCurrentPage" should {
Get("/hello") ~> logCurrentPage(5)(complete("Dummy msg")) ~> check {
//validate test here.
}
}
}
I ended up with the following:
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.testkit.ScalatestRouteTest
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport._
import org.scalamock.scalatest.MockFactory
import org.scalatest.{BeforeAndAfter, Matchers}
class MyTest extends ScalatestRouteTest {
...
"validate(top=None, skip=None, modifiedDate=None)" should "pass validation" in {
myCustomDirective.validate(top, skip, modifiedDate) {
(validatedParameters: ValidatedParameters) => {
validatedParameters shouldEqual expectedResult
complete("") // Needed to pass compilation
}
}
}
}

Testing actor crash using TestActorRef

I am new to actors, I am learning, how to test actors using TestActorRef
My actor code:
package actors
import actors.GreetingActor.Hi
import akka.actor.Actor
object GreetingActor {
case class Hi()
}
class GreetingActor extends Actor {
var greeting = ""
override def receive: Receive = {
case Hi() =>
greeting = "Hi"
case _ =>
throw new IllegalArgumentException("not supported message")
}
override def postRestart(reason: Throwable) = {
println(s"actor is restarted because of ${reason.getMessage}")
}
}
I am sure that everything works as I want in this code, but I can't show it in test. Especially I can't show that one of the most important thing, that my actor crashed. The test is very simple and obvious I send message that is not Hi() and should track somehow that actor crashed with IllegalArgumentException. My current test code:
package actors
import actors.GreetingActor.Hi
import akka.actor.ActorSystem
import akka.testkit.{TestActorRef, TestKit}
import org.scalatest.{MustMatchers, WordSpecLike}
class GreetingActorTest extends TestKit(ActorSystem("testsystem")) with WordSpecLike
with MustMatchers with StopSystemAfterAll {
"A Hello Actor" must {
"change state when it receives a message, single threaded" in {
val greetingActor = TestActorRef[GreetingActor]
greetingActor ! Hi()
greetingActor.underlyingActor.greeting mustBe "Hi"
}
"throw exception when it received unknown message, single threaded" in {
val greetingActor = TestActorRef[GreetingActor]
greetingActor ! "hi"
//some code that checks that actor crashed
}
}
}
The question is how can I track in test that my actor crashed using TestActorRef? Appreciate any help.
Change your test to the following:
"throw exception when it received unknown message, single threaded" in {
assertThrows[IllegalArgumentException] {
val greetingActor = TestActorRef[GreetingActor]
greetingActor.receive("hi")
}
}
Per the actor docs, you need to use receive so the exception doesn't get swallowed:
http://doc.akka.io/docs/akka/current/scala/testing.html#The_Way_In-Between__Expecting_Exceptions

Testing Spray API with Akka Router

I'm working on a Spray API, with an Akka router to send the incoming messages on to a pool of actors for handling the logic. Now I want to write some tests for the API, but I'm struggling to find the right structure for the code. The API looks as follows at the moment:
import akka.actor.{ActorRef, ActorSystem, Props, Actor}
import akka.io.IO
import akka.routing.SmallestMailboxPool
import akka.util.Timeout
import akka.pattern.ask
import com.typesafe.config.ConfigFactory
import spray.json._
import spray.can.Http
import scala.concurrent.duration._
import spray.routing._
import spray.http._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Success
import scala.util.Failure
object implicits{
implicit val system = ActorSystem("ApiSystem")
implicit val timeout = Timeout(5.seconds)
implicit val conf = ConfigFactory.load()
// Custom case class for parsing JSON parameter.
case class Msg(key1:String, key2:String, key3:Int)
object JsonProtocol extends DefaultJsonProtocol {
implicit val msg = jsonFormat3(Msg)
}
case class PostMsg(msg:String)
case object PostSuccess
case class PostFailure(msg:String)
}
import implicits._
object MyApi extends App {
override def main(Args: Array[String]):Unit = {
// create and start our service actor
val service = system.actorOf(Props(new MyApiActor(system)), "MyApi-service")
IO(Http) ? Http.Bind(service, interface = conf.getString("http.host"), port = conf.getInt("http.port"))
}
}
class MyApiActor(system: ActorSystem) extends Actor with MyApiService {
// the HttpService trait defines only one abstract member, which
// connects the services environment to the enclosing actor or test
def actorRefFactory = context
// this actor only runs our route, but you could add
// other things here, like request stream processing
// or timeout handling
def receive = runRoute(myRoute)
}
// this trait defines our service behavior independently from the service actor
trait MyApiService extends HttpService {
import implicits.JsonProtocol._
var actorPool = system.actorOf(SmallestMailboxPool(conf.getInt("actor-number")).props(Props(new HandlingActor(conf))), "msgRouter")
val myRoute =
path("msg") {
post {
entity(as[String]) { obj =>
try{
// if this parsing succeeds, the posted msg satisfies the preconditions set.
obj.parseJson.convertTo[Msg]
} catch {
case e: DeserializationException => {
complete(HttpResponse(status=StatusCodes.BadRequest, entity="Invalid json provided."))
}
case e: Exception => {
complete(HttpResponse(status=StatusCodes.InternalServerError, entity="Unknown internal server error."))
}
}
onComplete(actorPool ? PostMsg(obj)) {
case Success(value) => complete(HttpResponse(status = StatusCodes.OK, entity = "Pushed Msg"))
case Failure(value) => complete(HttpResponse(status = StatusCodes.InternalServerError, entity = "Handling failed."))
}
}
}
}
}
What I would like to test is the response of the API to various HTTP messages (i.e. correct calls, incorrect calls etc.). The logic in the handling actor is simply to push the message to a Kafka bus, so I would like to "mock" this behaviour (i.e. be able to test the API response if this push succeeds and also what happens when this push fails).
The thing I'm struggling with most at the moment is how to setup the test. For now, I am setting up the API using the same commands as in the main method shown, but I need to specify a different actorPool, as I don't want any messages to actually be pushed. How should I best go about achieving such tests?
I am using Scalatest, with the Akka and Spray testkit. (plus possibly mockito for mocking if necessary)
I have few suggestions to make your testing easier:
Do not create the actor pool in your trait. Instead inject the ActorRef from the ActorPool using a def instead of a val in the route. Then it will be easier to inject your actorPool TestProbe() to test. For example (I have not tried/compiled this code):
class MyApiActor extends Actor with MyApiService {
// the HttpService trait defines only one abstract member, which
// connects the services environment to the enclosing actor or test
def actorRefFactory = context
val actorPool = context.actorOf(SmallestMailboxPool(conf.getInt("actor-number")).props(Props(new HandlingActor(conf))), "msgRouter")
// this actor only runs our route, but you could add
// other things here, like request stream processing
// or timeout handling
def receive = runRoute(myRoute(actorPool))
}
// this trait defines our service behavior independently from the service actor
trait MyApiService extends HttpService {
import implicits.JsonProtocol._
def myRoute(actorPool: ActorRef) =
path("msg") {
post {
entity(as[String]) { obj =>
try{
// if this parsing succeeds, the posted msg satisfies the preconditions set.
obj.parseJson.convertTo[Msg]
} catch {
case e: DeserializationException => {
complete(StatusCodes.BadRequest, "Invalid json provided.")
}
case e: Exception => {
complete(StatusCodes.InternalServerError, "Unknown internal server error.")
}
}
onComplete(actorPool ? PostMsg(obj)) {
case Success(value) => complete(StatusCodes.OK, "Pushed Msg")
case Failure(value) => complete(StatusCodes.InternalServerError, "Handling failed.")
}
}
}
}
}
Then the test can look like this:
class HttpListenerSpec extends WordSpecLike with Matchers with ScalatestRouteTest with MyApiService {
"An HttpListener" should {
"accept GET at /msg" in {
val actorPool = TestProbe()
(stuff for responding with TestProbe()...)
Get("/msg") ~> myRoute(actorPool.ref) ~> check {
status shouldBe OK
val response = responseAs[String]
assert(...)
}
}
}
}
Also, as a final suggestion. There are implicit conversions that integrate spray json and spray so you can do entity(as[Msg]). Look for the following:
import spray.httpx.marshalling._
import spray.httpx.unmarshalling._
import spray.httpx.SprayJsonSupport._
import MsgJsonProtocol._

Test HttpErrorHandler with scalates/specs2 in Play Framework 2.4.2

I have implemented my own HttpErrorHander in Play Framework 2.4.2 and it functions very well, but now I want to be able to test with "Fake Actions" that intentionally throw Exceptions. I have tried in scalatest and specs2
import play.api.http.HttpErrorHandler
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent._
class MyErrorHandler extends HttpErrorHandler {
def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
Future.successful(
Status(statusCode)("A client error occurred: " + message)
)
}
def onServerError(request: RequestHeader, exception: Throwable) = {
Future.successful(
InternalServerError("A server error occurred: " + exception.getMessage)
)
}
}
I tried so far the following tests. I try to debug the code, but I am never entering my methods. The methods of play.api.http.DefaultHttpErrorHandler are neither executed.
object ThrowableControllerSpec extends PlaySpecification with Results {
"Example Page" should {
"throwErrorAction should be valid" in {
val controller = new TestController()
val result: Future[Result] = controller.exceptionAction().apply(FakeRequest())
//val bodyText: String = contentAsString(result)
status(result) mustEqual INTERNAL_SERVER_ERROR
//bodyText must be startingWith "A server error occurred:"
}
}
}
The Action-method in TestController.exceptionAction looks:
def exceptionAction() = Action {
if (true)
throw new Exception("error")
else
Ok("")
}
The second try:
class ApplicationSpec extends Specification {
"Application" should {
"sent 500 on server error" in new WithApplication {
route(FakeRequest(GET, "/exception")) must beSome.which(status(_) == INTERNAL_SERVER_ERROR)
}
}
}
And the route for /exception
GET /exception controllers.TestController.exceptionAction
I also added in application.conf play.http.errorHandler. But as I said, this is working, but I am not able to test it. The test always fails with the Exception given in exceptionAction.
Thank you in advance
If you are using Specs2, try this
await(controller.exceptionAction()(FakeRequest())) must throwA[Throwable] // or any error you want to test against
See these links.
https://twitter.github.io/scala_school/specs.html
http://www.scalatest.org/user_guide/using_assertions (for ScalaTest)
http://www.scalatest.org/getting_started_with_fun_suite
Hope this helps!