To run some controllers only in test mode, for playframework

Is it possible to run several controllers and routes only in test mode?

I need to make fun of some answers when I click on the link. I would create a controller and routes that would be available only when the play test starts.

Is it possible?

+4
source share
4 answers

As Mike pointed out, the code from my blog really shows you how to set up a route file so that only certain routes are available in Dev or Prod mode. So that...

%{ if (play.mode.isDev()) { }% GET /route4 Application.noProd GET /route5 Application.noProd GET /route6 Application.noProd %{ } }% 

However, this will really prevent these routes from working in Prod mode, but they will not exclude access to the controller. The reason for this is that it is more likely that the following will be indicated at the bottom of your routes file:

 * /{controller}/{action} {controller}.{action} 

This means that I can access the Application.noProd action using the catch all route, which will be the following URL

 /application/noprod 

So. If you want to hide your routes and controllers, you have several options.

  • You can delete the entire route so that there is no entry other than the routes you have defined. This means that you need to specify all routes for all actions in your routes file.

  • Secondly, you can check play.mode.isDev() in your actions and call badRequest() to prevent access. This would make it much more noticeable, but may be an unacceptable coding overhead.

+5
source

Yes it is possible. You can add the code to the route file, for example:

 %{ if (play.mode.isDev()) { }% GET /route4 Application.noProd GET /route5 Application.noProd GET /route6 Application.noProd %{ } }% 

Everything is explained on this site: http://playframework.wordpress.com/2011/07/15/hidden-features-of-the-play-framework-routes-file/

+2
source

I just installed it like this:

 %{ if (play.id.equals("test")) { }% GET /testme Test.index %{ } }% 

therefore, the route will exist only if you run the “test test”, which is more specific.

In addition, I set my control test to .. / test / controllers /, so it will not exist in production mode. Somehow, I had to put my views /Test/index.html inside ... / app because it could not be found inside ... / test, which is strange because the controller is under ... / test. Perhaps I could make a problem for this, but I will not, because Play2 has been around for a long time.

0
source

Another way to hide test controllers and views is to move them to the test directory. Just create the controller package in the test folder, for example:

 public class TestController extends Controller { public static void show() { renderTemplate("/test/views/Test/show.html"); } } 

This way you can save your shared route, and your TestController will never be open to the public in another mode, and then test .

0
source

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


All Articles