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