1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappHeaders.java,v 1.10 2003/03/13 01:23:37 mbecke Exp $
3 * $Revision: 1.10 $
4 * $Date: 2003/03/13 01:23:37 $
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63 package org.apache.commons.httpclient;
64
65 import java.net.InetAddress;
66
67 import junit.framework.Test;
68 import junit.framework.TestSuite;
69
70 import org.apache.commons.httpclient.methods.GetMethod;
71
72 /***
73 * This suite of tests depends upon the httpclienttest webapp,
74 * which is available in the httpclient/src/test-webapp
75 * directory in the CVS tree.
76 * <p>
77 * The webapp should be deployed in the context "httpclienttest"
78 * on a servlet engine running on port 8080 on the localhost
79 * (IP 127.0.0.1).
80 * <p>
81 * You can change the assumed port by setting the
82 * "httpclient.test.localPort" property.
83 * You can change the assumed host by setting the
84 * "httpclient.test.localHost" property.
85 * You can change the assumed context by setting the
86 * "httpclient.test.webappContext" property.
87 *
88 * @author Rodney Waldhoff
89 * @version $Id: TestWebappHeaders.java,v 1.10 2003/03/13 01:23:37 mbecke Exp $
90 */
91 public class TestWebappHeaders extends TestWebappBase {
92
93 public TestWebappHeaders(String testName) {
94 super(testName);
95 }
96
97 public static Test suite() {
98 TestSuite suite = new TestSuite(TestWebappHeaders.class);
99 return suite;
100 }
101
102 public static void main(String args[]) {
103 String[] testCaseName = { TestWebappHeaders.class.getName() };
104 junit.textui.TestRunner.main(testCaseName);
105 }
106
107 // ------------------------------------------------------------------ Tests
108
109 /***
110 * Test {@link HttpMethod#addRequestHeader}.
111 */
112 public void testAddRequestHeader() throws Exception {
113 HttpClient client = createHttpClient();
114 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
115 method.setRequestHeader(new Header("addRequestHeader(Header)","True"));
116 method.setRequestHeader("addRequestHeader(String,String)","Also True");
117
118 try {
119 client.executeMethod(method);
120 } catch (Throwable t) {
121 t.printStackTrace();
122 fail("Unable to execute method : " + t.toString());
123 }
124 // Tomcat 4 at least converts the header name to lower case
125 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(header)\";value=\"True\"<br>") >= 0);
126 assertTrue(method.getResponseBodyAsString().indexOf("name=\"addrequestheader(string,string)\";value=\"Also True\"<br>") >= 0);
127 }
128
129 /***
130 * Test {@link HttpMethod#removeRequestHeader}.
131 */
132 public void testRemoveRequestHeader() throws Exception {
133 HttpClient client = createHttpClient();
134 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
135 method.setRequestHeader(new Header("XXX-A-HEADER","true"));
136 method.removeRequestHeader("XXX-A-HEADER");
137
138 try {
139 client.executeMethod(method);
140 } catch (Throwable t) {
141 t.printStackTrace();
142 fail("Unable to execute method : " + t.toString());
143 }
144 // Tomcat 4 at least converts the header name to lower case
145 assertTrue(!(method.getResponseBodyAsString().indexOf("xxx-a-header") >= 0));
146 }
147
148 /***
149 * Test {@link HttpMethod#addRequestHeader}.
150 */
151 public void testOverwriteRequestHeader() throws Exception {
152 HttpClient client = createHttpClient();
153 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
154 method.setRequestHeader(new Header("xxx-a-header","one"));
155 method.setRequestHeader("XXX-A-HEADER","two");
156
157 try {
158 client.executeMethod(method);
159 } catch (Throwable t) {
160 t.printStackTrace();
161 fail("Unable to execute method : " + t.toString());
162 }
163 // Tomcat 4 at least converts the header name to lower case
164 assertTrue(method.getResponseBodyAsString().indexOf("name=\"xxx-a-header\";value=\"two\"<br>") >= 0);
165 }
166
167 /***
168 * Test {@link HttpMethod#getResponseHeader}.
169 */
170 public void testGetResponseHeader() throws Exception {
171 HttpClient client = createHttpClient();
172 GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
173
174 try {
175 client.executeMethod(method);
176 } catch (Throwable t) {
177 t.printStackTrace();
178 fail("Unable to execute method : " + t.toString());
179 }
180 Header h = new Header("HeaderSetByServlet","Yes");
181 assertEquals(h,method.getResponseHeader("headersetbyservlet"));
182 }
183
184 /***
185 * Test {@link HttpMethodBase.addHostRequestHeader}.
186 */
187 public void testHostRequestHeader() throws Exception {
188 InetAddress addr = InetAddress.getByName(getHost());
189 String ip = addr.getHostAddress();
190 String hostname = addr.getHostName();
191
192 HttpClient client = new HttpClient();
193 GetMethod get = new GetMethod("/" + getWebappContext());
194
195 // Open connection using IP. Host header should be sent
196 // Note: RFC 2616 is somewhat unclear on whether a host should
197 // be sent in this context - however, both Mozilla and IE send
198 // the header for an IP address, instead of sending blank.
199 client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
200 try {
201 client.executeMethod(get);
202 } catch (Throwable t) {
203 t.printStackTrace();
204 fail("Unable to execute method : " + t.toString());
205 }
206 Header hostHeader = get.getRequestHeader("Host");
207 assertTrue(hostHeader != null);
208
209 // reset
210 get.recycle();
211 get.setPath("/" + getWebappContext());
212
213 // Open connection using Host. Host header should
214 // contain this value (this test will fail if DNS
215 // is not available. Additionally, if the port is
216 // something other that 80, then the port value
217 // should also be present in the header.
218 client.getHostConfiguration().setHost(hostname, getPort(), getProtocol());
219 try {
220 client.executeMethod(get);
221 } catch (Throwable t) {
222 t.printStackTrace();
223 fail("Unable to execute method : " + t.toString());
224 }
225 hostHeader = get.getRequestHeader("Host");
226 assertTrue(hostHeader != null);
227 if (getPort() == 80) {
228 // no port information should be in the value
229 assertTrue(hostHeader.getValue().equals(hostname));
230 } else {
231 assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort()));
232 }
233 }
234
235 }
236
This page was automatically generated by Maven