1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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  
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.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: 155418 $
49   */
50  public class TestVirtualHost extends HttpClientTestBase {
51  
52      // ------------------------------------------------------------ Constructor
53      public TestVirtualHost(final String testName) throws IOException {
54          super(testName);
55      }
56  
57      // ------------------------------------------------------------------- Main
58      public static void main(String args[]) {
59          String[] testCaseName = { TestVirtualHost.class.getName() };
60          junit.textui.TestRunner.main(testCaseName);
61      }
62  
63      // ------------------------------------------------------- TestCase Methods
64  
65      public static Test suite() {
66          return new TestSuite(TestVirtualHost.class);
67      }
68  
69      private class VirtualService implements HttpService {
70  
71          public VirtualService() {
72              super();
73          }
74  
75          public boolean process(final SimpleRequest request, final SimpleResponse response)
76              throws IOException
77          {
78              HttpVersion httpversion = request.getRequestLine().getHttpVersion();
79              Header hostheader = request.getFirstHeader("Host");
80              if (hostheader == null) {
81                  response.setStatusLine(httpversion, HttpStatus.SC_BAD_REQUEST);
82                  response.setBodyString("Host header missing");
83              } else {
84                  response.setStatusLine(httpversion, HttpStatus.SC_OK);
85                  response.setBodyString(hostheader.getValue());
86              }
87              return true;
88          }
89      }
90  
91      public void testVirtualHostHeader() throws IOException {
92          this.server.setHttpService(new VirtualService());
93  
94          GetMethod httpget = new GetMethod("/test/");
95          
96          HostConfiguration hostconf = new HostConfiguration();
97          hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
98          hostconf.getParams().setVirtualHost("somehost");
99          try {
100             this.client.executeMethod(hostconf, httpget);
101             String hostheader = "somehost:" + this.server.getLocalPort();
102             assertEquals(hostheader, httpget.getResponseBodyAsString());
103         } finally {
104             httpget.releaseConnection();
105         }
106     }
107 
108     public void testNoVirtualHostHeader() throws IOException {
109         this.server.setHttpService(new VirtualService());
110 
111         GetMethod httpget = new GetMethod("/test/");
112         
113         HostConfiguration hostconf = new HostConfiguration();
114         hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
115         hostconf.getParams().setVirtualHost(null);
116         try {
117             this.client.executeMethod(hostconf, httpget);
118             String hostheader = this.server.getLocalAddress() + ":" + this.server.getLocalPort();
119             assertEquals(hostheader, httpget.getResponseBodyAsString());
120         } finally {
121             httpget.releaseConnection();
122         }
123     }
124 }