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 package org.apache.commons.httpclient;
29
30 import java.io.IOException;
31 import java.net.UnknownHostException;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.httpclient.methods.GetMethod;
37 import org.apache.commons.httpclient.protocol.Protocol;
38 import org.apache.commons.httpclient.server.SimpleProxy;
39
40 /***
41 * Tests basic HostConfiguration functionality.
42 *
43 * @author Oleg Kalnichevski
44 *
45 * @version $Id: TestHostConfiguration.java 169849 2005-05-12 17:05:07Z olegk $
46 */
47 public class TestHostConfiguration extends HttpClientTestBase {
48
49 public TestHostConfiguration(final String testName) throws IOException {
50 super(testName);
51 }
52
53 public static Test suite() {
54 return new TestSuite(TestHostConfiguration.class);
55 }
56
57 public static void main(String args[]) {
58 String[] testCaseName = { TestHostConfiguration.class.getName() };
59 junit.textui.TestRunner.main(testCaseName);
60 }
61
62 public void testRelativeURLHitWithDefaultHost() throws IOException {
63 this.server.setHttpService(new EchoService());
64
65 this.client.getHostConfiguration().setHost(
66 this.server.getLocalAddress(),
67 this.server.getLocalPort(),
68 Protocol.getProtocol("http"));
69
70 GetMethod httpget = new GetMethod("/test/");
71 try {
72 this.client.executeMethod(httpget);
73 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
74 } finally {
75 httpget.releaseConnection();
76 }
77 }
78
79 public void testRelativeURLHitWithoutDefaultHost() throws IOException {
80 this.server.setHttpService(new EchoService());
81
82 this.client.setHostConfiguration(new HostConfiguration());
83
84 GetMethod httpget = new GetMethod("/test/");
85 try {
86 this.client.executeMethod(httpget);
87 fail("IllegalArgumentException should have been thrown");
88 } catch (IllegalArgumentException expected) {
89 } finally {
90 httpget.releaseConnection();
91 }
92 }
93
94 public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
95 this.server.setHttpService(new EchoService());
96
97 this.client.setHostConfiguration(new HostConfiguration());
98
99 GetMethod httpget = new GetMethod("http://" +
100 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
101 try {
102 this.client.executeMethod(httpget);
103 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
104 } finally {
105 httpget.releaseConnection();
106 }
107 }
108
109 public void testAbsoluteURLOverridesClientDefaultHost() throws IOException {
110 this.server.setHttpService(new EchoService());
111
112 this.client.getHostConfiguration().setHost("somewhere.outthere.in.pampa", 9999);
113
114 GetMethod httpget = new GetMethod("http://" +
115 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
116 try {
117 this.client.executeMethod(httpget);
118 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
119 } finally {
120 httpget.releaseConnection();
121 }
122 httpget = new GetMethod("/test/");
123 try {
124 this.client.executeMethod(httpget);
125 fail("UnknownHostException should have been thrown");
126 } catch (UnknownHostException expected) {
127 } finally {
128 httpget.releaseConnection();
129 }
130 }
131
132 public void testAbsoluteURLOverridesDefaultHostParam() throws IOException {
133
134 this.proxy = new SimpleProxy();
135
136 this.server.setHttpService(new EchoService());
137
138 HostConfiguration hostconfig = new HostConfiguration();
139 hostconfig.setHost("somehwere.outthere.in.pampa", 9999);
140 hostconfig.setProxy(
141 this.proxy.getLocalAddress(),
142 this.proxy.getLocalPort());
143
144 GetMethod httpget = new GetMethod("http://" +
145 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
146 try {
147 this.client.executeMethod(hostconfig, httpget);
148 assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
149 assertNotNull(httpget.getResponseHeader("Via"));
150 } finally {
151 httpget.releaseConnection();
152 }
153 httpget = new GetMethod("/test/");
154 try {
155 this.client.executeMethod(hostconfig, httpget);
156 assertEquals(HttpStatus.SC_NOT_FOUND, httpget.getStatusCode());
157 } finally {
158 httpget.releaseConnection();
159 }
160 }
161
162 }