Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/11223659 to your computer and use it in GitHub Desktop.
Save j5ik2o/11223659 to your computer and use it in GitHub Desktop.
object Server extends App {
val service = new Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] =
Future.value(
new DefaultHttpResponse(
req.getProtocolVersion, HttpResponseStatus.OK
)
)
}
val server = Http.serve(":8080", service)
Await.ready(server)
}
object Server extends App {
class GetUser(userId: Int) extends Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] = /* */
}
class CreateUser(userId: Int) extends Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] = /* */
}
val routingService =
RoutingService.byRequest { request =>
(request.method, Path(request.path)) match {
case Method.Get -> Root / "api1" / Integer(userId) => new GetUser(userId)
case Method.Post -> Root / "api2" / Integer(userId) => new CreateUser(userId)
}
}
val server = Http.serve(":8080", routingService)
Await.ready(server)
}
class IntegrationTestSpec
extends Specification
with ControllerWithServerTestSupport {
val mainController = /* */
val routingFilter = /* */
val server = Server(filter = Some(routingFilter))
"integration-test" should {
"test the get method" in new WithServer(server) {
testGet("/api1/1") {
result =>
result must beSuccessfulTry.like {
case response =>
response.contentAsString() must_== "userId = 1"
}
}
}
}
}
object PlayLikeApplicationForController extends ConsoleApplication {
case class MainController() extends ControllerSupport {
def api1(userId: Int) = SimpleAction {
request =>
val userId = request.routeParams("userId")
responseBuilder.withTextPlain(s"api1: userId = $userId").toFuture
}
def api2(userId: Int) = SimpleAction {
request =>
val userId = request.routeParams("userId")
responseBuilder.withTextPlain(s"api2: userId = $userId").toFuture
}
}
val mainController = MainController()
override protected val routingFilter = Some(RoutingFilter.createForActions {
implicit pathPatternParser =>
Seq(
Get % "/api1/:userId" -> mainController.api1,
Post % "/api2/:userId" -> mainController.api2
)
})
startWithAwait()
}
object ScalatraLikeControllerApplication
extends ConsoleApplication {
get("/api1/:userId") {
request =>
val userId = request.routeParams("userId")
responseBuilder.withTextPlain(s"api1: userId = $userId").toFuture
}
post("/api2/:userId") {
request =>
val userId = request.routeParams("userId")
responseBuilder.withTextPlain(s"api2: userId = $userId").toFuture
}
startWithAwait()
}
class UnitTestSpec
extends Specification
with ControllerSimpleTestSupport {
val mainController = /* */
"unit-test" should {
"test the get method" in new WithTestScope {
override val routingFilter = /* */
testGet("/api1/1") {
result =>
result must beSuccessfulTry.like {
case response =>
response.contentAsString() must_== "userId = 1"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment