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
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
53 public TestVirtualHost(final String testName) throws IOException {
54 super(testName);
55 }
56
57
58 public static void main(String args[]) {
59 String[] testCaseName = { TestVirtualHost.class.getName() };
60 junit.textui.TestRunner.main(testCaseName);
61 }
62
63
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 }