rest-assured: Proxy not used when call https based service

Our proxy http://corporate.proxy:8080 should be used for all protocols.

Am I using the RestAssurred proxy incorrectly when making an HTTPS rest call or is this not currently supported?

NOTE: I’m testing against “https://jsonplaceholder.typicode.com” as its the only public https rest api I can find.

PreemptiveBasicAuthScheme auth = new PreemptiveBasicAuthScheme();
auth.setUserName("XXXX");
auth.setPassword("XXXX");

String url = "https://jsonplaceholder.typicode.com/posts";

Response r2 = RestAssured
        .given()            
        .header("Proxy-Authorization", auth.generateAuthToken())            
        .proxy("corporate.proxy", 8080)
        .get(url);

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15

Most upvoted comments

Whoohoo! Finally able to talk through the proxy via https. I don’t know if you will be able to adapt this for RestAssured but here is the solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

/**
 *
 * A complete Java class that shows how to open a URL, then read data (text) from that URL,
 * HttpURLConnection class (in combination with an InputStreamReader and BufferedReader).
 *
 * @author orginal code from alvin alexander, devdaily.com, all the https stuff came off the web
 *
 */
public class JavaHttpUrlConnectionReader
{
    public static String doAction() throws Exception
    {
        String url = "https://jsonplaceholder.typicode.com/posts";
        // if your url can contain weird characters you will want to 
        // encode it here, something like this:
        // myUrl = URLEncoder.encode(myUrl, "UTF-8");

        String results = doHttpUrlConnectionAction(url);
        System.out.println(results);
        return results;

    }

    /**
     * Returns the output from the given URL.
     * 
     * I tried to hide some of the ugliness of the exception-handling
     * in this method, and just return a high level Exception from here.
     * Modify this behavior as desired.
     * 
     * @param desiredUrl
     * @return
     * @throws Exception
     */
    private static String doHttpUrlConnectionAction(String desiredUrl) throws Exception
    {
        URL url = null;
        BufferedReader reader = null;
        StringBuilder stringBuilder;

        try
        {       
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);


            // create the HttpURLConnection
            url = new URL(desiredUrl);

            // Set proxy 
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname", 8080));

            HttpURLConnection connection;

            if (desiredUrl.startsWith("https")) {
                connection = (HttpsURLConnection) url.openConnection(proxy);
            } else {
                connection = (HttpURLConnection) url.openConnection(proxy);
            }

            // Set proxy authentication    
            String uname_pwd = "user" + ":" + "pasword";
            String authString = "Basic " + new sun.misc.BASE64Encoder().encode(uname_pwd.getBytes());
            connection.setRequestProperty("Proxy-Authorization", authString);

            //connection.setSSLSocketFactory(sc.getSocketFactory());
            //connection.setHostnameVerifier(allHostsValid);        

            // just want to do an HTTP GET here
            connection.setRequestMethod("GET");

            // uncomment this if you want to write output to this url
            //connection.setDoOutput(true);

            // give it 15 seconds to respond
            connection.setReadTimeout(15*1000);
            connection.connect();

            // read the output from the server
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            stringBuilder = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line + "\n");
            }
            return stringBuilder.toString();
        }
        catch (Exception e)
        {
            System.out.println(e.getClass().getName() + ": " + e.getMessage());
            throw e;
        }
        finally
        {
            // close the reader; this can throw an exception too, so
            // wrap it in another try/catch block.
            if (reader != null)
            {
                try
                {
                    reader.close();
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        }
    }
}