harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qi...@apache.org
Subject svn commit: r727252 - in /harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests: internal/net/www/protocol/http/HttpURLConnectionTest.java java/net/AuthenticatorTest.java
Date Wed, 17 Dec 2008 03:04:45 GMT
Author: qiuxx
Date: Tue Dec 16 19:04:44 2008
New Revision: 727252

URL: http://svn.apache.org/viewvc?rev=727252&view=rev
Log:
Apply for HARMONY-6049,( [classlib] [luni] Add some test cases for java.net.Authenticator
and org.apache.harmony.luni.tests.internal.net.www.protocol.http.HttpURLConnection)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/AuthenticatorTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java?rev=727252&r1=727251&r2=727252&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
Tue Dec 16 19:04:44 2008
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.Authenticator;
+import java.net.ConnectException;
 import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
@@ -33,6 +34,8 @@
 import java.net.URL;
 import java.security.Permission;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
@@ -294,6 +297,44 @@
     }
 
     /**
+     * @tests HttpURLConnection.getHeaderFields
+     */
+    public void test_getHeaderFields() throws Exception {
+        URL url = new URL("http://www.apache.org");
+        HttpURLConnection httpURLConnect = (HttpURLConnection) url
+                .openConnection();
+        assertEquals(200, httpURLConnect.getResponseCode());
+        assertEquals("OK", httpURLConnect.getResponseMessage());
+        Map headers = httpURLConnect.getHeaderFields();
+        // there should be at least 2 headers
+        assertTrue(headers.size() > 1);
+        List list = (List) headers.get("Content-Length");
+        if (list == null) {
+            list = (List) headers.get("content-length");
+        }
+        assertNotNull(list);
+        try {
+            headers.put("key", "value");
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+        try {
+            list.set(0, "value");
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+        
+        try {
+            httpURLConnect.setRequestProperty("key", "value");
+            fail("should throw IlegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+    }
+
+    /**
      * @tests org.apache.harmony.luni.internal.net.www.http.getOutputStream()
      */
     public void testGetOutputStream() throws Exception {
@@ -376,6 +417,38 @@
     }
 
     /**
+     * @tests HttpURLConnection.usingProxy
+     */
+    public void testUsingProxy2() throws Exception {
+        try {
+            System.setProperty("http.proxyHost", "www.apache.org");
+            URL url = new URL("http://www.apache.org");
+            HttpURLConnection urlConnect = (HttpURLConnection) url
+                    .openConnection();
+            urlConnect.getInputStream();
+            assertTrue(urlConnect.usingProxy());
+            
+            System.setProperty("http.proxyPort", "81");
+            url = new URL("http://www.apache.org");
+            urlConnect = (HttpURLConnection) url.openConnection();
+            urlConnect.getInputStream();
+            assertFalse(urlConnect.usingProxy());
+            
+            url = new URL("http://localhost");
+            urlConnect = (HttpURLConnection) url.openConnection();
+            try {
+                urlConnect.getInputStream();
+                fail("should throw ConnectException");
+            } catch (ConnectException e) {
+                // Expected
+            }
+            assertFalse(urlConnect.usingProxy());
+        } finally {
+            System.setProperties(null);
+        }
+    }
+
+    /**
      * Test checks if the proxy provided by proxy selector will be used for
      * connection to the server
      */

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/AuthenticatorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/AuthenticatorTest.java?rev=727252&r1=727251&r2=727252&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/AuthenticatorTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/AuthenticatorTest.java
Tue Dec 16 19:04:44 2008
@@ -16,14 +16,21 @@
 
 package org.apache.harmony.luni.tests.java.net;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.Authenticator;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
+import java.net.PasswordAuthentication;
+import java.net.ServerSocket;
+import java.net.Socket;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.net.Authenticator.RequestorType;
 
 import junit.framework.TestCase;
+import tests.support.Support_PortManager;
 
 public class AuthenticatorTest extends TestCase {
 
@@ -120,6 +127,69 @@
         assertNull(mock.getRequestorType());
     }
 
+    /**
+     * @tests java.net.Authenticator#setDefault(java.net.Authenticator)
+     */
+    public void test_setDefault() {
+        final int port = Support_PortManager.getNextPort();
+        final Object lock = new Object();
+        final int[] result = new int[1];
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    ServerSocket ss = null;
+                    synchronized (lock) {
+                        ss = new ServerSocket(port);
+                        lock.notifyAll();
+                    }
+                    Socket s = ss.accept();
+                    InputStream in = s.getInputStream();
+                    in.read(new byte[1024]);
+                    OutputStream out = s.getOutputStream();
+                    out
+                            .write("HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate:Basic realm=\"something\"\r\n\r\n"
+                                    .getBytes("ISO8859_1"));
+                    Thread.sleep(500);
+                    out.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+        synchronized (lock) {
+            t.start();
+            try {
+                lock.wait();
+            } catch (InterruptedException e) {
+                // ignored
+            }
+        }
+        Authenticator.setDefault(new Authenticator() {
+            protected PasswordAuthentication getPasswordAuthentication() {
+                synchronized (lock) {
+                    result[0] = 1;
+                }
+                return null;
+            }
+        });
+        Authenticator.setDefault(new Authenticator() {
+            protected PasswordAuthentication getPasswordAuthentication() {
+                synchronized (lock) {
+                    result[0] = 2;
+                }
+                return null;
+            }
+        });
+        try {
+            new URL("http://localhost:" + port).openStream();
+        } catch (IOException e) {
+            // ignored
+        }
+        synchronized (lock) {
+            assertEquals("wrong authenticator: " + result[0], 2, result[0]);
+        }
+    }
+
     /*
      * Mock Authernticator for test
      */



Mime
View raw message