Play 2 reverse routing, get route from controller method - scala

Using the Play 2.2.3 framework, given I have a route like this one in my routes file :
GET /event controllers.Event.events
How can I programatically get the "/event" knowing the method I am trying to reach, in this case 'controllers.Authentication.authenticate' ?
I need it for test purpose, because I would like to have better test waiting not for the route itself but for the controllers method really called. So maybe there is another solution than getting the route and test it this way like I do for now :
//Login with good password and login
val Some(loginResult) = route(FakeRequest(POST, "/login").withFormUrlEncodedBody(
("email", email)
, ("password", password)))
status(loginResult)(10.seconds) mustBe 303
redirectLocation(loginResult).get mustBe "/event"

You construct the reverse route in this way:
[full package name].routes.[controller].[method]
In your example:
controllers.routes.Authentication.authenticate
controllers.routes.Events.events
But say you broke your packages out like controllers.auth and controllers.content, then they would be:
controllers.auth.routes.Authentication.authenticate
controllers.content.routes.Events.events
The reverse routes return a Call, which contains an HTTP method and URI. In your tests you can construct a FakeRequest with a Call:
FakeRequest(controllers.routes.Authentication.authenticate)
And you can also use it to test the redirect URI:
val call: Call = controllers.routes.Events.events
redirectLocation(loginResult) must beSome(call.url)

Related

How to test redirect location with reverse routing?

How can I test if a controller redirects to a specific location using the reverse routing in play framework 2.3? I would like to do something like this:
"LoginController#authenticate" should{
"Redirect to index on success" in{
...
val result = loginControllerTest.authenticate.apply(request)
redirectLocation(result) must be(routes.Application.index)
}
routes.Application.index is a Call which holds a method (GET, POST, PUT, DELETE) and a url.
redirectLocation(result) returns an Option[String] (None if there is no redirect)
You would want something like:
redirectLocation(result) must beSome(routes.Application.index.url)
routes.Application.index.toString would do the same.

Play modules test & FakeApplication

I would like to know what's the best way to run specs2 tests on a PlayFramework module and be able to simulate it running.
My module contains some routes in a file named mymodule.routes
In my apps I integrate them by adding the following line in my routes file
-> /mymodule mymodule.Routes
This is the test from my module I try to run but returns a 404 error :
"test myroute" in {
running(FakeApplication()) {
await(WS.url("http://localhost:9000/mymodule/myroute").get).status must equalTo(OK)
}
}
FakeApplication does not really lunch a web process, so you cannot test using http access to localhost.
You have three options:
Testing the controller directly
Testing the router
Testing the whole app.
Testing the controller is done by directly calling your controller and checking the result, as suggested in play documentation, and providing a FakeRequest()
val result = controllers.Application.index("Bob")(FakeRequest())
Testing the router is done by calling routeAndCall with a FakeRequest argument, specifying the relative path:
val Some(result) = routeAndCall(FakeRequest(GET, "/Bob"))
Eventually, if you want to test your whole application, you need to start a TestServer:
"run in a server" in {
running(TestServer(3333)) {
await(WS.url("http://localhost:3333").get).status must equalTo(OK)
}
}
Your question says: "What is the best option?". The answer is: there is nothing such as a best option, there are different way of testing for different purpose. You should pick up the testing strategy which better fits your requirement. In this case, since you want to test the router, I suggest you try approach n.2

Routing based on query parameter in Play framework

My web application will be triggered from an external system. It will call one request path of my app, but uses different query parameters for different kinds of requests.
One of the parameters is the "action" that defines what is to be done. The rest of the params depend on the "action".
So I can get request params like these:
action=sayHello&user=Joe
action=newUser&name=Joe&address=xxx
action=resetPassword
...
I would like to be able to encode it similarly in the routes file for play so it does the query param based routing and as much of the validation of other parameters as possible.
What I have instead is one routing for all of these possibilities with plenty of optional parameters. The action processing it starts with a big pattern match to do dispatch and parameter validation.
Googling and checking SO just popped up plenty of samples where the params are encoded in the request path somehow, so multiple paths are routed to the same action, but I would like the opposite: one path routed to different actions.
One of my colleagues said we could have one "dispatcher" action that would just redirect based on the "action" parameter. It would be a bit more structured then the current solution, but it would not eliminate the long list of optional parameters which should be selectively passed to the next action, so I hope one knows an even better solution :-)
BTW the external system that calls my app is developed by another company and I have no influence on this design, so it's not an option to change the way how my app is triggered.
The single dispatcher action is probably the way to go, and you don't need to specify all of your optional parameters in the route. If action is always there then that's the only one you really need.
GET /someRoute controller.dispatcher(action: String)
Then in your action method you can access request.queryString to get any of the other optional parameters.
Note: I am NOT experienced Scala developer, so maybe presented snippets can be optimized... What's important for you they are valid and working.
So...
You don't need to declare every optional param in the routes file. It is great shortcut for type param's validation and best choice would be convince 'other company' to use API prepared by you... Anyway if you haven't such possibility you can also handle their requests as required.
In general: the dispatcher approach seems to be right in this place, fortunately you don't need to declare all optional params in the routes and pass it between actions/methods as they can be fetched directly from request. In PHP it can be compared to $_GET['action'] and in Java version of Play 2 controller - DynamicForm class - form().bindFromRequest.get("action").
Let's say that you have a route:
GET /dispatcher controllers.Application.dispatcher
In that case your dispatcher action (and additional methods) can look like:
def dispatcher = Action { implicit request =>
request.queryString.get("action").flatMap(_.headOption).getOrElse("invalid") match {
case "sayHello" => sayHelloMethod
case "newUser" => newUserMethod
case _ => BadRequest("Action not allowed!")
}
}
// http://localhost:9000/dispatcher?action=sayHello&name=John
def sayHelloMethod(implicit request: RequestHeader) = {
val name = request.queryString.get("name").flatMap(_.headOption).getOrElse("")
Ok("Hello " + name )
}
// http://localhost:9000/dispatcher?action=newUser&name=John+Doe&address=john#doe.com
def newUserMethod(implicit request: RequestHeader) = {
val name = request.queryString.get("name").flatMap(_.headOption).getOrElse("")
val address = request.queryString.get("address").flatMap(_.headOption).getOrElse("")
Ok("We are creating new user " + name + " with address " + address)
}
Of course you will need to validate incoming types and values 'manually', especially when actions will be operating on the DataBase, anyway biggest part of your problem you have resolved now.

Play Framework - Redirect with params

I am trying to figure out how to do a redirect within a controller action in Play (2.0) using Scala.
The redirect using
Redirect(routes.Application.index)
works just fine.
What I cannot figure out from the docs, API, or Google is how to add parameters to the call.
I am coming from Grails where this could be done easily as follows:
redirect action: "index", params: ["key": "value"]
.
The only way I have found is to call Redirect using a string url and a query string, which seems awkward.
Basically I would like to make use of Redirect(Call) somehow, but I do not how to create the Call object using reverse routing.
Am I missing something/not getting the concept in Play/Scala?
Thanks in Advance!
Ellou'
A route is just a function, so you can pass arguments as usual:
// Redirect to /hello/Bob
def helloBob = Action {
Redirect(routes.Application.hello("Bob"))
}
This snippet comes from http://www.playframework.org/documentation/2.0/ScalaRouting (at the bottom)
You can also avoid creating another function just for this in your controller. In your route config, you can simply add something like this:
GET /google #controllers.Default.redirect(to = "http://google.com")

Scalatra - how do we do an internal redirect / forward of request

I want to call another internal url from my scalatra 'controller'. I can't do a simple redirect, as there's some security settings that mean a user has access only to the first url.
Is there a way to do this?
get("/foo") {
servletContext.getRequestDispatcher("/bar").forward(request, response)
}
The get() method is defined as (similar to POST, et al):
def get(transformers : org.scalatra.RouteTransformer*)(action : => scala.Any) : org.scalatra.Route
Depends on what you mean by internal redirect, I presume you just want to execute another route's action. You have a few options of what you can do. This seems to be working for me:
val canonicalEndpoint = get("/first/route") {
//do things in here
}
Then you could subsequently do:
get("/second/route")( canonicalEndpoint.action )
And I think you would get your desired response.
I like saving the whole Route response of the get() as you may also want to use that with scalatra's url() function in routing.