Search in sources :

Example 91 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-integration by spring-projects.

the class HttpProxyScenarioTests method testHttpProxyScenario.

@Test
public void testHttpProxyScenario() throws Exception {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;
    ZoneId GMT = ZoneId.of("GMT");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MILLISECOND, 0);
    final long ifModifiedSince = c.getTimeInMillis();
    Instant instant = Instant.ofEpochMilli(ifModifiedSince);
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, GMT);
    String ifModifiedSinceValue = dateTimeFormatter.format(zonedDateTime);
    c.add(Calendar.DATE, -1);
    long ifUnmodifiedSince = c.getTimeInMillis();
    instant = Instant.ofEpochMilli(ifUnmodifiedSince);
    zonedDateTime = ZonedDateTime.ofInstant(instant, GMT);
    final String ifUnmodifiedSinceValue = dateTimeFormatter.format(zonedDateTime);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    request.setQueryString("foo=bar&FOO=BAR");
    request.addHeader("If-Modified-Since", ifModifiedSinceValue);
    request.addHeader("If-Unmodified-Since", ifUnmodifiedSinceValue);
    request.addHeader("Connection", "Keep-Alive");
    request.setContentType("text/plain");
    Object handler = this.handlerMapping.getHandler(request).getHandler();
    assertNotNull(handler);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RestTemplate template = Mockito.spy(new RestTemplate());
    final String contentDispositionValue = "attachment; filename=\"test.txt\"";
    Mockito.doAnswer(invocation -> {
        URI uri = invocation.getArgument(0);
        assertEquals(new URI("http://testServer/test?foo=bar&FOO=BAR"), uri);
        HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
        HttpHeaders httpHeaders = httpEntity.getHeaders();
        assertEquals(ifModifiedSince, httpHeaders.getIfModifiedSince());
        assertEquals(ifUnmodifiedSinceValue, httpHeaders.getFirst("If-Unmodified-Since"));
        assertEquals("Keep-Alive", httpHeaders.getFirst("Connection"));
        MultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<String, String>(httpHeaders);
        responseHeaders.set("Connection", "close");
        responseHeaders.set("Content-Disposition", contentDispositionValue);
        return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
    }).when(template).exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), (Class<?>) isNull());
    PropertyAccessor dfa = new DirectFieldAccessor(this.handler);
    dfa.setPropertyValue("restTemplate", template);
    RequestAttributes attributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(attributes);
    this.handlerAdapter.handle(request, response, handler);
    assertEquals(ifModifiedSinceValue, response.getHeaderValue("If-Modified-Since"));
    assertEquals(ifUnmodifiedSinceValue, response.getHeaderValue("If-Unmodified-Since"));
    assertEquals("close", response.getHeaderValue("Connection"));
    assertEquals(contentDispositionValue, response.getHeader("Content-Disposition"));
    assertEquals("text/plain", response.getContentType());
    Message<?> message = this.checkHeadersChannel.receive(2000);
    MessageHeaders headers = message.getHeaders();
    assertEquals(ifModifiedSince, headers.get("If-Modified-Since"));
    assertEquals(ifUnmodifiedSince, headers.get("If-Unmodified-Since"));
    RequestContextHolder.resetRequestAttributes();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URI(java.net.URI) ZonedDateTime(java.time.ZonedDateTime) MessageHeaders(org.springframework.messaging.MessageHeaders) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PropertyAccessor(org.springframework.beans.PropertyAccessor) ZoneId(java.time.ZoneId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Calendar(java.util.Calendar) Instant(java.time.Instant) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ResponseEntity(org.springframework.http.ResponseEntity) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) DateTimeFormatter(java.time.format.DateTimeFormatter) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 92 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-integration by spring-projects.

the class HttpProxyScenarioTests method testHttpMultipartProxyScenario.

@Test
public void testHttpMultipartProxyScenario() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/testmp");
    request.addHeader("Connection", "Keep-Alive");
    request.setContentType("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ");
    request.setContent("foo".getBytes());
    Object handler = this.handlerMapping.getHandler(request).getHandler();
    assertNotNull(handler);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RestTemplate template = Mockito.spy(new RestTemplate());
    Mockito.doAnswer(invocation -> {
        URI uri = invocation.getArgument(0);
        assertEquals(new URI("http://testServer/testmp"), uri);
        HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
        HttpHeaders httpHeaders = httpEntity.getHeaders();
        assertEquals("Keep-Alive", httpHeaders.getFirst("Connection"));
        assertEquals("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ", httpHeaders.getContentType().toString());
        HttpEntity<?> entity = (HttpEntity<?>) invocation.getArguments()[2];
        assertThat(entity.getBody(), instanceOf(byte[].class));
        assertEquals("foo", new String((byte[]) entity.getBody()));
        MultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<String, String>(httpHeaders);
        responseHeaders.set("Connection", "close");
        responseHeaders.set("Content-Type", "text/plain");
        return new ResponseEntity<Object>(responseHeaders, HttpStatus.OK);
    }).when(template).exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), (Class<?>) isNull());
    PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
    dfa.setPropertyValue("restTemplate", template);
    RequestAttributes attributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(attributes);
    this.handlerAdapter.handle(request, response, handler);
    assertEquals("close", response.getHeaderValue("Connection"));
    assertEquals("text/plain", response.getContentType());
    RequestContextHolder.resetRequestAttributes();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) PropertyAccessor(org.springframework.beans.PropertyAccessor) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) URI(java.net.URI) ResponseEntity(org.springframework.http.ResponseEntity) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 93 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-integration by spring-projects.

the class HttpOutboundChannelAdapterParserTests method restTemplateConfig.

@Test
public void restTemplateConfig() {
    RestTemplate restTemplate = TestUtils.getPropertyValue(this.restTemplateConfig, "handler.restTemplate", RestTemplate.class);
    assertEquals(customRestTemplate, restTemplate);
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 94 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-integration by spring-projects.

the class HttpOutboundChannelAdapterParserTests method minimalConfig.

@Test
public void minimalConfig() {
    DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(this.minimalConfig);
    RestTemplate restTemplate = TestUtils.getPropertyValue(this.minimalConfig, "handler.restTemplate", RestTemplate.class);
    assertNotSame(customRestTemplate, restTemplate);
    HttpRequestExecutingMessageHandler handler = (HttpRequestExecutingMessageHandler) endpointAccessor.getPropertyValue("handler");
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
    assertEquals(false, handlerAccessor.getPropertyValue("expectReply"));
    assertEquals(this.applicationContext.getBean("requests"), endpointAccessor.getPropertyValue("inputChannel"));
    assertNull(handlerAccessor.getPropertyValue("outputChannel"));
    DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
    ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory) templateAccessor.getPropertyValue("requestFactory");
    assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
    Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
    assertEquals("http://localhost/test1", uriExpression.getValue());
    assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
    assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
    assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) HttpRequestExecutingMessageHandler(org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 95 with RestTemplate

use of org.springframework.web.client.RestTemplate in project taskana by Taskana.

the class WorkbasketControllerIntTest method testGetAllWorkbaskets.

@Test
public void testGetAllWorkbaskets() {
    RestTemplate template = getRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<PagedResources<WorkbasketSummaryResource>> response = template.exchange("http://127.0.0.1:" + port + "/v1/workbaskets", HttpMethod.GET, request, new ParameterizedTypeReference<PagedResources<WorkbasketSummaryResource>>() {
    });
    assertNotNull(response.getBody().getLink(Link.REL_SELF));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PagedResources(org.springframework.hateoas.PagedResources) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)519 Test (org.junit.Test)135 Test (org.junit.jupiter.api.Test)78 HttpHeaders (org.springframework.http.HttpHeaders)77 HttpEntity (org.springframework.http.HttpEntity)76 URI (java.net.URI)73 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)45 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)44 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)40 IOException (java.io.IOException)36 Bean (org.springframework.context.annotation.Bean)35 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)32 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)27 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)27 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)22 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)22 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)22