Play! framework 1.2.5: How to test if response is secure?

357 views Asked by At

A test case for my contact formular page is to make sure it's always in a secure context respectively using SSL. Basically, all I want to know, is that I have a given request where request.secure = true;

The following response does not contain any information about this and its headers are empty:

@Test
public void shouldShowContactForm() {
    Response response = GET("/contact");
    // How can I ask the response, if the complete URL is in HTTPS?
}

Even if I explicitly set my own request, I cant see the right way to do this:

@Test
public void shouldShowContactFormInSSLContext() {
    Request request = newRequest();
    request.secure = true;
    Response response = GET(request, "/contact");
    // Is it now possible?
}

Is this even the right approach to test this or am I simply missing something important about the request/response?

1

There are 1 answers

2
rhmiller_ic On

For this question I think what I've done for my apps in the past is have a @before interceptor on all my controllers that looks like this.

     @Before
     static void checkSSL(){
         if(Play.mode.equals(Play.Mode.PROD)){
             if(!request.secure) {
                 Router.ActionDefinition cashTicketDefinition = Router.reverse(request.controller + "." + request.actionMethod);
                 cashTicketDefinition.absolute();
                 String url = cashTicketDefinition.url.replaceFirst( "http:", "https:");

                 redirect(url, true);
        }
    }
}