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 package org.apache.commons.httpclient;
30
31 import java.io.IOException;
32 import java.net.InetAddress;
33 import java.util.Iterator;
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.protocol.Protocol;
40 import org.apache.commons.httpclient.server.HttpService;
41 import org.apache.commons.httpclient.server.SimpleRequest;
42 import org.apache.commons.httpclient.server.SimpleResponse;
43
44 /***
45 * @author Rodney Waldhoff
46 * @version $Id: TestHeaderOps.java 192986 2005-06-22 19:51:04Z olegk $
47 */
48 public class TestHeaderOps extends HttpClientTestBase {
49
50 public TestHeaderOps(String testName) throws Exception {
51 super(testName);
52 }
53
54 public static Test suite() {
55 TestSuite suite = new TestSuite(TestHeaderOps.class);
56 return suite;
57 }
58
59 public static void main(String args[]) {
60 String[] testCaseName = { TestHeaderOps.class.getName() };
61 junit.textui.TestRunner.main(testCaseName);
62 }
63
64
65
66 class HeaderDumpService implements HttpService {
67
68 public HeaderDumpService() {
69 super();
70 }
71
72 public boolean process(final SimpleRequest request, final SimpleResponse response)
73 throws IOException
74 {
75 HttpVersion httpversion = request.getRequestLine().getHttpVersion();
76 response.setStatusLine(httpversion, HttpStatus.SC_OK);
77 response.addHeader(new Header("Content-Type", "text/plain"));
78 response.addHeader(new Header("HeaderSetByServlet", "Yes"));
79
80 StringBuffer buffer = new StringBuffer();
81 buffer.append("Request headers: \r\n");
82 for (Iterator i = request.getHeaderIterator(); i.hasNext(); ) {
83 Header header = (Header) i.next();
84 buffer.append("name=\"");
85 buffer.append(header.getName().toLowerCase());
86 buffer.append("\";value=\"");
87 buffer.append(header.getValue());
88 buffer.append("\"\r\n");
89 }
90 response.setBodyString(buffer.toString());
91 return true;
92 }
93 }
94
95 /***
96 * Test {@link HttpMethod#addRequestHeader}.
97 */
98 public void testAddRequestHeader() throws Exception {
99 this.server.setHttpService(new HeaderDumpService());
100
101 GetMethod method = new GetMethod("/");
102 method.setRequestHeader(new Header("addRequestHeader(Header)","True"));
103 method.setRequestHeader("addRequestHeader(String,String)","Also True");
104 try {
105 this.client.executeMethod(method);
106 String s = method.getResponseBodyAsString();
107 assertTrue(s.indexOf("name=\"addrequestheader(header)\";value=\"True\"") >= 0);
108 assertTrue(s.indexOf("name=\"addrequestheader(string,string)\";value=\"Also True\"") >= 0);
109 } finally {
110 method.releaseConnection();
111 }
112 }
113
114 /***
115 * Test {@link HttpMethod#removeRequestHeader}.
116 */
117 public void testRemoveRequestHeader() throws Exception {
118 this.server.setHttpService(new HeaderDumpService());
119
120 GetMethod method = new GetMethod("/");
121 method.setRequestHeader(new Header("XXX-A-HEADER","true"));
122 method.removeRequestHeader("XXX-A-HEADER");
123
124 try {
125 this.client.executeMethod(method);
126 String s = method.getResponseBodyAsString();
127 assertTrue(!(s.indexOf("xxx-a-header") >= 0));
128 } finally {
129 method.releaseConnection();
130 }
131 }
132
133 /***
134 * Test {@link HttpMethod#setRequestHeader}.
135 */
136 public void testOverwriteRequestHeader() throws Exception {
137 this.server.setHttpService(new HeaderDumpService());
138
139 GetMethod method = new GetMethod("/");
140 method.setRequestHeader(new Header("xxx-a-header","one"));
141 method.setRequestHeader("XXX-A-HEADER","two");
142
143 try {
144 this.client.executeMethod(method);
145 String s = method.getResponseBodyAsString();
146 assertTrue(s.indexOf("name=\"xxx-a-header\";value=\"two\"") >= 0);
147 } finally {
148 method.releaseConnection();
149 }
150 }
151
152 /***
153 * Test {@link HttpMethod#getResponseHeader}.
154 */
155 public void testGetResponseHeader() throws Exception {
156 this.server.setHttpService(new HeaderDumpService());
157
158 GetMethod method = new GetMethod("/");
159 try {
160 this.client.executeMethod(method);
161 Header h = new Header("HeaderSetByServlet","Yes");
162 assertEquals(h, method.getResponseHeader("headersetbyservlet"));
163 } finally {
164 method.releaseConnection();
165 }
166 }
167
168 /***
169 * Test {@link HttpMethodBase.addHostRequestHeader}.
170 */
171 public void testHostRequestHeader() throws Exception {
172 this.server.setHttpService(new HeaderDumpService());
173
174 String hostname = this.server.getLocalAddress();
175 int port = this.server.getLocalPort();
176
177 InetAddress addr = InetAddress.getByName(hostname);
178 String ip = addr.getHostAddress();
179
180 GetMethod get = new GetMethod("/");
181
182
183
184
185
186 this.client.getHostConfiguration().setHost(ip, port);
187 try {
188 this.client.executeMethod(get);
189 Header hostHeader = get.getRequestHeader("Host");
190 assertTrue(hostHeader != null);
191 if (port == Protocol.getProtocol("http").getDefaultPort()) {
192
193 assertTrue(hostHeader.getValue().equals(ip));
194 } else {
195 assertTrue(hostHeader.getValue().equals(ip + ":" + port));
196 }
197 } finally {
198 get.releaseConnection();
199 }
200
201 get = new GetMethod("/");
202
203
204
205
206
207 this.client.getHostConfiguration().setHost(hostname, port);
208 try {
209 this.client.executeMethod(get);
210 Header hostHeader = get.getRequestHeader("Host");
211 assertTrue(hostHeader != null);
212 if (port == Protocol.getProtocol("http").getDefaultPort()) {
213
214 assertTrue(hostHeader.getValue().equals(hostname));
215 } else {
216 assertTrue(hostHeader.getValue().equals(hostname + ":" + port));
217 }
218 } finally {
219 get.releaseConnection();
220 }
221 }
222 }
223