use of org.springframework.web.util.DefaultUriBuilderFactory in project spring-boot by spring-projects.
the class TestRestTemplateTests method restOperationsAreAvailable.
@Test
public void restOperationsAreAvailable() throws Exception {
RestTemplate delegate = mock(RestTemplate.class);
given(delegate.getUriTemplateHandler()).willReturn(new DefaultUriBuilderFactory());
final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class, method.getName(), method.getParameterTypes());
assertThat(equivalent).as("Method %s not found", method).isNotNull();
assertThat(Modifier.isPublic(equivalent.getModifiers())).as("Method %s should have been public", equivalent).isTrue();
try {
equivalent.invoke(restTemplate, mockArguments(method.getParameterTypes()));
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
Object[] arguments = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
arguments[i] = mockArgument(parameterTypes[i]);
}
return arguments;
}
@SuppressWarnings("rawtypes")
private Object mockArgument(Class<?> type) throws Exception {
if (String.class.equals(type)) {
return "String";
}
if (Object[].class.equals(type)) {
return new Object[0];
}
if (URI.class.equals(type)) {
return new URI("http://localhost");
}
if (HttpMethod.class.equals(type)) {
return HttpMethod.GET;
}
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);
}
}, new ReflectionUtils.MethodFilter() {
@Override
public boolean matches(Method method) {
return Modifier.isPublic(method.getModifiers());
}
});
}
use of org.springframework.web.util.DefaultUriBuilderFactory in project spring-framework by spring-projects.
the class RestTemplateTests method getForObjectWithCustomUriTemplateHandler.
@Test
public void getForObjectWithCustomUriTemplateHandler() throws Exception {
DefaultUriBuilderFactory uriTemplateHandler = new DefaultUriBuilderFactory();
template.setUriTemplateHandler(uriTemplateHandler);
URI expectedUri = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
given(requestFactory.createRequest(expectedUri, HttpMethod.GET)).willReturn(request);
given(request.getHeaders()).willReturn(new HttpHeaders());
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getHeaders()).willReturn(new HttpHeaders());
given(response.getBody()).willReturn(null);
Map<String, String> uriVariables = new HashMap<>(2);
uriVariables.put("hotel", "1");
uriVariables.put("publicpath", "pics/logo.png");
uriVariables.put("scale", "150x150");
String url = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}";
template.getForObject(url, String.class, uriVariables);
verify(response).close();
}
use of org.springframework.web.util.DefaultUriBuilderFactory in project spring-framework by spring-projects.
the class DefaultWebClientBuilder method initUriBuilderFactory.
private UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
return this.uriBuilderFactory;
}
DefaultUriBuilderFactory factory = this.baseUrl != null ? new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory();
factory.setDefaultUriVariables(this.defaultUriVariables);
return factory;
}
Aggregations