

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;


public class ProxyTest {
	private static Logger logger = LogManager.getLogger(ProxyTest.class);
	
	private static final String proxyhost="127.0.0.1";
	private static final int proxyport=8080;
	private static Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyhost, proxyport));
	
	private static String url1= "http://www.ebay.com/";
	private static String url2= "https://www.yahoo.com/";
	
	private String getContentFromHttpGetURL(HttpURLConnection con) throws IOException {
		con.setRequestMethod("GET");
        InputStream is = con.getInputStream();
        int code = con.getResponseCode();
		assertTrue(code == HttpURLConnection.HTTP_OK);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		String line = null;
		StringBuilder responseData = new StringBuilder();
		while((line = reader.readLine()) != null) {
		    responseData.append(line);
		}
        String str=responseData.toString();
        logger.info(String.format("Content is of length:%d", str.length()));
		is.close();
		con.disconnect();
		return str;
	}
	
	@Test
	public void test1() throws IOException {
		for (int i=0;i<2;i++){
			URL url = new URL(url1);
			HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
			String str = getContentFromHttpGetURL(con);
		}
	}
	
	
	@Test
	public void test2() throws IOException {
		for (int i=0;i<2;i++){
			URL url = new URL(url2);
			HttpsURLConnection con = (HttpsURLConnection) url.openConnection(proxy);
			String str = getContentFromHttpGetURL(con);
		}
	}
}
