diff --git a/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RequestFactoryLiveTest.java b/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RequestFactoryLiveTest.java new file mode 100644 index 0000000000..f9ca66f14d --- /dev/null +++ b/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RequestFactoryLiveTest.java @@ -0,0 +1,38 @@ +package com.baeldung.resttemplate.proxy; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.Proxy.Type; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +public class RequestFactoryLiveTest { + + RestTemplate restTemplate; + + @Before + public void setUp() { + Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("201.91.82.155", 3128)); + + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + requestFactory.setProxy(proxy); + + restTemplate = new RestTemplate(requestFactory); + } + + @Test + public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() { + ResponseEntity responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class); + + assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK))); + } +} diff --git a/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RestTemplateCustomizerLiveTest.java b/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RestTemplateCustomizerLiveTest.java new file mode 100644 index 0000000000..8f318b758b --- /dev/null +++ b/spring-resttemplate/src/test/java/com/baeldung/resttemplate/proxy/RestTemplateCustomizerLiveTest.java @@ -0,0 +1,55 @@ +package com.baeldung.resttemplate.proxy; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.DefaultProxyRoutePlanner; +import org.apache.http.protocol.HttpContext; +import org.junit.Before; +import org.junit.Test; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.boot.web.client.RestTemplateCustomizer; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +public class RestTemplateCustomizerLiveTest { + + RestTemplate restTemplate; + + @Before + public void setUp() { + restTemplate = new RestTemplateBuilder(new ProxyCustomizer()).build(); + } + + @Test + public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() { + ResponseEntity responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class); + + assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK))); + } + + private static class ProxyCustomizer implements RestTemplateCustomizer { + + @Override + public void customize(RestTemplate restTemplate) { + HttpHost proxy = new HttpHost("201.91.82.155", 3128); + HttpClient httpClient = HttpClientBuilder.create() + .setRoutePlanner(new DefaultProxyRoutePlanner(proxy) { + @Override + public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException { + return super.determineProxy(target, request, context); + } + }) + .build(); + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); + } + } +}