I am currently having trouble with the TestRestTemplate. We are using special characters in our application as "äöüß" for example and our endpoints are secured with basic authentication. So if I set the default encoding for the StringHttpMessageConverter within the TestRestTemplate to UTF-8 (default is ISO_8859_1) the encoding is reset if I use the method
testRestTemplate.withBasicAuth(username, password)
The reason is that the method withBasicAuth(...)
is creating a new instance of TestRestTemplate and therefore all previous configurations are lost...
I tried to override the withbasicAuth(...)
method but keeping my configurations enforces me to create the instance completely again meaning I need two methods that literally create the same instance of a TestRestTemplate. One with the method overridden and one method in which I do not need the basic auth...
Comment From: dreis2211
@Captain-P-Goldfish Could you share an example/snippet? That generally helps the Spring-Boot team with reproducing.
I wonder if you're using a RestTemplateBuilder
to construct your TestRestTemplate
? E.g. something like this:
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
messageConverter.setDefaultCharset(StandardCharsets.UTF_8);
restTemplateBuilder = restTemplateBuilder.messageConverters(messageConverter);
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
Comment From: Captain-P-Goldfish
The base problem for us is the encoding in the StringHttpMessageConverter I produced the following method to generate a TestRestTemplate
private static TestRestTemplate createRestTemplate()
{
HttpClient httpClient = HttpClientBuilder.create()
.setSSLContext(getSslContext())
.setSSLHostnameVerifier((s, sslSession) -> true)
.setDefaultCookieStore(new BasicCookieStore())
.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setHttpClient(httpClient);
RestTemplateBuilder builder = new RestTemplateBuilder();
TestRestTemplate.HttpClientOption[] httpClientOptions = {TestRestTemplate.HttpClientOption.ENABLE_REDIRECTS};
TestRestTemplate testRestTemplate = new TestRestTemplate(builder, null, null, httpClientOptions)
{
@Override
public TestRestTemplate withBasicAuth(String username, String password)
{
TestRestTemplate authTemplate = super.withBasicAuth(UtilityMethods.encodeUrl(username),
UtilityMethods.encodeUrl(password));
{
authTemplate.getRestTemplate().getMessageConverters().clear();
authTemplate.getRestTemplate().setMessageConverters(this.getRestTemplate().getMessageConverters());
}
authTemplate.getRestTemplate().setRequestFactory(clientHttpRequestFactory);
return authTemplate;
}
};
testRestTemplate.getRestTemplate().setRequestFactory(clientHttpRequestFactory);
List<HttpMessageConverter<?>> converterList = testRestTemplate.getRestTemplate().getMessageConverters();
converterList.stream()
.filter(converter -> converter instanceof StringHttpMessageConverter)
.map(converter -> (StringHttpMessageConverter)converter)
.findAny()
.ifPresent(converter -> converter.setDefaultCharset(StandardCharsets.UTF_8));
return testRestTemplate;
}
please not that I have overridden the "withBasicauth" method and that I am overriding the messageconverters. The default encoding for the StringHttpMessageConverter is set to ISO and if the "withBasicAuth" is called the encoding (if-changed) is resetted to ISO
Comment From: dreis2211
May I ask what Spring-Boot version you're using, @Captain-P-Goldfish ? If I put your code to the test - e.g. like the following:
TestRestTemplate testRestTemplate = createRestTemplate();
testRestTemplate.getRestTemplate().getMessageConverters().forEach(httpMessageConverter -> {
if (httpMessageConverter instanceof StringHttpMessageConverter) {
System.out.println(((StringHttpMessageConverter) httpMessageConverter).getDefaultCharset());
}
});
TestRestTemplate basicAuthTemplate = testRestTemplate.withBasicAuth("test", "test");
basicAuthTemplate.getRestTemplate().getMessageConverters().forEach(httpMessageConverter -> {
if (httpMessageConverter instanceof StringHttpMessageConverter) {
System.out.println(((StringHttpMessageConverter) httpMessageConverter).getDefaultCharset());
}
});
I get these outputs (on the latest master though):
UTF-8
UTF-8
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: Captain-P-Goldfish
sorry, got sick and forgot about the issue in the meantime... we are using springboot 2.2.4
Comment From: wilkinsona
Thanks for the additional info, @Captain-P-Goldfish. Using the test from @dreis2211 and Spring Boot 2.2.4 I see UTF-8
as the encoding for both StringHttpMessageConverter
s.
If you'd like us to spend some more time investigating, please provide a minimal and complete sample that reproduces the problem. You can share such a sample with us by zipping it up and attaching it to this issue or by pushing it to a separate repository on GitHub.