-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented support for routing code samples
- Loading branch information
Showing
13 changed files
with
301 additions
and
168 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
documentation/manual/scalaGuide/main/http/code/ScalaRouting.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package scalaguide.http.routing | ||
|
||
import org.specs2.mutable.Specification | ||
import play.api.test.FakeRequest | ||
import play.api.mvc._ | ||
import play.api.test.Helpers._ | ||
import play.core.Router | ||
|
||
package controllers { | ||
|
||
object Client { | ||
def findById(id: Long) = Some("showing client " + id) | ||
} | ||
|
||
object Clients extends Controller { | ||
|
||
// #show-client-action | ||
def show(id: Long) = Action { | ||
Client.findById(id).map { client => | ||
Ok(views.html.Clients.display(client)) | ||
}.getOrElse(NotFound) | ||
} | ||
// #show-client-action | ||
|
||
def list() = Action(Ok("all clients")) | ||
} | ||
|
||
object Application extends Controller { | ||
def download(name: String) = Action(Ok("download " + name)) | ||
def homePage() = Action(Ok("home page")) | ||
|
||
def loadContentFromDatabase(page: String) = Some("showing page " + page) | ||
|
||
// #show-page-action | ||
def show(page: String) = Action { | ||
loadContentFromDatabase(page).map { htmlContent => | ||
Ok(htmlContent).as("text/html") | ||
}.getOrElse(NotFound) | ||
} | ||
// #show-page-action | ||
} | ||
|
||
object Items extends Controller { | ||
def show(id: Long) = Action(Ok("showing item " + id)) | ||
} | ||
|
||
object Api extends Controller { | ||
def list(version: Option[String]) = Action(Ok("version " + version)) | ||
} | ||
} | ||
|
||
package query { | ||
package object controllers { | ||
val Application = scalaguide.http.routing.controllers.Application | ||
} | ||
} | ||
|
||
package fixed { | ||
package object controllers { | ||
val Application = scalaguide.http.routing.controllers.Application | ||
} | ||
} | ||
|
||
package defaultvalue.controllers { | ||
object Clients extends Controller { | ||
def list(page: Int) = Action(Ok("clients page " + page)) | ||
} | ||
} | ||
|
||
object ScalaRoutingSpec extends Specification { | ||
"the scala router" should { | ||
"support simple routing with a long parameter" in { | ||
contentOf(FakeRequest("GET", "/clients/10")).trim must_== "showing client 10" | ||
} | ||
"support a static path" in { | ||
contentOf(FakeRequest("GET", "/clients/all")) must_== "all clients" | ||
} | ||
"support a path part that spans multiple segments" in { | ||
contentOf(FakeRequest("GET", "/files/foo/bar")) must_== "download foo/bar" | ||
} | ||
"support regex path parts" in { | ||
contentOf(FakeRequest("GET", "/items/20")) must_== "showing item 20" | ||
} | ||
"support parameterless actions" in { | ||
contentOf(FakeRequest("GET", "/")) must_== "home page" | ||
} | ||
"support passing parameters from the path" in { | ||
contentOf(FakeRequest("GET", "/foo")) must_== "showing page foo" | ||
} | ||
"support passing parameters from the query string" in { | ||
contentOf(FakeRequest("GET", "/?page=foo"), query.Routes) must_== "showing page foo" | ||
} | ||
"support fixed values for parameters" in { | ||
contentOf(FakeRequest("GET", "/foo"), fixed.Routes) must_== "showing page foo" | ||
contentOf(FakeRequest("GET", "/"), fixed.Routes) must_== "showing page home" | ||
} | ||
"support default values for parameters" in { | ||
contentOf(FakeRequest("GET", "/clients"), defaultvalue.Routes) must_== "clients page 1" | ||
contentOf(FakeRequest("GET", "/clients?page=2"), defaultvalue.Routes) must_== "clients page 2" | ||
} | ||
"support optional values for parameters" in { | ||
contentOf(FakeRequest("GET", "/api/list-all")) must_== "version None" | ||
contentOf(FakeRequest("GET", "/api/list-all?version=3.0")) must_== "version Some(3.0)" | ||
} | ||
|
||
} | ||
|
||
def contentOf(rh: RequestHeader, router: Router.Routes = Routes) = contentAsString(router.routes(rh) match { | ||
case e: EssentialAction => AsyncResult(e(rh).run) | ||
}) | ||
} |
4 changes: 4 additions & 0 deletions
4
documentation/manual/scalaGuide/main/http/code/scalaguide.http.routing.defaultvalue.routes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# #clients | ||
# Pagination links, like /clients?page=3 | ||
GET /clients controllers.Clients.list(page: Int ?= 1) | ||
# #clients |
5 changes: 5 additions & 0 deletions
5
documentation/manual/scalaGuide/main/http/code/scalaguide.http.routing.fixed.routes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# #page | ||
# Extract the page parameter from the path, or fix the value for / | ||
GET / controllers.Application.show(page = "home") | ||
GET /:page controllers.Application.show(page) | ||
# #page |
4 changes: 4 additions & 0 deletions
4
documentation/manual/scalaGuide/main/http/code/scalaguide.http.routing.query.routes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# #page | ||
# Extract the page parameter from the query string. | ||
GET / controllers.Application.show(page) | ||
# #page |
32 changes: 32 additions & 0 deletions
32
documentation/manual/scalaGuide/main/http/code/scalaguide.http.routing.routes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
# #static-path | ||
GET /clients/all controllers.Clients.list() | ||
# #static-path | ||
|
||
# #clients-show-comment | ||
# Display a client. | ||
# #clients-show ###skip | ||
GET /clients/:id controllers.Clients.show(id: Long) | ||
# #clients-show #clients-show-comment | ||
|
||
# #spanning-path | ||
GET /files/*name controllers.Application.download(name) | ||
# #spanning-path | ||
|
||
# #regex-path | ||
GET /items/$id<[0-9]+> controllers.Items.show(id: Long) | ||
# #regex-path | ||
|
||
# #home-page | ||
GET / controllers.Application.homePage() | ||
# #home-page | ||
|
||
# #page | ||
# Extract the page parameter from the path. | ||
GET /:page controllers.Application.show(page) | ||
# #page | ||
|
||
# #optional | ||
# The version parameter is optional. E.g. /api/list-all?version=3.0 | ||
GET /api/list-all controllers.Api.list(version: Option[String]) | ||
# #optional |
2 changes: 2 additions & 0 deletions
2
...manual/scalaGuide/main/http/code/scalaguide/http/routing/views/Clients/display.scala.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@(msg: String) | ||
@msg |
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
Oops, something went wrong.