Ensure withBasicAuth keeps error handler

Fix `TestRestTemplate` so that any custom `ErrorHandler` isn't lost
when calling `withBasicAuth`.

Fixes gh-7441
This commit is contained in:
Phillip Webb
2016-11-22 10:58:30 -08:00
parent 3423ca87b4
commit 1452d3cd51
2 changed files with 27 additions and 17 deletions

View File

@@ -916,14 +916,16 @@ public class TestRestTemplate {
*/
public TestRestTemplate withBasicAuth(String username, String password) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(getRestTemplate().getErrorHandler());
restTemplate.setMessageConverters(getRestTemplate().getMessageConverters());
restTemplate.setInterceptors(
removeBasicAuthInterceptorIfPresent(getRestTemplate().getInterceptors()));
restTemplate.setRequestFactory(getRestTemplate().getRequestFactory());
restTemplate.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
return new TestRestTemplate(restTemplate, username, password,
this.httpClientOptions);
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username,
password, this.httpClientOptions);
testRestTemplate.getRestTemplate()
.setErrorHandler(getRestTemplate().getErrorHandler());
return testRestTemplate;
}
private List<ClientHttpRequestInterceptor> removeBasicAuthInterceptorIfPresent(