Last active
August 29, 2015 14:00
-
-
Save j5ik2o/11223659 to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
} |
This file contains 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
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) | |
} |
This file contains 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
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" | |
} | |
} | |
} | |
} | |
} |
This file contains 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
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() | |
} |
This file contains 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
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() | |
} |
This file contains 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
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