Search in sources :

Example 1 with RequestEntity

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());
        }
    });
}
Also used : Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) URI(java.net.URI) IOException(java.io.IOException) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) ReflectionUtils(org.springframework.util.ReflectionUtils) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 2 with RequestEntity

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());
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) RequestEntity(org.springframework.http.RequestEntity) Map(java.util.Map) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with RequestEntity

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();
}
Also used : RequestEntity(org.springframework.http.RequestEntity) URI(java.net.URI)

Example 4 with RequestEntity

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());
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) MediaType(org.springframework.http.MediaType) RequestEntity(org.springframework.http.RequestEntity) URI(java.net.URI) Test(org.junit.Test)

Example 5 with RequestEntity

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());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestEntity(org.springframework.http.RequestEntity) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Aggregations

RequestEntity (org.springframework.http.RequestEntity)13 Test (org.junit.Test)9 URI (java.net.URI)7 RestTemplate (org.springframework.web.client.RestTemplate)4 IOException (java.io.IOException)3 Map (java.util.Map)3 HttpHeaders (org.springframework.http.HttpHeaders)3 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)2 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultiValueMap (org.springframework.util.MultiValueMap)2 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 MockUp (mockit.MockUp)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1