use of org.springframework.http.RequestEntity 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.http.RequestEntity in project spring-boot by spring-projects.
the class BasicErrorControllerIntegrationTests method testRequestBodyValidationForMachineClient.
@Test
@SuppressWarnings("rawtypes")
public void testRequestBodyValidationForMachineClient() throws Exception {
load();
RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation"))).contentType(MediaType.APPLICATION_JSON).body("{}");
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
String resp = entity.getBody().toString();
assertThat(resp).contains("Error count: 1");
assertThat(resp).contains("errors=[{");
assertThat(resp).contains("codes=[");
assertThat(resp).contains(MethodArgumentNotValidException.class.getName());
}
use of org.springframework.http.RequestEntity in project spring-framework by spring-projects.
the class AbstractRequestMappingIntegrationTests method prepareOptions.
private RequestEntity<Void> prepareOptions(String url, HttpHeaders headers) throws Exception {
URI uri = new URI("http://localhost:" + this.port + url);
RequestEntity.HeadersBuilder<?> builder = options(uri);
addHeaders(builder, headers);
return builder.build();
}
use of org.springframework.http.RequestEntity in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorMockTests method shouldResolveRequestEntityArgument.
@Test
public void shouldResolveRequestEntityArgument() throws Exception {
String body = "Foo";
MediaType contentType = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Content-Type", contentType.toString());
servletRequest.setMethod("GET");
servletRequest.setServerName("www.example.com");
servletRequest.setServerPort(80);
servletRequest.setRequestURI("/path");
servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8));
given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true);
given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
Object result = processor.resolveArgument(paramRequestEntity, mavContainer, webRequest, null);
assertTrue(result instanceof RequestEntity);
assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
RequestEntity<?> requestEntity = (RequestEntity<?>) result;
assertEquals("Invalid method", HttpMethod.GET, requestEntity.getMethod());
// using default port (which is 80), so do not need to append the port (-1 means ignore)
assertEquals("Invalid url", new URI("http", null, "www.example.com", -1, "/path", null, null), requestEntity.getUrl());
assertEquals("Invalid argument", body, requestEntity.getBody());
}
use of org.springframework.http.RequestEntity in project spring-security-oauth by spring-projects.
the class ClientCredentialsProviderTests method testHardCodedAuthenticationWrongClient.
@Test
public void testHardCodedAuthenticationWrongClient() {
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("grant_type", "client_credentials");
params.add("client_id", "my-trusted-client");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RequestEntity<MultiValueMap<String, String>> req = new RequestEntity<MultiValueMap<String, String>>(params, headers, HttpMethod.POST, tokenUri);
try {
restTemplate.exchange(req, Map.class);
fail("Expected HTTP 401");
} catch (HttpStatusCodeException e) {
assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
}
}
Aggregations