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