Skip to content

Instantly share code, notes, and snippets.

@fokot
Created November 4, 2016 12:09
Show Gist options
  • Save fokot/3827d2f0fc2cb02b3c4f65d8344f2f18 to your computer and use it in GitHub Desktop.
Save fokot/3827d2f0fc2cb02b3c4f65d8344f2f18 to your computer and use it in GitHub Desktop.
Akka-http + specs2 routes testing
package com.raptorintelligence
import akka.http.specs2.RouteTestSpecs2
import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable.Specification
class ConnectotMicroserviceTest extends Specification with RouteTestSpecs2 {
"routes are tested" >> { implicit ee: ExecutionEnv =>
Get("/test") ~> ConnectorMicroservice.route ~> checkEntity must equalTo("this is test").await
}
}
package akka.http.specs2
import akka.actor.ActorSystem
import akka.event.LoggingAdapter
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.{RequestContext, RequestContextImpl, RouteResult}
import akka.http.scaladsl.settings.RoutingSettings
import akka.stream.ActorMaterializer
import akka.util.ByteString
import org.specs2.specification.core.{Fragments, SpecificationStructure}
import org.specs2.specification.create.FragmentsFactory
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
trait RouteTestSpecs2 extends RequestBuilding with SpecificationStructure with FragmentsFactory {
implicit val system = ActorSystem("Main")
implicit val materializer = ActorMaterializer()
override def map(fs: => Fragments) =
super
.map(fs)
.append({ println("hahaha"); fragmentFactory.step(system.terminate())} )
implicit class HttpRequestWithSendTo(req: HttpRequest) {
def context = new RequestContextImpl(req, null: LoggingAdapter, null: RoutingSettings)
def ~>[A](f: RequestContext => A) = f(context)
}
def checkEntity(res: Future[RouteResult]): Future[String] =
res.flatMap(
_.asInstanceOf[RouteResult.Complete]
.response
.entity
.dataBytes
.runFold(ByteString())(_ ++ _).map(_.utf8String)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment