hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r159509 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/Header.java test/org/apache/http/TestHeader.java
Date Wed, 30 Mar 2005 18:53:04 GMT
Author: olegk
Date: Wed Mar 30 10:53:04 2005
New Revision: 159509

URL: http://svn.apache.org/viewcvs?view=rev&rev=159509
Log:
Added static Header#parse method

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/Header.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/Header.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/Header.java?view=diff&r1=159508&r2=159509
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/Header.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/Header.java Wed Mar 30 10:53:04
2005
@@ -154,4 +154,17 @@
         return hash;
     }
     
+    public static Header parse(final String headerLine) throws ProtocolException {
+        if (headerLine == null) {
+            throw new IllegalArgumentException("Header line may not be null");
+        }
+        int colon = headerLine.indexOf(":");
+        if (colon < 0) {
+            throw new ProtocolException("Unable to parse header: " + headerLine);
+        }
+        String name = headerLine.substring(0, colon).trim();
+        String value = headerLine.substring(colon + 1).trim();
+        return new Header(name, value);
+    }
+    
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java?view=diff&r1=159508&r2=159509
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java Wed Mar
30 10:53:04 2005
@@ -106,7 +106,29 @@
         assertEquals("element3", elements[2].getName()); 
         assertEquals(null, elements[2].getValue()); 
         assertEquals(1, elements[1].getParameters().length); 
-
     }    
     
+    public void testHeaderParsing() throws Exception {
+        Header header1 = Header.parse("name:value"); 
+        assertEquals("name", header1.getName());
+        assertEquals("value", header1.getValue());
+
+        Header header2 = Header.parse("    name   :    value       "); 
+        assertEquals("name", header2.getName());
+        assertEquals("value", header2.getValue());
+        
+        try {
+            Header header3 = Header.parse("  whatever  ");
+            fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            // expected
+        }
+
+        try {
+            Header header3 = Header.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }    
 }



Mime
View raw message