1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53 public TestEffectiveHttpVersion(String testName) {
54 super(testName);
55 }
56
57
58 public static void main(String args[]) {
59 String[] testCaseName = { TestEffectiveHttpVersion.class.getName() };
60 junit.textui.TestRunner.main(testCaseName);
61 }
62
63
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 }