001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestVirtualHost.java,v 1.2 2004/10/31 14:42:59 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.IOException;
035: import java.net.InetAddress;
036: import java.net.Socket;
037: import java.net.UnknownHostException;
038:
039: import junit.framework.Test;
040: import junit.framework.TestSuite;
041:
042: import org.apache.commons.httpclient.methods.GetMethod;
043: import org.apache.commons.httpclient.params.HttpConnectionParams;
044: import org.apache.commons.httpclient.protocol.Protocol;
045: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
046: import org.apache.commons.httpclient.server.HttpService;
047: import org.apache.commons.httpclient.server.RequestLine;
048: import org.apache.commons.httpclient.server.SimpleRequest;
049: import org.apache.commons.httpclient.server.SimpleResponse;
050:
051: /**
052: * HTTP protocol versioning tests.
053: *
054: * @author Oleg Kalnichevski
055: *
056: * @version $Revision: 480424 $
057: */
058: public class TestVirtualHost extends HttpClientTestBase {
059:
060: // ------------------------------------------------------------ Constructor
061: public TestVirtualHost(final String testName) throws IOException {
062: super (testName);
063: }
064:
065: // ------------------------------------------------------------------- Main
066: public static void main(String args[]) {
067: String[] testCaseName = { TestVirtualHost.class.getName() };
068: junit.textui.TestRunner.main(testCaseName);
069: }
070:
071: // ------------------------------------------------------- TestCase Methods
072:
073: public static Test suite() {
074: return new TestSuite(TestVirtualHost.class);
075: }
076:
077: private class VirtualService implements HttpService {
078:
079: public VirtualService() {
080: super ();
081: }
082:
083: public boolean process(final SimpleRequest request,
084: final SimpleResponse response) throws IOException {
085: HttpVersion httpversion = request.getRequestLine()
086: .getHttpVersion();
087: Header hostheader = request.getFirstHeader("Host");
088: if (hostheader == null) {
089: response.setStatusLine(httpversion,
090: HttpStatus.SC_BAD_REQUEST);
091: response.setBodyString("Host header missing");
092: } else {
093: response.setStatusLine(httpversion, HttpStatus.SC_OK);
094: response.setBodyString(hostheader.getValue());
095: }
096: return true;
097: }
098: }
099:
100: public void testVirtualHostHeader() throws IOException {
101: this .server.setHttpService(new VirtualService());
102:
103: GetMethod httpget = new GetMethod("/test/");
104:
105: HostConfiguration hostconf = new HostConfiguration();
106: hostconf.setHost(this .server.getLocalAddress(), this .server
107: .getLocalPort(), "http");
108: hostconf.getParams().setVirtualHost("somehost");
109: try {
110: this .client.executeMethod(hostconf, httpget);
111: String hostheader = "somehost:"
112: + this .server.getLocalPort();
113: assertEquals(hostheader, httpget.getResponseBodyAsString());
114: } finally {
115: httpget.releaseConnection();
116: }
117: }
118:
119: public void testNoVirtualHostHeader() throws IOException {
120: this .server.setHttpService(new VirtualService());
121:
122: GetMethod httpget = new GetMethod("/test/");
123:
124: HostConfiguration hostconf = new HostConfiguration();
125: hostconf.setHost(this .server.getLocalAddress(), this .server
126: .getLocalPort(), "http");
127: hostconf.getParams().setVirtualHost(null);
128: try {
129: this .client.executeMethod(hostconf, httpget);
130: String hostheader = this .server.getLocalAddress() + ":"
131: + this .server.getLocalPort();
132: assertEquals(hostheader, httpget.getResponseBodyAsString());
133: } finally {
134: httpget.releaseConnection();
135: }
136: }
137:
138: private class VirtualHostService implements HttpService {
139:
140: public VirtualHostService() {
141: super ();
142: }
143:
144: public boolean process(final SimpleRequest request,
145: final SimpleResponse response) throws IOException {
146: RequestLine reqline = request.getRequestLine();
147: HttpVersion ver = reqline.getHttpVersion();
148: Header header = request.getFirstHeader("Host");
149: if (header == null) {
150: response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST);
151: return true;
152: }
153: String host = header.getValue();
154: if (host.equalsIgnoreCase("whatever.com")) {
155: response.setStatusLine(ver,
156: HttpStatus.SC_MOVED_TEMPORARILY);
157: response.setHeader(new Header("Location",
158: "testhttp://www.whatever.com/"));
159: return true;
160: } else if (host.equalsIgnoreCase("www.whatever.com")) {
161: response.setStatusLine(ver,
162: HttpStatus.SC_MOVED_TEMPORARILY);
163: response.setHeader(new Header("Location",
164: "testhttp://www.whatever.co.nz/"));
165: return true;
166: } else if (host.equalsIgnoreCase("www.whatever.co.nz")) {
167: response.setStatusLine(ver, HttpStatus.SC_OK);
168: return true;
169: } else {
170: response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
171: return true;
172: }
173: }
174: }
175:
176: private class VirtualSocketFactory implements ProtocolSocketFactory {
177:
178: private final String hostname;
179: private final int port;
180:
181: public VirtualSocketFactory(final String hostname, int port) {
182: super ();
183: this .hostname = hostname;
184: this .port = port;
185: }
186:
187: public Socket createSocket(final String host, int port,
188: final InetAddress localAddress, int localPort,
189: final HttpConnectionParams params) throws IOException,
190: UnknownHostException, ConnectTimeoutException {
191: return new Socket(this .hostname, this .port);
192: }
193:
194: public Socket createSocket(String host, int port,
195: InetAddress localAddress, int localPort)
196: throws IOException, UnknownHostException {
197: return new Socket(this .hostname, this .port);
198: }
199:
200: public Socket createSocket(String host, int port)
201: throws IOException, UnknownHostException {
202: return new Socket(this .hostname, this .port);
203: }
204:
205: }
206:
207: public void testRedirectWithVirtualHost() throws IOException {
208: String host = this .server.getLocalAddress();
209: int port = this .server.getLocalPort();
210:
211: Protocol testhttp = new Protocol("http",
212: new VirtualSocketFactory(host, port), port);
213: Protocol.registerProtocol("testhttp", testhttp);
214: try {
215: this .server.setHttpService(new VirtualHostService());
216: this .client.getHostConfiguration().setHost(host, port,
217: "testhttp");
218: this .client.getHostConfiguration().getParams()
219: .setVirtualHost("whatever.com");
220: GetMethod httpget = new GetMethod("/");
221: httpget.setFollowRedirects(true);
222: try {
223: this .client.executeMethod(httpget);
224: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
225: assertEquals("http://www.whatever.co.nz/", httpget
226: .getURI().toString());
227: } finally {
228: httpget.releaseConnection();
229: }
230: } finally {
231: Protocol.unregisterProtocol("testhttp");
232: }
233:
234: }
235:
236: }
|