Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2 reverse routing, get route from controller method

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"
like image 939
sam Avatar asked Jun 25 '14 10:06

sam


1 Answers

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)
like image 94
Michael Zajac Avatar answered Nov 20 '22 04:11

Michael Zajac