You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/03/24 21:19:44 UTC

svn commit: r158943 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/RequestLine.java test/org/apache/http/TestAll.java test/org/apache/http/TestRequestLine.java

Author: olegk
Date: Thu Mar 24 12:19:43 2005
New Revision: 158943

URL: http://svn.apache.org/viewcvs?view=rev&rev=158943
Log:
Added HTTP request line class

Added:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java   (with props)
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java   (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java

Added: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java?view=auto&rev=158943
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java (added)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java Thu Mar 24 12:19:43 2005
@@ -0,0 +1,107 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http;
+
+import java.util.NoSuchElementException;
+import java.util.StringTokenizer;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+public class RequestLine {
+
+    private final HttpVersion httpversion;
+    private final String method;
+    private final String uri;
+
+    public RequestLine(final String method, final String uri, final HttpVersion httpversion) {
+    	super();
+    	if (method == null) {
+    		throw new IllegalArgumentException("Method may not be null");
+    	}
+    	if (uri == null) {
+    		throw new IllegalArgumentException("URI may not be null");
+    	}
+    	if (httpversion == null) {
+    		throw new IllegalArgumentException("HTTP version may not be null");
+    	}
+    	this.method = method;
+        this.uri = uri;
+        this.httpversion = httpversion;
+    }
+
+    public String getMethod() {
+        return this.method;
+    }
+
+    public HttpVersion getHttpVersion() {
+        return this.httpversion;
+    }
+
+    public String getUri() {
+        return this.uri;
+    }
+
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append(this.method);
+        sb.append(" ");
+        sb.append(this.uri);
+        sb.append(" ");
+        sb.append(this.httpversion);
+        return sb.toString();
+    }
+    
+    public static RequestLine parse(final String requestLine) 
+            throws HttpException {
+        if (requestLine == null) {
+            throw new IllegalArgumentException("Request line string may not be null");
+        }
+        String method = null;
+        String uri = null;
+        String protocol = null;
+        try {
+            StringTokenizer st = new StringTokenizer(requestLine, " ");
+            method = st.nextToken().trim();
+            uri = st.nextToken().trim();
+            protocol = st.nextToken();
+        } catch (NoSuchElementException e) {
+            throw new ProtocolException("Invalid request line: " + requestLine);
+        }
+        return new RequestLine(method, uri, HttpVersion.parse(protocol));
+    }
+
+}

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/RequestLine.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java?view=diff&r1=158942&r2=158943
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestAll.java Thu Mar 24 12:19:43 2005
@@ -48,6 +48,7 @@
         suite.addTest(TestHttpStatus.suite());
         suite.addTest(TestHttpVersion.suite());
         suite.addTest(TestStatusLine.suite());
+        suite.addTest(TestRequestLine.suite());
         return suite;
     }
 

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java?view=auto&rev=158943
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java Thu Mar 24 12:19:43 2005
@@ -0,0 +1,123 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http;
+
+import junit.framework.*;
+
+/**
+ * Simple tests for {@link RequestLine}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ */
+public class TestRequestLine extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestRequestLine(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestRequestLine.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestRequestLine.class);
+    }
+
+    public void testConstructor() {
+        RequestLine requestline = new RequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
+        assertEquals("GET", requestline.getMethod()); 
+        assertEquals("/stuff", requestline.getUri()); 
+        assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion()); 
+    }
+        
+    public void testConstructorInvalidInput() {
+        try {
+            RequestLine requestline = new RequestLine(null, "/stuff", HttpVersion.HTTP_1_1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException e) { /* expected */ }
+        try {
+            RequestLine requestline = new RequestLine("GEt", null, HttpVersion.HTTP_1_1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException e) { /* expected */ }
+        try {
+            RequestLine requestline = new RequestLine("GET", "/stuff", (HttpVersion)null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException e) { /* expected */ }
+    }
+        
+    public void testSuccess() throws Exception {
+        //typical request line
+        RequestLine requestline = RequestLine.parse("GET /stuff HTTP/1.1");
+        assertEquals("GET /stuff HTTP/1.1", requestline.toString());
+        assertEquals("GET", requestline.getMethod());
+        assertEquals("/stuff", requestline.getUri());
+        assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion());
+
+        //Lots of blanks
+        requestline = RequestLine.parse("  GET    /stuff   HTTP/1.1   ");
+        assertEquals("GET /stuff HTTP/1.1", requestline.toString());
+        assertEquals("GET", requestline.getMethod());
+        assertEquals("/stuff", requestline.getUri());
+        assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion());
+
+        //this is not strictly valid, but is lienent
+        requestline = RequestLine.parse("\rGET /stuff HTTP/1.1");
+        assertEquals("GET", requestline.getMethod());
+        assertEquals("/stuff", requestline.getUri());
+        assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion());
+    }
+
+    public void testFailure() throws Exception {
+        RequestLine requestline = null;
+        try {
+            requestline = RequestLine.parse("GET /stuff");
+            fail();
+        } catch (HttpException e) { /* expected */ }
+
+        try {
+            requestline = RequestLine.parse("GET/stuff HTTP/1.1");
+            fail();
+        } catch (HttpException e) { /* expected */ }
+    }
+
+    public void testNullInput() throws Exception {
+        try {
+            RequestLine requestline = RequestLine.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException e) { /* expected */ }
+    }
+
+}

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestRequestLine.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain