Make TestRestTemplate withBasicAuth fault tolerant

It's part of TestRestTemplate's contract that it's fault tolerant. This
commit aligns the behavior of `withBasicAuth` with that expectation.

Closes gh-15780
pull/16235/head
Madhura Bhave 6 years ago
parent 0872eb0dd9
commit 2cdc93cf19

@ -1034,11 +1034,8 @@ public class TestRestTemplate {
.messageConverters(getRestTemplate().getMessageConverters()) .messageConverters(getRestTemplate().getMessageConverters())
.interceptors(getRestTemplate().getInterceptors()) .interceptors(getRestTemplate().getInterceptors())
.uriTemplateHandler(getRestTemplate().getUriTemplateHandler()).build(); .uriTemplateHandler(getRestTemplate().getUriTemplateHandler()).build();
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username, return new TestRestTemplate(restTemplate, username, password,
password, this.httpClientOptions); this.httpClientOptions);
testRestTemplate.getRestTemplate()
.setErrorHandler(getRestTemplate().getErrorHandler());
return testRestTemplate;
} }
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })

@ -237,14 +237,15 @@ public class TestRestTemplateTests {
} }
@Test @Test
public void withBasicAuthDoesNotResetErrorHandler() { public void withBasicAuthShouldUseNoOpErrorHandler() throws Exception {
TestRestTemplate originalTemplate = new TestRestTemplate("foo", "bar"); TestRestTemplate originalTemplate = new TestRestTemplate("foo", "bar");
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class); ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
originalTemplate.getRestTemplate().setErrorHandler(errorHandler); originalTemplate.getRestTemplate().setErrorHandler(errorHandler);
TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user",
"password"); "password");
assertThat(basicAuthTemplate.getRestTemplate().getErrorHandler()) assertThat(basicAuthTemplate.getRestTemplate().getErrorHandler())
.isSameAs(errorHandler); .isInstanceOf(Class.forName(
"org.springframework.boot.test.web.client.TestRestTemplate$NoOpResponseErrorHandler"));
} }
@Test @Test

Loading…
Cancel
Save