1   /*
2    * ====================================================================
3    *
4    *  Copyright 1999-2004 The Apache Software Foundation
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   * ====================================================================
18   *
19   * This software consists of voluntary contributions made by many
20   * individuals on behalf of the Apache Software Foundation.  For more
21   * information on the Apache Software Foundation, please see
22   * <http://www.apache.org/>.
23   */
24  
25  package org.apache.commons.httpclient;
26  
27  
28  import org.apache.commons.httpclient.protocol.Protocol; 
29  import junit.framework.*;
30  
31  /***
32   * Simple tests for {@link StatusLine}.
33   *
34   * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a>
35   * @version $Id: TestRequestLine.java 224451 2005-07-23 10:23:59Z olegk $
36   */
37  public class TestRequestLine extends TestCase {
38  
39      // ------------------------------------------------------------ Constructor
40      public TestRequestLine(String testName) {
41          super(testName);
42      }
43  
44      // ------------------------------------------------------------------- Main
45      public static void main(String args[]) {
46          String[] testCaseName = { TestRequestLine.class.getName() };
47          junit.textui.TestRunner.main(testCaseName);
48      }
49  
50      // ------------------------------------------------------- TestCase Methods
51  
52      public static Test suite() {
53          return new TestSuite(TestRequestLine.class);
54      }
55  
56      // ----------------------------------------------------------- Test Methods
57  
58      public void testRequestLineGeneral() throws Exception {
59          
60          HttpConnection conn = new HttpConnection("localhost", 80);
61          FakeHttpMethod method = new FakeHttpMethod();
62          assertEquals("Simple / HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
63  
64          method = new FakeHttpMethod("stuff");
65          assertEquals("Simple stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
66  
67          conn = new HttpConnection("proxy", 8080, "localhost", 80, Protocol.getProtocol("http"));
68  
69          method = new FakeHttpMethod();
70          assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
71  
72          method = new FakeHttpMethod("stuff");
73          assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
74  
75          conn = new HttpConnection("proxy", 8080, "localhost", -1, Protocol.getProtocol("http"));
76  
77          method = new FakeHttpMethod();
78          assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
79  
80          method = new FakeHttpMethod("stuff");
81          assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
82  
83          conn = new HttpConnection("proxy", 8080, "localhost", 666, Protocol.getProtocol("http"));
84  
85          method = new FakeHttpMethod();
86          assertEquals("Simple http://localhost:666/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
87  
88          method = new FakeHttpMethod("stuff");
89          assertEquals("Simple http://localhost:666/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
90      }
91  
92      public void testRequestLineQuery() throws Exception {
93          HttpConnection conn = new HttpConnection("localhost", 80);
94  
95          FakeHttpMethod method = new FakeHttpMethod();
96          method.setQueryString( new NameValuePair[] {
97              new NameValuePair("param1", " !#$%&\'()*+,-./:;<=>?@[//]^_`{|}~"),
98              new NameValuePair("param2", "some stuff")
99            } );
100         assertEquals("Simple /?param1=+%21%23%24%25%26%27%28%29*%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D%7E&param2=some+stuff HTTP/1.1\r\n", 
101                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
102     }
103 
104     public void testRequestLinePath() throws Exception {
105         HttpConnection conn = new HttpConnection("localhost", 80);
106 
107         FakeHttpMethod method = new FakeHttpMethod();
108         method.setPath("/some%20stuff/");
109         assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
110                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
111 
112         method = new FakeHttpMethod("/some%20stuff/");
113         assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
114                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
115     }
116 }