|
|
|
|
@ -16,6 +16,7 @@
@@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.test.web.client; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
import java.lang.reflect.Modifier; |
|
|
|
|
import java.net.URI; |
|
|
|
|
@ -27,21 +28,33 @@ import org.junit.Test;
@@ -27,21 +28,33 @@ import org.junit.Test;
|
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory; |
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; |
|
|
|
|
import org.springframework.boot.web.client.RestTemplateBuilder; |
|
|
|
|
import org.springframework.core.ParameterizedTypeReference; |
|
|
|
|
import org.springframework.http.HttpEntity; |
|
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
|
import org.springframework.http.RequestEntity; |
|
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory; |
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor; |
|
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
|
|
|
|
import org.springframework.http.client.InterceptingClientHttpRequestFactory; |
|
|
|
|
import org.springframework.http.client.support.BasicAuthorizationInterceptor; |
|
|
|
|
import org.springframework.mock.http.client.MockClientHttpRequest; |
|
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse; |
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils; |
|
|
|
|
import org.springframework.util.ReflectionUtils; |
|
|
|
|
import org.springframework.util.ReflectionUtils.MethodCallback; |
|
|
|
|
import org.springframework.web.client.ResponseErrorHandler; |
|
|
|
|
import org.springframework.web.client.RestOperations; |
|
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
import org.springframework.web.util.DefaultUriTemplateHandler; |
|
|
|
|
import org.springframework.web.util.UriTemplateHandler; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
import static org.mockito.Matchers.any; |
|
|
|
|
import static org.mockito.Matchers.eq; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests for {@link TestRestTemplate}. |
|
|
|
|
@ -49,6 +62,7 @@ import static org.mockito.Mockito.mock;
@@ -49,6 +62,7 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
* @author Dave Syer |
|
|
|
|
* @author Phillip Webb |
|
|
|
|
* @author Stephane Nicoll |
|
|
|
|
* @author Andy Wilkinson |
|
|
|
|
*/ |
|
|
|
|
public class TestRestTemplateTests { |
|
|
|
|
|
|
|
|
|
@ -87,6 +101,8 @@ public class TestRestTemplateTests {
@@ -87,6 +101,8 @@ public class TestRestTemplateTests {
|
|
|
|
|
@Test |
|
|
|
|
public void restOperationsAreAvailable() throws Exception { |
|
|
|
|
RestTemplate delegate = mock(RestTemplate.class); |
|
|
|
|
given(delegate.getUriTemplateHandler()) |
|
|
|
|
.willReturn(new DefaultUriTemplateHandler()); |
|
|
|
|
final TestRestTemplate restTemplate = new TestRestTemplate(delegate); |
|
|
|
|
ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() { |
|
|
|
|
|
|
|
|
|
@ -131,6 +147,10 @@ public class TestRestTemplateTests {
@@ -131,6 +147,10 @@ public class TestRestTemplateTests {
|
|
|
|
|
if (Class.class.equals(type)) { |
|
|
|
|
return Object.class; |
|
|
|
|
} |
|
|
|
|
if (RequestEntity.class.equals(type)) { |
|
|
|
|
return new RequestEntity<>(HttpMethod.GET, |
|
|
|
|
new URI("http://localhost")); |
|
|
|
|
} |
|
|
|
|
return mock(type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -195,6 +215,231 @@ public class TestRestTemplateTests {
@@ -195,6 +215,231 @@ public class TestRestTemplateTests {
|
|
|
|
|
.isSameAs(errorHandler); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void deleteHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.delete(relativeUri); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void exchangeWithRequestEntityAndClassHandlesRelativeUris() |
|
|
|
|
throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.exchange( |
|
|
|
|
new RequestEntity<>(HttpMethod.GET, relativeUri), String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void exchangeWithRequestEntityAndParameterizedTypeReferenceHandlesRelativeUris() |
|
|
|
|
throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.exchange( |
|
|
|
|
new RequestEntity<>(HttpMethod.GET, relativeUri), |
|
|
|
|
new ParameterizedTypeReference<String>() { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void exchangeHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.exchange(relativeUri, HttpMethod.GET, |
|
|
|
|
new HttpEntity<byte[]>(new byte[0]), String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void exchangeWithParameterizedTypeReferenceHandlesRelativeUris() |
|
|
|
|
throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.exchange(relativeUri, HttpMethod.GET, |
|
|
|
|
new HttpEntity<byte[]>(new byte[0]), |
|
|
|
|
new ParameterizedTypeReference<String>() { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void executeHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.execute(relativeUri, HttpMethod.GET, null, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void getForEntityHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.getForEntity(relativeUri, String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void getForObjectHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.getForObject(relativeUri, String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void headForHeadersHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.headForHeaders(relativeUri); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void optionsForAllowHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.optionsForAllow(relativeUri); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void patchForObjectHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.patchForObject(relativeUri, "hello", String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void postForEntityHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.postForEntity(relativeUri, "hello", String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void postForLocationHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.postForLocation(relativeUri, "hello"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void postForObjectHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.postForObject(relativeUri, "hello", String.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void putHandlesRelativeUris() throws IOException { |
|
|
|
|
verifyRelativeUriHandling(new TestRestTemplateCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate, |
|
|
|
|
URI relativeUri) { |
|
|
|
|
testRestTemplate.put(relativeUri, "hello"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void verifyRelativeUriHandling(TestRestTemplateCallback callback) |
|
|
|
|
throws IOException { |
|
|
|
|
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class); |
|
|
|
|
MockClientHttpRequest request = new MockClientHttpRequest(); |
|
|
|
|
request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK)); |
|
|
|
|
URI relativeUri = URI.create("a/b/c.txt"); |
|
|
|
|
URI absoluteUri = URI.create("http://localhost:8080/" + relativeUri.toString()); |
|
|
|
|
given(requestFactory.createRequest(eq(absoluteUri), any())).willReturn(request); |
|
|
|
|
RestTemplate delegate = new RestTemplate(); |
|
|
|
|
TestRestTemplate template = new TestRestTemplate(delegate); |
|
|
|
|
delegate.setRequestFactory(requestFactory); |
|
|
|
|
UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class); |
|
|
|
|
given(uriTemplateHandler.expand(relativeUri.toString(), new Object[0])) |
|
|
|
|
.willReturn(absoluteUri); |
|
|
|
|
template.setUriTemplateHandler(uriTemplateHandler); |
|
|
|
|
callback.doWithTestRestTemplate(template, relativeUri); |
|
|
|
|
verify(requestFactory).createRequest(eq(absoluteUri), any()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void assertBasicAuthorizationInterceptorCredentials( |
|
|
|
|
TestRestTemplate testRestTemplate, String username, String password) { |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@ -211,4 +456,10 @@ public class TestRestTemplateTests {
@@ -211,4 +456,10 @@ public class TestRestTemplateTests {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static interface TestRestTemplateCallback { |
|
|
|
|
|
|
|
|
|
void doWithTestRestTemplate(TestRestTemplate testRestTemplate, URI relativeUri); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|