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
33 import java.io.IOException;
34 import java.net.InetAddress;
35 import java.net.Socket;
36 import java.net.UnknownHostException;
37
38 import junit.framework.Test;
39 import junit.framework.TestSuite;
40
41 import org.apache.commons.httpclient.methods.GetMethod;
42 import org.apache.commons.httpclient.params.HttpConnectionParams;
43 import org.apache.commons.httpclient.protocol.Protocol;
44 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
45 import org.apache.commons.httpclient.server.HttpService;
46 import org.apache.commons.httpclient.server.RequestLine;
47 import org.apache.commons.httpclient.server.SimpleRequest;
48 import org.apache.commons.httpclient.server.SimpleResponse;
49
50 /***
51 * HTTP protocol versioning tests.
52 *
53 * @author Oleg Kalnichevski
54 *
55 * @version $Revision: 366870 $
56 */
57 public class TestVirtualHost extends HttpClientTestBase {
58
59
60 public TestVirtualHost(final String testName) throws IOException {
61 super(testName);
62 }
63
64
65 public static void main(String args[]) {
66 String[] testCaseName = { TestVirtualHost.class.getName() };
67 junit.textui.TestRunner.main(testCaseName);
68 }
69
70
71
72 public static Test suite() {
73 return new TestSuite(TestVirtualHost.class);
74 }
75
76 private class VirtualService implements HttpService {
77
78 public VirtualService() {
79 super();
80 }
81
82 public boolean process(final SimpleRequest request, final SimpleResponse response)
83 throws IOException
84 {
85 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
86 Header hostheader = request.getFirstHeader("Host");
87 if (hostheader == null) {
88 response.setStatusLine(httpversion, HttpStatus.SC_BAD_REQUEST);
89 response.setBodyString("Host header missing");
90 } else {
91 response.setStatusLine(httpversion, HttpStatus.SC_OK);
92 response.setBodyString(hostheader.getValue());
93 }
94 return true;
95 }
96 }
97
98 public void testVirtualHostHeader() throws IOException {
99 this.server.setHttpService(new VirtualService());
100
101 GetMethod httpget = new GetMethod("/test/");
102
103 HostConfiguration hostconf = new HostConfiguration();
104 hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
105 hostconf.getParams().setVirtualHost("somehost");
106 try {
107 this.client.executeMethod(hostconf, httpget);
108 String hostheader = "somehost:" + this.server.getLocalPort();
109 assertEquals(hostheader, httpget.getResponseBodyAsString());
110 } finally {
111 httpget.releaseConnection();
112 }
113 }
114
115 public void testNoVirtualHostHeader() throws IOException {
116 this.server.setHttpService(new VirtualService());
117
118 GetMethod httpget = new GetMethod("/test/");
119
120 HostConfiguration hostconf = new HostConfiguration();
121 hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
122 hostconf.getParams().setVirtualHost(null);
123 try {
124 this.client.executeMethod(hostconf, httpget);
125 String hostheader = this.server.getLocalAddress() + ":" + this.server.getLocalPort();
126 assertEquals(hostheader, httpget.getResponseBodyAsString());
127 } finally {
128 httpget.releaseConnection();
129 }
130 }
131
132 private class VirtualHostService implements HttpService {
133
134 public VirtualHostService() {
135 super();
136 }
137
138 public boolean process(final SimpleRequest request, final SimpleResponse response)
139 throws IOException {
140 RequestLine reqline = request.getRequestLine();
141 HttpVersion ver = reqline.getHttpVersion();
142 Header header = request.getFirstHeader("Host");
143 if (header == null) {
144 response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST);
145 return true;
146 }
147 String host = header.getValue();
148 if (host.equalsIgnoreCase("whatever.com")) {
149 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
150 response.setHeader(new Header("Location", "testhttp://www.whatever.com/"));
151 return true;
152 } else if (host.equalsIgnoreCase("www.whatever.com")) {
153 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
154 response.setHeader(new Header("Location", "testhttp://www.whatever.co.nz/"));
155 return true;
156 } else if (host.equalsIgnoreCase("www.whatever.co.nz")) {
157 response.setStatusLine(ver, HttpStatus.SC_OK);
158 return true;
159 } else {
160 response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
161 return true;
162 }
163 }
164 }
165
166 private class VirtualSocketFactory implements ProtocolSocketFactory {
167
168 private final String hostname;
169 private final int port;
170
171 public VirtualSocketFactory(final String hostname, int port) {
172 super();
173 this.hostname = hostname;
174 this.port = port;
175 }
176
177 public Socket createSocket(
178 final String host,
179 int port,
180 final InetAddress localAddress,
181 int localPort,
182 final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
183 return new Socket(this.hostname, this.port);
184 }
185
186 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException {
187 return new Socket(this.hostname, this.port);
188 }
189
190 public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
191 return new Socket(this.hostname, this.port);
192 }
193
194 }
195
196 public void testRedirectWithVirtualHost() throws IOException {
197 String host = this.server.getLocalAddress();
198 int port = this.server.getLocalPort();
199
200 Protocol testhttp = new Protocol("http", new VirtualSocketFactory(host, port), port);
201 Protocol.registerProtocol("testhttp", testhttp);
202 try {
203 this.server.setHttpService(new VirtualHostService());
204 this.client.getHostConfiguration().setHost(host, port, "testhttp");
205 this.client.getHostConfiguration().getParams().setVirtualHost("whatever.com");
206 GetMethod httpget = new GetMethod("/");
207 httpget.setFollowRedirects(true);
208 try {
209 this.client.executeMethod(httpget);
210 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
211 assertEquals("http://www.whatever.co.nz/", httpget.getURI().toString());
212 } finally {
213 httpget.releaseConnection();
214 }
215 } finally {
216 Protocol.unregisterProtocol("testhttp");
217 }
218
219 }
220
221 }