grails-plugins / grails-rest-client-builder

REST client plugin that uses Spring's RestTemplate
http://grails.org/plugin/rest-client-builder
Apache License 2.0
65 stars 32 forks source link

Feature Request: Allow turning off automatic redirect following #55

Open dustindclark opened 5 years ago

dustindclark commented 5 years ago

I know automatically following permanent redirects follows the spec, but I use RestBuilder extensively for integration testing. It would be nice to turn this off so that redirects can be properly tested.

My current workaround looks like this:

RestBuilder restBuilder = new RestBuilder()
restBuilder.restTemplate = new RestTemplate(new HttpRequestFactoryWithoutRedirectFollowing())

and here's the ClientHttpRequestFactory implementation:

class HttpRequestFactoryWithoutRedirectFollowing extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod)
        connection.setInstanceFollowRedirects(false)
    }
}

Instead, I think it would be cleaner to turn this off via configuration (similiar to other connection properties). For example:

RestBuilder restBuilder = new RestBuilder(followRedirects: false)