Play 2 reverse routing, get route from controller

Using the Play 2.2.3 frame, given that I have a route similar to this in the routes file:

GET /event controllers.Event.events 

How can I programmatically get "/ event", knowing the method I'm trying to achieve, in this case "controllerlers.Authentication.authenticate"?

I need this for testing purposes, because I would like to have a better test that does not expect the route itself, but the method that really called. So maybe there is another solution, except to get the route and test it in the way I do 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" 
+6
source share
1 answer

You create a return route 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 like controllers.auth and controllers.content , then they will be:

  controllers.auth.routes.Authentication.authenticate controllers.content.routes.Events.events 

Return routes return a Call that contains the HTTP method and URI. In your tests, you can build FakeRequest with Call :

  FakeRequest(controllers.routes.Authentication.authenticate) 

And you can also use it to check the redirect URI:

  val call: Call = controllers.routes.Events.events redirectLocation(loginResult) must beSome(call.url) 
+25
source

Source: https://habr.com/ru/post/971334/


All Articles