0
            URL url = new URL("https://requested.host:55554/requested.data");
            HttpsURLConnection connection = (HttpsURLConnection) url
                    .openConnection(new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress("localhost", ss
                                    .getLocalPort())));
            connection.setDoOutput(true);
 
            // perform the interaction between the peers and check the results 
            SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss,
                    OK_CODE);
            checkConnectionStateParameters(connection, peerSocket);
        } finally { 
            // roll the properties back to system values 
            tearDownStoreProperties(); 
        } 
    } 
 
    /** 
     * Tests HTTPS connection process made through the proxy server. Proxy 
     * server needs authentication but client fails to authenticate 
     * (Authenticator was not set up in the system). 
     */ 
    public void testProxyAuthConnectionFailed() throws Throwable { 
        // setting up the properties pointing to the key/trust stores 
        setUpStoreProperties(); 
 
        try { 
            // create the SSLServerSocket which will be used by server side 
            ServerSocket ss = new ServerSocket(0);
 
            // create the HostnameVerifier to check that Hostname verification 
            // is done 
            TestHostnameVerifier hnv = new TestHostnameVerifier();
            HttpsURLConnection.setDefaultHostnameVerifier(hnv);
 
            // create HttpsURLConnection to be tested 
            URL url = new URL("https://requested.host:55555/requested.data");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection(new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress("localhost", ss
                                    .getLocalPort())));
 
            // perform the interaction between the peers and check the results 
            try { 
                doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE);
            } catch (IOException e) {
                // SSL Tunnelling failed 
                if (DO_LOG) {
                    System.out.println("Got expected IOException: "
                            + e.getMessage());
                } 
            } 
        } finally { 
            // roll the properties back to system values 
            tearDownStoreProperties(); 
        } 
    } 
 
    /** 
     * Tests the behaviour of HTTPS connection in case of unavailability of 
     * requested resource. 
     */ 
    public void testProxyConnection_Not_Found_Response() throws Throwable { 
        // setting up the properties pointing to the key/trust stores 
        setUpStoreProperties(); 
 
        try { 
            // create the SSLServerSocket which will be used by server side 
            ServerSocket ss = new ServerSocket(0);
 
            // create the HostnameVerifier to check that Hostname verification 
            // is done 
            TestHostnameVerifier hnv = new TestHostnameVerifier();
            HttpsURLConnection.setDefaultHostnameVerifier(hnv);
 
            // create HttpsURLConnection to be tested 
            URL url = new URL("https://localhost:" + ss.getLocalPort());
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection(new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress("localhost", ss
                                    .getLocalPort())));
 
            try { 
                doInteraction(connection, ss, NOT_FOUND_CODE); // NOT FOUND
                fail("Expected exception was not thrown.");
            } catch (FileNotFoundException e) {
                if (DO_LOG) {
                    System.out.println("Expected exception was thrown: "
                            + e.getMessage());
                } 
            } 
        } finally { 
            // roll the properties back to system values 
            tearDownStoreProperties(); 
        } 
    } 
 
    // --------------------------------------------------------------------- 
    // ------------------------ Staff Methods ------------------------------ 
    // --------------------------------------------------------------------- 
 
    /** 
     * Log the name of the test case to be executed. 
     */ 
    public void setUp() throws Exception { 
        if (DO_LOG) {
            System.out.println();
            System.out.println("------------------------");
            System.out.println("------ " + getName());
            System.out.println("------------------------");
        } 
    } 
 
    /** 
     * Checks the HttpsURLConnection getter's values and compares them with 
     * actual corresponding values of remote peer. 
     */ 
    public static void checkConnectionStateParameters( 
            HttpsURLConnection clientConnection, SSLSocket serverPeer)
            throws Exception { 
        SSLSession session = serverPeer.getSession();
 
        assertEquals(session.getCipherSuite(), clientConnection
                .getCipherSuite());
 
        assertEquals(session.getLocalPrincipal(), clientConnection
                .getPeerPrincipal());
 
        assertEquals(session.getPeerPrincipal(), clientConnection
                .getLocalPrincipal());
 
        Certificate[] serverCertificates = clientConnection
                .getServerCertificates();
        Certificate[] localCertificates = session.getLocalCertificates();
        assertTrue("Server certificates differ from expected", Arrays.equals(
                serverCertificates, localCertificates));
 
        localCertificates = clientConnection.getLocalCertificates();
        serverCertificates = session.getPeerCertificates();
        assertTrue("Local certificates differ from expected", Arrays.equals(
                serverCertificates, localCertificates));
    } 
 
Experience pair programming with AI  Get Codota for Java