Search in sources :

Example 11 with RequestEntity

use of org.springframework.http.RequestEntity in project spring-boot by spring-projects.

the class SampleSessionRedisApplicationTests method sessionExpiry.

@Test
public void sessionExpiry() throws Exception {
    String port = null;
    try {
        ConfigurableApplicationContext context = new SpringApplicationBuilder().sources(SampleSessionRedisApplication.class).properties("server.port:0").initializers(new ServerPortInfoApplicationContextInitializer()).run();
        port = context.getEnvironment().getProperty("local.server.port");
    } catch (RuntimeException ex) {
        if (!redisServerRunning(ex)) {
            return;
        }
        throw ex;
    }
    URI uri = URI.create("http://localhost:" + port + "/");
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
    String uuid1 = response.getBody();
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Cookie", response.getHeaders().getFirst("Set-Cookie"));
    RequestEntity<Void> request = new RequestEntity<>(requestHeaders, HttpMethod.GET, uri);
    String uuid2 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid1).isEqualTo(uuid2);
    Thread.sleep(5000);
    String uuid3 = restTemplate.exchange(request, String.class).getBody();
    assertThat(uuid2).isNotEqualTo(uuid3);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HttpHeaders(org.springframework.http.HttpHeaders) ServerPortInfoApplicationContextInitializer(org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) Test(org.junit.Test)

Example 12 with RequestEntity

use of org.springframework.http.RequestEntity in project spring-boot by spring-projects.

the class BasicErrorControllerIntegrationTests method testBindingExceptionForMachineClient.

@Test
@SuppressWarnings("rawtypes")
public void testBindingExceptionForMachineClient() throws Exception {
    load();
    RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind"))).accept(MediaType.APPLICATION_JSON).build();
    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("org.springframework.validation.BindException");
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) RequestEntity(org.springframework.http.RequestEntity) Map(java.util.Map) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 13 with RequestEntity

use of org.springframework.http.RequestEntity in project java-chassis by ServiceComb.

the class TestRestTemplateWrapper method testRestTemplateWrapper.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRestTemplateWrapper() {
    try {
        MockUtil.getInstance().mockConsumerProviderManager();
        MockUtil.getInstance().mockRegisterManager();
        MockUtil.getInstance().mockServicePathManager();
        MockUtil.getInstance().mockOperationLocator();
        MockUtil.getInstance().mockOperationMeta();
        MockUtil.getInstance().mockSchemaMeta();
        MockUtil.getInstance().mockSchemaUtils();
        MockUtil.getInstance().mockBeanUtils();
        MockUtil.getInstance().mockRequestMeta();
        MockUtil.getInstance().mockInvokerUtils();
        RestTemplateWrapper lRestTemplateWrapper = (RestTemplateWrapper) RestTemplateBuilder.create();
        lRestTemplateWrapper.put(URI.create("cse://test"), new Object());
        lRestTemplateWrapper.delete(URI.create("cse://test"));
        lRestTemplateWrapper.put("cse://test", new Object());
        lRestTemplateWrapper.getForObject("cse://test", new Object().getClass());
        lRestTemplateWrapper.getForEntity("cse://test", new Object().getClass());
        lRestTemplateWrapper.postForObject("cse://test", new Object(), new Object().getClass());
        lRestTemplateWrapper.postForEntity("cse://test", new Object(), new Object().getClass());
        lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
        lRestTemplateWrapper.delete("cse://test", new Object());
        lRestTemplateWrapper.delete("cse://test", new HashMap<>());
        Map<String, ?> uriVariables = new HashMap<>();
        lRestTemplateWrapper.getForObject("cse://test", new Object().getClass(), uriVariables);
        lRestTemplateWrapper.getForEntity("cse://test", new Object().getClass(), uriVariables);
        lRestTemplateWrapper.getForObject(URI.create("cse://test"), new Object().getClass());
        lRestTemplateWrapper.getForEntity(URI.create("cse://test"), new Object().getClass());
        lRestTemplateWrapper.postForObject("cse://test", new Object(), new Object().getClass(), uriVariables);
        lRestTemplateWrapper.postForObject(URI.create("cse://test"), new Object(), new Object().getClass());
        lRestTemplateWrapper.postForEntity("cse://test", new Object(), new Object().getClass(), uriVariables);
        lRestTemplateWrapper.postForEntity(URI.create("cse://test"), new Object(), new Object().getClass());
        lRestTemplateWrapper.put("cse://test", new Object(), uriVariables);
        lRestTemplateWrapper.put(URI.create("cse://test"), new Object());
        lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass(), uriVariables);
        ParameterizedTypeReference instance = Mockito.mock(ParameterizedTypeReference.class);
        lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, instance, uriVariables);
        lRestTemplateWrapper.exchange("cse://test", HttpMethod.GET, HttpEntity.EMPTY, instance, new Object().getClass());
        RequestEntity requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create("cse://test"));
        lRestTemplateWrapper.exchange(requestEntity, new Object().getClass());
        lRestTemplateWrapper.exchange(requestEntity, instance);
        lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, new Object().getClass());
        lRestTemplateWrapper.exchange(URI.create("cse://test"), HttpMethod.GET, HttpEntity.EMPTY, instance);
        Assert.assertTrue(RestTemplateBuilder.create().getClass().isAssignableFrom(RestTemplateWrapper.class));
    } catch (Exception e) {
        Assert.assertNotNull(e);
    }
}
Also used : HashMap(java.util.HashMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) RequestEntity(org.springframework.http.RequestEntity) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) 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