1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java,v 1.4 2004/10/31 14:42:59 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  import java.io.IOException;
33  
34  import junit.framework.Test;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.httpclient.methods.GetMethod;
38  import org.apache.commons.httpclient.params.HttpMethodParams;
39  
40  /***
41   * HTTP protocol versioning tests.
42   *
43   * @author Oleg Kalnichevski
44   * 
45   * @version $Revision: 155418 $
46   */
47  public class TestEffectiveHttpVersion extends HttpClientTestBase {
48  
49      // ------------------------------------------------------------ Constructor
50      public TestEffectiveHttpVersion(final String testName) throws IOException {
51          super(testName);
52      }
53  
54      // ------------------------------------------------------------------- Main
55      public static void main(String args[]) {
56          String[] testCaseName = { TestEffectiveHttpVersion.class.getName() };
57          junit.textui.TestRunner.main(testCaseName);
58      }
59  
60      // ------------------------------------------------------- TestCase Methods
61  
62      public static Test suite() {
63          return new TestSuite(TestEffectiveHttpVersion.class);
64      }
65  
66      public void testClientLevelHttpVersion() throws IOException {
67          this.server.setHttpService(new EchoService());
68  
69          HttpVersion testver = new HttpVersion(1, 10);
70  
71          this.client.getParams().setVersion(testver);
72          GetMethod httpget = new GetMethod("/test/");
73          try {
74              this.client.executeMethod(httpget);
75          } finally {
76              httpget.releaseConnection();
77          }
78          assertEquals(testver, httpget.getEffectiveVersion());
79      }
80  
81      public void testMethodLevelHttpVersion() throws IOException {
82          this.server.setHttpService(new EchoService());
83  
84          HttpVersion globalver = new HttpVersion(1, 10);
85          HttpVersion testver1 = new HttpVersion(1, 11);
86          HttpVersion testver2 = new HttpVersion(1, 12);
87  
88          this.client.getParams().setVersion(globalver);
89          
90          GetMethod httpget1 = new GetMethod("/test/");
91          httpget1.getParams().setVersion(testver1);
92          try {
93              this.client.executeMethod(httpget1);
94          } finally {
95              httpget1.releaseConnection();
96          }
97          assertEquals(testver1, httpget1.getEffectiveVersion());
98  
99          GetMethod httpget2 = new GetMethod("/test/");
100         httpget2.getParams().setVersion(testver2);
101         try {
102             this.client.executeMethod(httpget2);
103         } finally {
104             httpget2.releaseConnection();
105         }
106         assertEquals(testver2, httpget2.getEffectiveVersion());
107 
108         GetMethod httpget3 = new GetMethod("/test/");
109         try {
110             this.client.executeMethod(httpget3);
111         } finally {
112             httpget3.releaseConnection();
113         }
114         assertEquals(globalver, httpget3.getEffectiveVersion());
115     }
116 
117     public void testHostLevelHttpVersion() throws IOException {
118         this.server.setHttpService(new EchoService());
119 
120         HttpVersion testver = new HttpVersion(1, 11);
121         HttpVersion hostver = new HttpVersion(1, 12);
122 
123         this.client.getParams().setVersion(testver);
124         
125         GetMethod httpget1 = new GetMethod("/test/");
126         httpget1.getParams().setVersion(testver);
127         
128         HostConfiguration hostconf = new HostConfiguration();
129         hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http"); 
130         try {
131             this.client.executeMethod(hostconf, httpget1);
132         } finally {
133             httpget1.releaseConnection();
134         }
135         assertEquals(testver, httpget1.getEffectiveVersion());
136 
137         GetMethod httpget2 = new GetMethod("/test/");
138         hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
139         hostconf.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, hostver); 
140         try {
141             this.client.executeMethod(hostconf, httpget2);
142         } finally {
143             httpget2.releaseConnection();
144         }
145         assertEquals(hostver, httpget2.getEffectiveVersion());
146     }
147 }