~ubuntu-branches/ubuntu/lucid/commons-httpclient/lucid

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/httpclient/TestWebappHeaders.java

  • Committer: Bazaar Package Importer
  • Author(s): Barry Hawkins
  • Date: 2005-11-25 13:12:23 UTC
  • Revision ID: james.westby@ubuntu.com-20051125131223-2g7eyo21pqgrohpo
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 * ====================================================================
 
6
 *
 
7
 *  Copyright 1999-2004 The Apache Software Foundation
 
8
 *
 
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
 
12
 *
 
13
 *      http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
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
 * ====================================================================
 
21
 *
 
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/>.
 
26
 *
 
27
 * [Additional notices, if required by prior licensing conditions]
 
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
    // ------------------------------------------------------------------ Tests
 
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
        // 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);
 
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
        // Tomcat 4 at least converts the header name to lower case
 
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
        // Tomcat 4 at least converts the header name to lower case
 
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
        // 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());
 
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
        // reset 
 
178
        get.recycle();
 
179
        get.setPath("/" + getWebappContext());
 
180
 
 
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());
 
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
            // no port information should be in the value
 
197
            assertTrue(hostHeader.getValue().equals(hostname));
 
198
        } else {
 
199
            assertTrue(hostHeader.getValue().equals(hostname + ":" + getPort()));
 
200
        }
 
201
    }
 
202
 
 
203
}
 
204