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.net.InetAddress;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.apache.commons.httpclient.methods.GetMethod;
39
40 /***
41 * This suite of tests depends upon the httpclienttest webapp,
42 * which is available in the httpclient/src/test-webapp
43 * directory in the CVS tree.
44 * <p>
45 * The webapp should be deployed in the context "httpclienttest"
46 * on a servlet engine running on port 8080 on the localhost
47 * (IP 127.0.0.1).
48 * <p>
49 * You can change the assumed port by setting the
50 * "httpclient.test.localPort" property.
51 * You can change the assumed host by setting the
52 * "httpclient.test.localHost" property.
53 * You can change the assumed context by setting the
54 * "httpclient.test.webappContext" property.
55 *
56 * @author Rodney Waldhoff
57 * @version $Id: TestWebappHeaders.java,v 1.10.2.1 2004/02/22 18:21:16 olegk Exp $
58 */
59 public class TestWebappHeaders extends TestWebappBase {
60
61 public TestWebappHeaders(String testName) {
62 super(testName);
63 }
64
65 public static Test suite() {
66 TestSuite suite = new TestSuite(TestWebappHeaders.class);
67 return suite;
68 }
69
70 public static void main(String args[]) {
71 String[] testCaseName = { TestWebappHeaders.class.getName() };
72 junit.textui.TestRunner.main(testCaseName);
73 }
74
75
76
77 /***
78 * Test {@link HttpMethod#addRequestHeader}.
79 */
80 public void testAddRequestHeader() throws Exception {
81 HttpClient client = createHttpClient();
82 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
83 method.setRequestHeader(new Header("addRequestHeader(Header)","True"));
84 method.setRequestHeader("addRequestHeader(String,String)","Also True");
85
86 try {
87 client.executeMethod(method);
88 } catch (Throwable t) {
89 t.printStackTrace();
90 fail("Unable to execute method : " + t.toString());
91 }
92
93 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(header)\";value=\"True\"<br>") >= 0);
94 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(string,string)\";value=\"Also True\"<br>") >= 0);
95 }
96
97 /***
98 * Test {@link HttpMethod#removeRequestHeader}.
99 */
100 public void testRemoveRequestHeader() throws Exception {
101 HttpClient client = createHttpClient();
102 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
103 method.setRequestHeader(new Header("XXX-A-HEADER","true"));
104 method.removeRequestHeader("XXX-A-HEADER");
105
106 try {
107 client.executeMethod(method);
108 } catch (Throwable t) {
109 t.printStackTrace();
110 fail("Unable to execute method : " + t.toString());
111 }
112
113 assertTrue(!(method.getResponseBodyAsString().indexOf("xxx-a-header") >= 0));
114 }
115
116 /***
117 * Test {@link HttpMethod#addRequestHeader}.
118 */
119 public void testOverwriteRequestHeader() throws Exception {
120 HttpClient client = createHttpClient();
121 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
122 method.setRequestHeader(new Header("xxx-a-header","one"));
123 method.setRequestHeader("XXX-A-HEADER","two");
124
125 try {
126 client.executeMethod(method);
127 } catch (Throwable t) {
128 t.printStackTrace();
129 fail("Unable to execute method : " + t.toString());
130 }
131
132 assertTrue(method.getResponseBodyAsString().indexOf("name=\"xxx-a-header\";value=\"two\"<br>") >= 0);
133 }
134
135 /***
136 * Test {@link HttpMethod#getResponseHeader}.
137 */
138 public void testGetResponseHeader() throws Exception {
139 HttpClient client = createHttpClient();
140 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
141
142 try {
143 client.executeMethod(method);
144 } catch (Throwable t) {
145 t.printStackTrace();
146 fail("Unable to execute method : " + t.toString());
147 }
148 Header h = new Header("HeaderSetByServlet","Yes");
149 assertEquals(h,method.getResponseHeader("headersetbyservlet"));
150 }
151
152 /***
153 * Test {@link HttpMethodBase.addHostRequestHeader}.
154 */
155 public void testHostRequestHeader() throws Exception {
156 InetAddress addr = InetAddress.getByName(getHost());
157 String ip = addr.getHostAddress();
158 String hostname = addr.getHostName();
159
160 HttpClient client = new HttpClient();
161 GetMethod get = new GetMethod("/" + getWebappContext());
162
163
164
165
166
167 client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
168 try {
169 client.executeMethod(get);
170 } catch (Throwable t) {
171 t.printStackTrace();
172 fail("Unable to execute method : " + t.toString());
173 }
174 Header hostHeader = get.getRequestHeader("Host");
175 assertTrue(hostHeader != null);
176
177
178 get.recycle();
179 get.setPath("/" + getWebappContext());
180
181
182
183
184
185
186 client.getHostConfiguration().setHost(hostname, getPort(), getProtocol());
187 try {
188 client.executeMethod(get);
189 } catch (Throwable t) {
190 t.printStackTrace();
191 fail("Unable to execute method : " + t.toString());
192 }
193 hostHeader = get.getRequestHeader("Host");
194 assertTrue(hostHeader != null);
195 if (getPort() == 80) {
196
197 assertTrue(hostHeader.getValue().equals(hostname));
198 } else {
199 assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort()));
200 }
201 }
202
203 }
204