Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to run some controllers in test mode only, for playframework

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

I need to mock some response when clicking by link. I would create controller and routs that would be available only when I run play test.

Is it possible?

like image 832
ses Avatar asked Jan 17 '23 23:01

ses


2 Answers

As Mike has indicated, the code from my Blog indeed shows you how to configure your route file, so that only certain routes are available in Dev or Prod mode. So...

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

However, this will indeed prevent these routes from working in Prod mode, but they will not exclude the controller from being accessed. The reason for this, is that more than likely you will have the following 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 would be the following URL

/application/noprod

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

  1. You could remove the catch-all route, so that there is no entry other than the specific routes you have set up. This does mean that you need to specify all your routes for all actions in your routes file.

  2. Secondly, you could check for play.mode.isDev() in your actions, and call badRequest() to prevent access. This would make this much more visible, but may be an unacceptable overhead in coding.

like image 65
Codemwnci Avatar answered Mar 03 '23 21:03

Codemwnci


Yes it possible. You can add code to your route file like:

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

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

like image 27
Mike Avatar answered Mar 03 '23 19:03

Mike