hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r354831 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ test/org/apache/http/
Date Wed, 07 Dec 2005 19:10:58 GMT
Author: olegk
Date: Wed Dec  7 11:10:47 2005
New Revision: 354831

URL: http://svn.apache.org/viewcvs?rev=354831&view=rev
Log:
Added static #format methods to HTTP version, request line and status line classes

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java Wed Dec
 7 11:10:47 2005
@@ -284,5 +284,24 @@
         buffer.append(s);
         return parse(buffer, 0, buffer.length());
     }
-    
+
+    public static void format(final CharArrayBuffer buffer, final HttpVersion ver) {
+        if (buffer == null) {
+            throw new IllegalArgumentException("String buffer may not be null");
+        }
+        if (ver == null) {
+            throw new IllegalArgumentException("Version may not be null");
+        }
+        buffer.append("HTTP/"); 
+        buffer.append(Integer.toString(ver.getMajor())); 
+        buffer.append('.'); 
+        buffer.append(Integer.toString(ver.getMinor())); 
+    }
+ 
+    public static String format(final HttpVersion ver) {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        format(buffer, ver);
+        return buffer.toString();
+    }
+        
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java Wed Dec
 7 11:10:47 2005
@@ -138,4 +138,24 @@
         return parse(buffer, 0, buffer.length());
     }
     
+    public static void format(final CharArrayBuffer buffer, final RequestLine requestline)
{
+        if (buffer == null) {
+            throw new IllegalArgumentException("String buffer may not be null");
+        }
+        if (requestline == null) {
+            throw new IllegalArgumentException("Request line may not be null");
+        }
+        buffer.append(requestline.getMethod());
+        buffer.append(' ');
+        buffer.append(requestline.getUri());
+        buffer.append(' ');
+        HttpVersion.format(buffer, requestline.getHttpVersion());
+    }
+ 
+    public static String format(final RequestLine requestline) {
+        CharArrayBuffer buffer = new CharArrayBuffer(64);
+        format(buffer, requestline);
+        return buffer.toString();
+    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java Wed Dec
 7 11:10:47 2005
@@ -207,4 +207,26 @@
         return buffer.toString();
     }
     
+    public static void format(final CharArrayBuffer buffer, final StatusLine statusline)
{
+        if (buffer == null) {
+            throw new IllegalArgumentException("String buffer may not be null");
+        }
+        if (statusline == null) {
+            throw new IllegalArgumentException("Status line may not be null");
+        }
+        HttpVersion.format(buffer, statusline.getHttpVersion());
+        buffer.append(' ');
+        buffer.append(Integer.toString(statusline.getStatusCode()));
+        if (statusline.getReasonPhrase() != null) {
+            buffer.append(' ');
+            buffer.append(statusline.getReasonPhrase());
+        }
+    }
+ 
+    public static String format(final StatusLine statusline) {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        format(buffer, statusline);
+        return buffer.toString();
+    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java Wed
Dec  7 11:10:47 2005
@@ -154,7 +154,7 @@
         }
     }
 
-    public void testInvalidInput() throws Exception {
+    public void testHttpVersionParsingInvalidInput() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("HTTP/1.1");
         try {
@@ -222,5 +222,26 @@
         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0);
         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0);
    }
+    
+    public void testHttpVersionFormatting() throws Exception {
+        String s = HttpVersion.format(HttpVersion.HTTP_1_1);
+        assertEquals("HTTP/1.1", s);
+    }
+    
+    public void testHttpVersionFormattingInvalidInput() throws Exception {
+        try {
+            HttpVersion.format(null, HttpVersion.HTTP_1_1);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HttpVersion.format(new CharArrayBuffer(10), (HttpVersion) null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
 }
 

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java Wed
Dec  7 11:10:47 2005
@@ -80,7 +80,7 @@
         } catch (IllegalArgumentException e) { /* expected */ }
     }
         
-    public void testSuccess() throws Exception {
+    public void testParseSuccess() throws Exception {
         //typical request line
         RequestLine requestline = RequestLine.parse("GET /stuff HTTP/1.1");
         assertEquals("GET /stuff HTTP/1.1", requestline.toString());
@@ -102,7 +102,7 @@
         assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion());
     }
 
-    public void testFailure() throws Exception {
+    public void testParseFailure() throws Exception {
         try {
             RequestLine.parse("    ");
             fail();
@@ -124,7 +124,7 @@
         } catch (HttpException e) { /* expected */ }
     }
 
-    public void testInvalidInput() throws Exception {
+    public void testParseInvalidInput() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("GET /stuff HTTP/1.1");
         try {
@@ -159,4 +159,25 @@
         }
     }
 
+    public void testFormatting() throws Exception {
+        RequestLine requestline = new RequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
+        String s = RequestLine.format(requestline);
+        assertEquals("GET /stuff HTTP/1.1", s);
+    }
+    
+    public void testFormattingInvalidInput() throws Exception {
+        try {
+            RequestLine.format(null, new RequestLine("GET", "/stuff", HttpVersion.HTTP_1_1));
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            RequestLine.format(new CharArrayBuffer(10), (RequestLine) null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java?rev=354831&r1=354830&r2=354831&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java Wed
Dec  7 11:10:47 2005
@@ -86,7 +86,7 @@
         } catch (IllegalArgumentException e) { /* expected */ }
     }
         
-    public void testSuccess() throws Exception {
+    public void testParseSuccess() throws Exception {
         //typical status line
         StatusLine statusLine = StatusLine.parse("HTTP/1.1 200 OK");
         assertEquals("HTTP/1.1 200 OK", statusLine.toString());
@@ -135,7 +135,7 @@
         assertEquals(HttpVersion.HTTP_1_1, statusLine.getHttpVersion());
     }
 
-    public void testFailure() throws Exception {
+    public void testParseFailure() throws Exception {
         try {
             StatusLine.parse("xxx 200 OK");
             fail();
@@ -156,7 +156,7 @@
         } catch (HttpException e) { /* expected */ }
     }
 
-    public void testInvalidInput() throws Exception {
+    public void testParseInvalidInput() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("HTTP/1.1 200 OK");
         try {
@@ -197,4 +197,29 @@
         statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
         assertEquals("HTTP/1.1 200", statusline.toString());
     }
+    
+    public void testFormatting() throws Exception {
+        StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
+        String s = StatusLine.format(statusline);
+        assertEquals("HTTP/1.1 200 OK", s);
+        statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
+        s = StatusLine.format(statusline);
+        assertEquals("HTTP/1.1 200", s);
+    }
+    
+    public void testFormattingInvalidInput() throws Exception {
+        try {
+            StatusLine.format(null, new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK"));
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            StatusLine.format(new CharArrayBuffer(10), (StatusLine) null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
 }



Mime
View raw message