Permalink
Browse files

Integrated sample code tests into Play build

  • Loading branch information...
1 parent bcab9f2 commit 0e3956d8944a3bc2138a222f859f1053314dcca3 @jroper jroper committed Apr 4, 2013
@@ -16,37 +16,21 @@ The `play.api.mvc.Action` companion object offers several helper methods to cons
The first simplest one just takes as argument an expression block returning a `Result`:
-```scala
-Action {
- Ok("Hello world")
-}
-```
+@[zero-arg-action](code/ScalaActions.scala)
This is the simplest way to create an Action, but we don't get a reference to the incoming request. It is often useful to access the HTTP request calling this Action.
So there is another Action builder that takes as an argument a function `Request => Result`:
-```scala
-Action { request =>
- Ok("Got request [" + request + "]")
-}
-```
+@[request-action](code/ScalaActions.scala)
It is often useful to mark the `request` parameter as `implicit` so it can be implicitly used by other APIs that need it:
-```scala
-Action { implicit request =>
- Ok("Got request [" + request + "]")
-}
-```
+@[implicit-request-action](code/ScalaActions.scala)
The last way of creating an Action value is to specify an additional `BodyParser` argument:
-```scala
-Action(parse.json) { implicit request =>
- Ok("Got request [" + request + "]")
-}
-```
+@[json-parser-action](code/ScalaActions.scala)
Body parsers will be covered later in this manual. For now you just need to know that the other methods of creating Action values use a default **Any content body parser**.
@@ -56,63 +40,29 @@ A `Controller` is nothing more than a singleton object that generates `Action` v
The simplest use case for defining an action generator is a method with no parameters that returns an `Action` value :
-```scala
-package controllers
-
-import play.api.mvc._
-
-object Application extends Controller {
-
- def index = Action {
- Ok("It works!")
- }
-
-}
-```
+@[full-controller](code/ScalaActions.scala)
Of course, the action generator method can have parameters, and these parameters can be captured by the `Action` closure:
-```scala
-def hello(name: String) = Action {
- Ok("Hello " + name)
-}
-```
+@[parameter-action](code/ScalaActions.scala)
## Simple results
For now we are just interested in simple results: An HTTP result with a status code, a set of HTTP headers and a body to be sent to the web client.
These results are defined by `play.api.mvc.SimpleResult`:
-```scala
-def index = Action {
- SimpleResult(
- header = ResponseHeader(200, Map(CONTENT_TYPE -> "text/plain")),
- body = Enumerator("Hello world!")
- )
-}
-```
+@[simple-result-action](code/ScalaActions.scala)
Of course there are several helpers available to create common results such as the `Ok` result in the sample above:
-```scala
-def index = Action {
- Ok("Hello world!")
-}
-```
+@[ok-result-action](code/ScalaActions.scala)
This produces exactly the same result as before.
Here are several examples to create various results:
-```scala
-val ok = Ok("Hello world!")
-val notFound = NotFound
-val pageNotFound = NotFound(<h1>Page not found</h1>)
-val badRequest = BadRequest(views.html.form(formWithErrors))
-val oops = InternalServerError("Oops")
-val anyStatus = Status(488)("Strange response type")
-```
+@[other-results](code/ScalaActions.scala)
All of these helpers can be found in the `play.api.mvc.Results` trait and companion object.
@@ -122,26 +72,16 @@ Redirecting the browser to a new URL is just another kind of simple result. Howe
There are several helpers available to create redirect results:
-```scala
-def index = Action {
- Redirect("/user/home")
-}
-```
+@[redirect-action](code/ScalaActions.scala)
The default is to use a `303 SEE_OTHER` response type, but you can also set a more specific status code if you need one:
-```scala
-def index = Action {
- Redirect("/user/home", status = MOVED_PERMANENTLY)
-}
-```
+@[moved-permanently-action](code/ScalaActions.scala)
## "TODO" dummy page
You can use an empty `Action` implementation defined as `TODO`: the result is a standard ‘Not implemented yet’ result page:
-```scala
-def index(name:String) = TODO
-```
+@[todo-action](code/ScalaActions.scala)
> **Next:** [[HTTP Routing | ScalaRouting]]
@@ -1,23 +1,184 @@
-package http.scalaactions
+package scalaguide.http.scalaactions {
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
import org.specs2.mutable.Specification
+import play.api.libs.json._
+import play.api.libs.iteratee.Enumerator
object ScalaActionsSpec extends Specification with Controller {
"A scala action" should {
- "Allow writing a simple echo action" in {
+ "allow writing a simple echo action" in {
//#echo-action
val echo = Action { request =>
Ok("Got request [" + request + "]")
}
//#echo-action
- running(FakeApplication()) {
- status(echo(FakeRequest())) must_== 200
+ testAction(echo)
+ }
+
+ "support zero arg actions" in {
+ testAction(
+ //#zero-arg-action
+ Action {
+ Ok("Hello world")
+ }
+ //#zero-arg-action
+ )
+ }
+
+ "pass the request to the action" in {
+ testAction(
+ //#request-action
+ Action { request =>
+ Ok("Got request [" + request + "]")
+ }
+ //#request-action
+ )
+ }
+
+ "pass the request implicitly to the action" in {
+ testAction(
+ //#implicit-request-action
+ Action { implicit request =>
+ Ok("Got request [" + request + "]")
+ }
+ //#implicit-request-action
+ )
+ }
+
+ "allow specifying a parser" in {
+ testAction(action =
+ //#json-parser-action
+ Action(parse.json) { implicit request =>
+ Ok("Got request [" + request + "]")
+ }
+ //#json-parser-action
+ , request = FakeRequest().withBody(Json.obj()).withHeaders(CONTENT_TYPE -> "application/json")
+ )
+ }
+
+ "work for a full controller class" in {
+ testAction(full.Application.index)
+ }
+
+ "support an action with parameters" in {
+ //#parameter-action
+ def hello(name: String) = Action {
+ Ok("Hello " + name)
+ }
+ //#parameter-action
+
+ assertAction(hello("world")) { result =>
+ contentAsString(result) must_== "Hello world"
+ }
+ }
+
+ "support returning a simple result" in {
+ //#simple-result-action
+ def index = Action {
+ SimpleResult(
+ header = ResponseHeader(200, Map(CONTENT_TYPE -> "text/plain")),
+ body = Enumerator("Hello world!")
+ )
+ }
+ //#simple-result-action
+ assertAction(index) { result =>
+ contentAsString(result) must_== "Hello world!"
+ header(CONTENT_TYPE, result) must be some "text/plain"
+ }
+ }
+
+ "support ok helper" in {
+ //#ok-result-action
+ def index = Action {
+ Ok("Hello world!")
+ }
+ //#ok-result-action
+ testAction(index)
+ }
+
+ "support other result types" in {
+ val formWithErrors = ""
+
+ //#other-results
+ val ok = Ok("Hello world!")
+ val notFound = NotFound
+ val pageNotFound = NotFound(<h1>Page not found</h1>)
+ val badRequest = BadRequest(views.html.form(formWithErrors))
+ val oops = InternalServerError("Oops")
+ val anyStatus = Status(488)("Strange response type")
+ //#other-results
+
+ anyStatus.header.status must_== 488
+ }
+
+ "support redirects" in {
+ //#redirect-action
+ def index = Action {
+ Redirect("/user/home")
+ }
+ //#redirect-action
+ assertAction(index, expectedResponse = SEE_OTHER) { result =>
+ header(LOCATION, result) must be some "/user/home"
+ }
+ }
+
+ "support other redirects" in {
+ //#moved-permanently-action
+ def index = Action {
+ Redirect("/user/home", MOVED_PERMANENTLY)
+ }
+ //#moved-permanently-action
+ assertAction(index, expectedResponse = MOVED_PERMANENTLY) { result =>
+ header(LOCATION, result) must be some "/user/home"
}
}
+
+ "support todo actions" in {
+ //#todo-action
+ def index(name:String) = TODO
+ //#todo-action
+ testAction(index("foo"), expectedResponse = NOT_IMPLEMENTED)
+ }
+
+ }
+
+ def testAction[A](action: Action[A], expectedResponse: Int = OK, request: Request[A] = FakeRequest()) {
+ assertAction(action, expectedResponse, request) { result => }
+ }
+
+ def assertAction[A](action: Action[A], expectedResponse: Int = OK, request: Request[A] = FakeRequest())(assertions: Result => Unit) {
+ running(FakeApplication()) {
+ val result = action(request)
+ status(result) must_== expectedResponse
+ assertions(result)
+ }
+ }
+}
+
+// Faking a form view
+package views.html {
+ object form {
+ def apply(obj: AnyRef): String = ""
+ }
+}
+}
+
+package scalaguide.http.scalaactions.full {
+//#full-controller
+//###insert: package controllers
+
+import play.api.mvc._
+
+object Application extends Controller {
+
+ def index = Action {
+ Ok("It works!")
}
}
+//#full-controller
+}
@@ -100,9 +100,7 @@ object Tasks {
}
copyDist("framework")
copyDist("samples")
-
- // Documentation
- IO.copyDirectory(playBase / "documentation", dist / "documentation", true, false)
+ copyDist("documentation")
// Copy the core files
copyMaintainPerms(coreFiles map (f => f -> (dist / f.getName)))
View
@@ -66,6 +66,14 @@ $PLAY "$@" clean-all test
cd $CURRENT
echo "[info]"
+echo "[info] ---- RUNNING DOCUMENTATION TESTS"
+echo "[info]"
+
+cd ../documentation
+$PLAY "$@" clean-all test
+cd $CURRENT
+
+echo "[info]"
echo "[info] ---- TESTING SAMPLE APPLICATIONS"
echo "[info]"
Oops, something went wrong.

0 comments on commit 0e3956d

Please sign in to comment.