Wednesday, 19 December 2012

Grails - Integration testing with the REST plugin using SSL (HTTPS)

I was writing a few integration tests in a Grails application which used the REST plugin to call out to an HTTPS (SSL) endpoint. In Config.groovy, we had this defined:

rest.https.truststore.path='truststore.jks'

rest.https.truststore.pass='asdf'

rest.https.keystore.path='keystore.jks'

rest.https.keystore.pass='asdf'

rest.https.cert.hostnameVerifier = 'ALLOW_ALL'

rest.https.sslSocketFactory.enforce = true



I knew for a fact that this works because when I run the test individual it is fine. However, when I run it via the command

grails test-app integration:



However, I keep getting errors while running the test.


Upon looking at the source of the plugin, I realized that the plugin is still using the ConfigurationHolder class for reading its configuration, which has been deprecated since version 2.0.0. It seems that the context inside the ConfigurationHolder object is not the same as what's in the grailApplication variable.

So to fix the issue, I had to declare the values declaratively (unfortunately).

   @Before
    public void setUp() {
        //need to override as the rest plugin isn't using the grailApplication variable.
        ConfigurationHolder.config.rest.https.truststore.path='truststore.jks'
        ConfigurationHolder.config.rest.https.truststore.pass='asdf'
        ConfigurationHolder.config.rest.https.keystore.path='keystore.jks'
        ConfigurationHolder.config.rest.https.keystore.pass='asdf'
        ConfigurationHolder.config.rest.https.cert.hostnameVerifier = 'ALLOW_ALL'
        ConfigurationHolder.config.rest.https.sslSocketFactory.enforce = true
        super.setUp()
    }





The ideal way to fix this is to monkey patch the rest plugin to use the grailsApplication. However I didn't want to deal with the headache of a custom patched plugin in our project.

No comments:

Post a Comment