hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r355177 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/Header.java test/org/apache/http/TestHeader.java
Date Thu, 08 Dec 2005 19:25:25 GMT
Author: olegk
Date: Thu Dec  8 11:25:15 2005
New Revision: 355177

URL: http://svn.apache.org/viewcvs?rev=355177&view=rev
Log:
* Minor code clean up
* More test cases for Header class

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java?rev=355177&r1=355176&r2=355177&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java Thu Dec  8
11:25:15 2005
@@ -152,7 +152,10 @@
         }
     }
 
-    
+    /**
+     * This class represents a raw HTTP header whose content is parsed
+     * 'on demand' only when the header value needs to be consumed  
+     */
     static class BufferedHeader extends Header {
         
         private final CharArrayBuffer buffer;
@@ -220,15 +223,15 @@
         Header[] headers = new Header[headerLines.size()];
         for (int i = 0; i < headerLines.size(); i++) {
             CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
-            int comma = buffer.indexOf(':');
-            if (comma == -1) {
+            int colon = buffer.indexOf(':');
+            if (colon == -1) {
                 throw new ProtocolException("Invalid header: " + buffer.toString());
             }
-            String s = buffer.substringTrimmed(0, comma);
+            String s = buffer.substringTrimmed(0, colon);
             if (s.equals("")) {
                 throw new ProtocolException("Invalid header: " + buffer.toString());
             }
-            headers[i] = new BufferedHeader(s, buffer, comma + 1);
+            headers[i] = new BufferedHeader(s, buffer, colon + 1);
         }
         return headers;
     }

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java?rev=355177&r1=355176&r2=355177&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java Thu Dec
 8 11:25:15 2005
@@ -37,7 +37,7 @@
 import junit.framework.TestSuite;
 
 /**
- * Unit tests for {@link NameValuePair}.
+ * Unit tests for {@link Header}.
  *
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  */
@@ -82,6 +82,8 @@
     public void testToString() {
         Header header1 = new Header("name1", "value1");
         assertEquals("name1: value1", header1.toString());
+        Header header2 = new Header("name2", null);
+        assertEquals("name2: ", header2.toString());
     }
     
     public void testHeaderElements() {
@@ -96,6 +98,11 @@
         assertEquals("element3", elements[2].getName()); 
         assertEquals(null, elements[2].getValue()); 
         assertEquals(1, elements[1].getParameters().length); 
+        
+        header = new Header("name", null);
+        elements = header.getElements();
+        assertNotNull(elements); 
+        assertEquals(0, elements.length); 
     }    
         
     public void testBasicHeaderParsing() throws Exception {
@@ -119,6 +126,52 @@
         assertEquals("stuff and more stuff and even more stuff", headers[2].getValue());
     }
 
+    public void testBufferedHeader() throws Exception {
+        String s = 
+            "header1  : stuff; param1 = value1; param2 = \"value 2\" \r\n" + 
+            "\r\n"; 
+        HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); 
+        Header[] headers = Header.parseAll(receiver);
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("header1  : stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
+        HeaderElement[] elements = headers[0].getElements();
+        assertNotNull(elements);
+        assertEquals(1, elements.length);
+        assertEquals("stuff", elements[0].getName());
+        assertEquals(null, elements[0].getValue());
+        NameValuePair[] params = elements[0].getParameters();
+        assertNotNull(params);
+        assertEquals(2, params.length);
+        assertEquals("param1", params[0].getName());
+        assertEquals("value1", params[0].getValue());
+        assertEquals("param2", params[1].getName());
+        assertEquals("value 2", params[1].getValue());
+    }
+
+    public void testParsingInvalidHeaders() throws Exception {
+        String s = "    stuff\r\n" + 
+            "header1: stuff\r\n" + 
+            "\r\n"; 
+        HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII");
+        try {
+            Header.parseAll(receiver);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+        s = "  :  stuff\r\n" + 
+            "header1: stuff\r\n" + 
+            "\r\n"; 
+        receiver = new HttpDataReceiverMockup(s, "US-ASCII");
+        try {
+            Header.parseAll(receiver);
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+    }
+    
     public void testParsingMalformedFirstHeader() throws Exception {
         String s = 
             "    header1: stuff\r\n" + 



Mime
View raw message