You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2010/02/21 15:01:46 UTC

svn commit: r912351 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java

Author: hindessm
Date: Sun Feb 21 14:01:46 2010
New Revision: 912351

URL: http://svn.apache.org/viewvc?rev=912351&view=rev
Log:
Add regression tests and fix remaining issues from "[#HARMONY-6452]
HttpUrlConnection converts request headers to lowercase ...".

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

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java?rev=912351&r1=912350&r2=912351&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java Sun Feb 21 14:01:46 2010
@@ -19,6 +19,8 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.SortedMap;
+import java.util.TreeMap;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -31,12 +33,12 @@
  */
 public class Header implements Cloneable {
     /*
-     * we use the non-synchronized ArrayList and HashMap instead of the
+     * we use the non-synchronized ArrayList and TreehMap instead of the
      * synchronized Vector and Hashtable
      */
     private ArrayList<String> props;
 
-    private HashMap<String, LinkedList<String>> keyTable;
+    private SortedMap<String, LinkedList<String>> keyTable;
 
     private String statusLine;
 
@@ -48,7 +50,8 @@
     public Header() {
         super();
         this.props = new ArrayList<String>(20);
-        this.keyTable = new HashMap<String, LinkedList<String>>(20);
+        this.keyTable = new TreeMap<String, LinkedList<String>>(
+                                String.CASE_INSENSITIVE_ORDER);
     }
 
     /**
@@ -79,7 +82,8 @@
         try {
             Header clone = (Header) super.clone();
             clone.props = (ArrayList<String>) props.clone();
-            clone.keyTable = new HashMap<String, LinkedList<String>>(20);
+            clone.keyTable = new TreeMap<String, LinkedList<String>>(
+                                     String.CASE_INSENSITIVE_ORDER);
             for (Map.Entry<String, LinkedList<String>> next : this.keyTable
                     .entrySet()) {
                 LinkedList<String> v = (LinkedList<String>) next.getValue()
@@ -105,7 +109,7 @@
         LinkedList<String> list = keyTable.get(key);
         if (list == null) {
             list = new LinkedList<String>();
-            keyTable.put(key.toLowerCase(), list);
+            keyTable.put(key, list);
         }
         list.add(value);
         props.add(key);
@@ -197,7 +201,7 @@
      *         such key exists.
      */
     public String get(String key) {
-        LinkedList<String> result = keyTable.get(key.toLowerCase());
+        LinkedList<String> result = keyTable.get(key);
         if (result == null) {
             return null;
         }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java?rev=912351&r1=912350&r2=912351&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java Sun Feb 21 14:01:46 2010
@@ -141,6 +141,105 @@
     }
 
     /**
+     * Regression test for HARMONY-6452
+     */
+    public void test_RequestProperty_case_insensitivity() 
+            throws MalformedURLException, IOException {
+
+        URLConnection u =
+            (URLConnection)(new URL("http://example.org/").openConnection());
+        u.setRequestProperty("KEY", "upper");
+        u.setRequestProperty("key", "lower");
+        assertEquals("set for \"KEY\" is overwritten by set for \"key\"",
+                     "lower", u.getRequestProperty("KEY"));
+        assertEquals("value can be retrieved by either key case",
+                     "lower", u.getRequestProperty("key"));
+        assertEquals("value can be retrieved by arbitrary key case",
+                     "lower", u.getRequestProperty("kEy"));
+
+        Map<String, List<String>> props = u.getRequestProperties();
+        List<String> values = props.get("KEY");
+        assertNotNull("first key does have an entry", values);
+        assertNull("second key does not have an entry", props.get("key"));
+
+        assertEquals("returned value list is correct size", 1, values.size());
+        assertTrue("returned value list contains expected value",
+                   values.contains("lower"));
+
+
+        // repeat the above with the case of keys reversed to confirm
+        // that first key is significant one
+        u = (URLConnection)(new URL("http://example.org/").openConnection());
+        u.setRequestProperty("key", "lower");
+        u.setRequestProperty("KEY", "upper");
+        assertEquals("set for \"key\" is overwritten by set for \"KEY\"",
+                     "upper", u.getRequestProperty("KEY"));
+        assertEquals("value can be retrieved by either key case",
+                     "upper", u.getRequestProperty("key"));
+        assertEquals("value can be retrieved by arbitrary key case",
+                     "upper", u.getRequestProperty("kEy"));
+
+        props = u.getRequestProperties();
+        values = props.get("key");
+        assertNotNull("first key does have an entry", values);
+        assertNull("second key does not have an entry", props.get("KEY"));
+
+        assertEquals("returned value list is correct size", 1, values.size());
+        assertTrue("returned value list contains expected value",
+                   values.contains("upper"));
+
+
+        // repeat the first test with set and add methods
+        u = (URLConnection)(new URL("http://example.org/").openConnection());
+        u.setRequestProperty("KEY", "value1");
+        u.addRequestProperty("key", "value2");
+        assertEquals("value for \"KEY\" is the last one added",
+                     "value2", u.getRequestProperty("KEY"));
+        assertEquals("value can be retrieved by either key case",
+                     "value2", u.getRequestProperty("key"));
+        assertEquals("value can be retrieved by arbitrary key case",
+                     "value2", u.getRequestProperty("kEy"));
+
+        props = u.getRequestProperties();
+        values = props.get("KEY");
+        assertNotNull("first key does have an entry", values);
+        assertNull("second key does not have an entry", props.get("key"));
+
+        assertEquals("returned value list is correct size", 2, values.size());
+        assertTrue("returned value list contains first value",
+                   values.contains("value1"));
+        assertTrue("returned value list contains second value",
+                   values.contains("value2"));
+
+
+        // repeat the previous test with only add methods
+        u = (URLConnection)(new URL("http://example.org/").openConnection());
+        u.addRequestProperty("KEY", "value1");
+        u.addRequestProperty("key", "value2");
+        u.addRequestProperty("Key", "value3");
+        assertEquals("value for \"KEY\" is the last one added",
+                     "value3", u.getRequestProperty("KEY"));
+        assertEquals("value can be retrieved by another key case",
+                     "value3", u.getRequestProperty("key"));
+        assertEquals("value can be retrieved by arbitrary key case",
+                     "value3", u.getRequestProperty("kEy"));
+
+        props = u.getRequestProperties();
+        values = props.get("KEY");
+        assertNotNull("first key does have an entry", values);
+        assertNull("second key does not have an entry", props.get("key"));
+        assertNull("third key does not have an entry", props.get("Key"));
+
+        assertEquals("returned value list is correct size", 3, values.size());
+        assertTrue("returned value list contains first value",
+                   values.contains("value1"));
+        assertTrue("returned value list contains second value",
+                   values.contains("value2"));
+        assertTrue("returned value list contains second value",
+                   values.contains("value3"));
+    }
+
+    /**
      * @tests java.net.URLConnection#addRequestProperty(java.lang.String,java.lang.String)
      */
     public void test_addRequestPropertyLjava_lang_StringLjava_lang_String()