1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java,v 1.1 2004/05/09 12:16:12 olegk Exp $
3    * $Revision: 1.1 $
4    * $Date: 2004/05/09 12:16:12 $
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  import org.apache.commons.httpclient.server.HttpService;
40  import org.apache.commons.httpclient.server.SimpleRequest;
41  import org.apache.commons.httpclient.server.SimpleResponse;
42  
43  /***
44   * HTTP protocol versioning tests.
45   *
46   * @author Oleg Kalnichevski
47   * 
48   * @version $Revision: 1.1 $
49   */
50  public class TestEffectiveHttpVersion extends HttpClientTestBase {
51  
52      // ------------------------------------------------------------ Constructor
53      public TestEffectiveHttpVersion(String testName) {
54          super(testName);
55      }
56  
57      // ------------------------------------------------------------------- Main
58      public static void main(String args[]) {
59          String[] testCaseName = { TestEffectiveHttpVersion.class.getName() };
60          junit.textui.TestRunner.main(testCaseName);
61      }
62  
63      // ------------------------------------------------------- TestCase Methods
64  
65      public static Test suite() {
66          return new TestSuite(TestEffectiveHttpVersion.class);
67      }
68  
69      private class EchoService implements HttpService {
70  
71          public EchoService() {
72              super();
73          }
74  
75          public boolean process(final SimpleRequest request, final SimpleResponse response)
76              throws IOException
77          {
78              String protocol = request.getRequestLine().getProtocol();
79              response.setStatusLine(protocol + " 200 OK");
80              response.setBodyString(request.getBodyString());
81              return true;
82          }
83      }
84  
85      public void testClientLevelHttpVersion() throws IOException {
86          this.server.setHttpService(new EchoService());
87  
88          HttpVersion testver = new HttpVersion(1, 10);
89  
90          this.client.getParams().setVersion(testver);
91          GetMethod httpget = new GetMethod("/test/");
92          try {
93              this.client.executeMethod(httpget);
94          } finally {
95              httpget.releaseConnection();
96          }
97          assertEquals(testver, httpget.getEffectiveVersion());
98      }
99  
100     public void testMethodLevelHttpVersion() throws IOException {
101         this.server.setHttpService(new EchoService());
102 
103         HttpVersion globalver = new HttpVersion(1, 10);
104         HttpVersion testver1 = new HttpVersion(1, 11);
105         HttpVersion testver2 = new HttpVersion(1, 12);
106 
107         this.client.getParams().setVersion(globalver);
108         
109         GetMethod httpget1 = new GetMethod("/test/");
110         httpget1.getParams().setVersion(testver1);
111         try {
112             this.client.executeMethod(httpget1);
113         } finally {
114             httpget1.releaseConnection();
115         }
116         assertEquals(testver1, httpget1.getEffectiveVersion());
117 
118         GetMethod httpget2 = new GetMethod("/test/");
119         httpget2.getParams().setVersion(testver2);
120         try {
121             this.client.executeMethod(httpget2);
122         } finally {
123             httpget2.releaseConnection();
124         }
125         assertEquals(testver2, httpget2.getEffectiveVersion());
126 
127         GetMethod httpget3 = new GetMethod("/test/");
128         try {
129             this.client.executeMethod(httpget3);
130         } finally {
131             httpget3.releaseConnection();
132         }
133         assertEquals(globalver, httpget3.getEffectiveVersion());
134     }
135 
136     public void testHostLevelHttpVersion() throws IOException {
137         this.server.setHttpService(new EchoService());
138 
139         HttpVersion testver = new HttpVersion(1, 11);
140         HttpVersion hostver = new HttpVersion(1, 12);
141 
142         this.client.getParams().setVersion(testver);
143         
144         GetMethod httpget1 = new GetMethod("/test/");
145         httpget1.getParams().setVersion(testver);
146         
147         HostConfiguration hostconf = new HostConfiguration();
148         hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http"); 
149         try {
150             this.client.executeMethod(hostconf, httpget1);
151         } finally {
152             httpget1.releaseConnection();
153         }
154         assertEquals(testver, httpget1.getEffectiveVersion());
155 
156         GetMethod httpget2 = new GetMethod("/test/");
157         hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
158         hostconf.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, hostver); 
159         try {
160             this.client.executeMethod(hostconf, httpget2);
161         } finally {
162             httpget2.releaseConnection();
163         }
164         assertEquals(hostver, httpget2.getEffectiveVersion());
165     }
166 }