Commit 269bb914 authored by Andy Wilkinson's avatar Andy Wilkinson

Add methods to RestTemplateBuilder for configuring interceptors

Closes gh-6701
parent 32d0021e
...@@ -72,6 +72,9 @@ public class RestTemplateBuilderTests { ...@@ -72,6 +72,9 @@ public class RestTemplateBuilderTests {
@Mock @Mock
private HttpMessageConverter<Object> messageConverter; private HttpMessageConverter<Object> messageConverter;
@Mock
private ClientHttpRequestInterceptor interceptor;
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
...@@ -203,6 +206,62 @@ public class RestTemplateBuilderTests { ...@@ -203,6 +206,62 @@ public class RestTemplateBuilderTests {
.hasSameSizeAs(new RestTemplate().getMessageConverters()); .hasSameSizeAs(new RestTemplate().getMessageConverters());
} }
@Test
public void interceptorsWhenInterceptorsAreNullShouldThrowException()
throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("interceptors must not be null");
this.builder.interceptors((ClientHttpRequestInterceptor[]) null);
}
@Test
public void interceptorsCollectionWhenInterceptorsAreNullShouldThrowException()
throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("interceptors must not be null");
this.builder.interceptors((Set<ClientHttpRequestInterceptor>) null);
}
@Test
public void interceptorsShouldApply() throws Exception {
RestTemplate template = this.builder.interceptors(this.interceptor).build();
assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}
@Test
public void interceptorsShouldReplaceExisting() throws Exception {
RestTemplate template = this.builder
.interceptors(mock(ClientHttpRequestInterceptor.class))
.interceptors(Collections.singleton(this.interceptor)).build();
assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}
@Test
public void additionalInterceptorsWhenInterceptorsAreNullShouldThrowException()
throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("interceptors must not be null");
this.builder.additionalInterceptors((ClientHttpRequestInterceptor[]) null);
}
@Test
public void additionalInterceptorsCollectionWhenInterceptorsAreNullShouldThrowException()
throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("interceptors must not be null");
this.builder.additionalInterceptors((Set<ClientHttpRequestInterceptor>) null);
}
@Test
public void additionalInterceptorsShouldAddToExisting() throws Exception {
ClientHttpRequestInterceptor interceptor = mock(
ClientHttpRequestInterceptor.class);
RestTemplate template = this.builder.interceptors(interceptor)
.additionalInterceptors(this.interceptor).build();
assertThat(template.getInterceptors()).containsOnly(interceptor,
this.interceptor);
}
@Test @Test
public void requestFactoryClassWhenFactoryIsNullShouldThrowException() public void requestFactoryClassWhenFactoryIsNullShouldThrowException()
throws Exception { throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment