2
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappHeaders.java,v 1.10.2.1 2004/02/22 18:21:16 olegk Exp $
3
* $Revision: 1.10.2.1 $
4
* $Date: 2004/02/22 18:21:16 $
5
* ====================================================================
7
* Copyright 1999-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
20
* ====================================================================
22
* This software consists of voluntary contributions made by many
23
* individuals on behalf of the Apache Software Foundation. For more
24
* information on the Apache Software Foundation, please see
25
* <http://www.apache.org/>.
27
* [Additional notices, if required by prior licensing conditions]
31
package org.apache.commons.httpclient;
33
import java.net.InetAddress;
35
import junit.framework.Test;
36
import junit.framework.TestSuite;
38
import org.apache.commons.httpclient.methods.GetMethod;
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.
45
* The webapp should be deployed in the context "httpclienttest"
46
* on a servlet engine running on port 8080 on the localhost
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.
56
* @author Rodney Waldhoff
57
* @version $Id: TestWebappHeaders.java,v 1.10.2.1 2004/02/22 18:21:16 olegk Exp $
59
public class TestWebappHeaders extends TestWebappBase {
61
public TestWebappHeaders(String testName) {
65
public static Test suite() {
66
TestSuite suite = new TestSuite(TestWebappHeaders.class);
70
public static void main(String args[]) {
71
String[] testCaseName = { TestWebappHeaders.class.getName() };
72
junit.textui.TestRunner.main(testCaseName);
75
// ------------------------------------------------------------------ Tests
78
* Test {@link HttpMethod#addRequestHeader}.
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");
87
client.executeMethod(method);
88
} catch (Throwable t) {
90
fail("Unable to execute method : " + t.toString());
92
// Tomcat 4 at least converts the header name to lower case
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);
98
* Test {@link HttpMethod#removeRequestHeader}.
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");
107
client.executeMethod(method);
108
} catch (Throwable t) {
110
fail("Unable to execute method : " + t.toString());
112
// Tomcat 4 at least converts the header name to lower case
113
assertTrue(!(method.getResponseBodyAsString().indexOf("xxx-a-header") >= 0));
117
* Test {@link HttpMethod#addRequestHeader}.
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");
126
client.executeMethod(method);
127
} catch (Throwable t) {
129
fail("Unable to execute method : " + t.toString());
131
// Tomcat 4 at least converts the header name to lower case
132
assertTrue(method.getResponseBodyAsString().indexOf("name=\"xxx-a-header\";value=\"two\"<br>") >= 0);
136
* Test {@link HttpMethod#getResponseHeader}.
138
public void testGetResponseHeader() throws Exception {
139
HttpClient client = createHttpClient();
140
GetMethod method = new GetMethod("/" + getWebappContext() + "/headers");
143
client.executeMethod(method);
144
} catch (Throwable t) {
146
fail("Unable to execute method : " + t.toString());
148
Header h = new Header("HeaderSetByServlet","Yes");
149
assertEquals(h,method.getResponseHeader("headersetbyservlet"));
153
* Test {@link HttpMethodBase.addHostRequestHeader}.
155
public void testHostRequestHeader() throws Exception {
156
InetAddress addr = InetAddress.getByName(getHost());
157
String ip = addr.getHostAddress();
158
String hostname = addr.getHostName();
160
HttpClient client = new HttpClient();
161
GetMethod get = new GetMethod("/" + getWebappContext());
163
// Open connection using IP. Host header should be sent
164
// Note: RFC 2616 is somewhat unclear on whether a host should
165
// be sent in this context - however, both Mozilla and IE send
166
// the header for an IP address, instead of sending blank.
167
client.getHostConfiguration().setHost(ip, getPort(), getProtocol());
169
client.executeMethod(get);
170
} catch (Throwable t) {
172
fail("Unable to execute method : " + t.toString());
174
Header hostHeader = get.getRequestHeader("Host");
175
assertTrue(hostHeader != null);
179
get.setPath("/" + getWebappContext());
181
// Open connection using Host. Host header should
182
// contain this value (this test will fail if DNS
183
// is not available. Additionally, if the port is
184
// something other that 80, then the port value
185
// should also be present in the header.
186
client.getHostConfiguration().setHost(hostname, getPort(), getProtocol());
188
client.executeMethod(get);
189
} catch (Throwable t) {
191
fail("Unable to execute method : " + t.toString());
193
hostHeader = get.getRequestHeader("Host");
194
assertTrue(hostHeader != null);
195
if (getPort() == 80) {
196
// no port information should be in the value
197
assertTrue(hostHeader.getValue().equals(hostname));
199
assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort()));