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 155418 2005-02-26 13:01:52Z dirkv $
36   */
37  public class TestRequestLine extends TestCase {
38  
39      private StatusLine statusLine = null;
40  
41      // ------------------------------------------------------------ Constructor
42      public TestRequestLine(String testName) {
43          super(testName);
44      }
45  
46      // ------------------------------------------------------------------- Main
47      public static void main(String args[]) {
48          String[] testCaseName = { TestRequestLine.class.getName() };
49          junit.textui.TestRunner.main(testCaseName);
50      }
51  
52      // ------------------------------------------------------- TestCase Methods
53  
54      public static Test suite() {
55          return new TestSuite(TestRequestLine.class);
56      }
57  
58      // ----------------------------------------------------------- Test Methods
59  
60      public void testRequestLineGeneral() throws Exception {
61          
62          HttpConnection conn = new HttpConnection("localhost", 80);
63          FakeHttpMethod method = new FakeHttpMethod();
64          assertEquals("Simple / HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
65  
66          method = new FakeHttpMethod("stuff");
67          assertEquals("Simple stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
68  
69          conn = new HttpConnection("proxy", 8080, "localhost", 80, Protocol.getProtocol("http"));
70  
71          method = new FakeHttpMethod();
72          assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
73  
74          method = new FakeHttpMethod("stuff");
75          assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
76  
77          conn = new HttpConnection("proxy", 8080, "localhost", -1, Protocol.getProtocol("http"));
78  
79          method = new FakeHttpMethod();
80          assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
81  
82          method = new FakeHttpMethod("stuff");
83          assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
84  
85          conn = new HttpConnection("proxy", 8080, "localhost", 666, Protocol.getProtocol("http"));
86  
87          method = new FakeHttpMethod();
88          assertEquals("Simple http://localhost:666/ HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
89  
90          method = new FakeHttpMethod("stuff");
91          assertEquals("Simple http://localhost:666/stuff HTTP/1.1\r\n", method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
92      }
93  
94      public void testRequestLineQuery() throws Exception {
95          HttpConnection conn = new HttpConnection("localhost", 80);
96  
97          FakeHttpMethod method = new FakeHttpMethod();
98          method.setQueryString( new NameValuePair[] {
99              new NameValuePair("param1", " !#$%&\'()*+,-./:;<=>?@[//]^_`{|}~"),
100             new NameValuePair("param2", "some stuff")
101           } );
102         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", 
103                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
104     }
105 
106     public void testRequestLinePath() throws Exception {
107         HttpConnection conn = new HttpConnection("localhost", 80);
108 
109         FakeHttpMethod method = new FakeHttpMethod();
110         method.setPath("/some%20stuff/");
111         assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
112                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
113 
114         method = new FakeHttpMethod("/some%20stuff/");
115         assertEquals("Simple /some%20stuff/ HTTP/1.1\r\n", 
116                 method.generateRequestLine(conn, HttpVersion.HTTP_1_1));
117     }
118 }