Merge pull request #9382 from roqmarcelo/BAEL-3844

BAEL-3844 : Send request to a proxy using RestTemplate
This commit is contained in:
rpvilao 2020-06-04 14:41:36 +02:00 committed by GitHub
commit 4b4824228b
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,50 @@
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;
/**
* This class is used to test a request using {@link RestTemplate} with {@link Proxy}
* using a {@link SimpleClientHttpRequestFactory} as configuration.
* <br />
* <br />
*
* Before running the test we should change the <code>PROXY_SERVER_HOST</code>
* and <code>PROXY_SERVER_PORT</code> constants in our class to match our preferred proxy configuration.
*/
public class RequestFactoryLiveTest {
private static final String PROXY_SERVER_HOST = "127.0.0.1";
private static final int PROXY_SERVER_PORT = 8080;
RestTemplate restTemplate;
@Before
public void setUp() {
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(PROXY_SERVER_HOST, PROXY_SERVER_PORT));
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);
restTemplate = new RestTemplate(requestFactory);
}
@Test
public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
}
}

View File

@ -0,0 +1,69 @@
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.Proxy;
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;
/**
* This class is used to test a request using {@link RestTemplate} with {@link Proxy}
* using a {@link RestTemplateCustomizer} as configuration.
* <br />
* <br />
*
* Before running the test we should change the <code>PROXY_SERVER_HOST</code>
* and <code>PROXY_SERVER_PORT</code> constants in our class to match our preferred proxy configuration.
*/
public class RestTemplateCustomizerLiveTest {
private static final String PROXY_SERVER_HOST = "127.0.0.1";
private static final int PROXY_SERVER_PORT = 8080;
RestTemplate restTemplate;
@Before
public void setUp() {
restTemplate = new RestTemplateBuilder(new ProxyCustomizer()).build();
}
@Test
public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() {
ResponseEntity<String> 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(PROXY_SERVER_HOST, PROXY_SERVER_PORT);
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));
}
}
}