001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestHeaderOps.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient;
031:
032: import java.io.IOException;
033: import java.net.InetAddress;
034: import java.util.Iterator;
035:
036: import junit.framework.Test;
037: import junit.framework.TestSuite;
038:
039: import org.apache.commons.httpclient.methods.GetMethod;
040: import org.apache.commons.httpclient.protocol.Protocol;
041: import org.apache.commons.httpclient.server.HttpService;
042: import org.apache.commons.httpclient.server.SimpleRequest;
043: import org.apache.commons.httpclient.server.SimpleResponse;
044:
045: /**
046: * @author Rodney Waldhoff
047: * @version $Id: TestHeaderOps.java 480424 2006-11-29 05:56:49Z bayard $
048: */
049: public class TestHeaderOps extends HttpClientTestBase {
050:
051: public TestHeaderOps(String testName) throws Exception {
052: super (testName);
053: }
054:
055: public static Test suite() {
056: TestSuite suite = new TestSuite(TestHeaderOps.class);
057: return suite;
058: }
059:
060: public static void main(String args[]) {
061: String[] testCaseName = { TestHeaderOps.class.getName() };
062: junit.textui.TestRunner.main(testCaseName);
063: }
064:
065: // ------------------------------------------------------------------ Tests
066:
067: class HeaderDumpService implements HttpService {
068:
069: public HeaderDumpService() {
070: super ();
071: }
072:
073: public boolean process(final SimpleRequest request,
074: final SimpleResponse response) throws IOException {
075: HttpVersion httpversion = request.getRequestLine()
076: .getHttpVersion();
077: response.setStatusLine(httpversion, HttpStatus.SC_OK);
078: response
079: .addHeader(new Header("Content-Type", "text/plain"));
080: response.addHeader(new Header("HeaderSetByServlet", "Yes"));
081:
082: StringBuffer buffer = new StringBuffer();
083: buffer.append("Request headers: \r\n");
084: for (Iterator i = request.getHeaderIterator(); i.hasNext();) {
085: Header header = (Header) i.next();
086: buffer.append("name=\"");
087: buffer.append(header.getName().toLowerCase());
088: buffer.append("\";value=\"");
089: buffer.append(header.getValue());
090: buffer.append("\"\r\n");
091: }
092: response.setBodyString(buffer.toString());
093: return true;
094: }
095: }
096:
097: /**
098: * Test {@link HttpMethod#addRequestHeader}.
099: */
100: public void testAddRequestHeader() throws Exception {
101: this .server.setHttpService(new HeaderDumpService());
102:
103: GetMethod method = new GetMethod("/");
104: method.setRequestHeader(new Header("addRequestHeader(Header)",
105: "True"));
106: method.setRequestHeader("addRequestHeader(String,String)",
107: "Also True");
108: try {
109: this .client.executeMethod(method);
110: String s = method.getResponseBodyAsString();
111: assertTrue(s
112: .indexOf("name=\"addrequestheader(header)\";value=\"True\"") >= 0);
113: assertTrue(s
114: .indexOf("name=\"addrequestheader(string,string)\";value=\"Also True\"") >= 0);
115: } finally {
116: method.releaseConnection();
117: }
118: }
119:
120: /**
121: * Test {@link HttpMethod#removeRequestHeader}.
122: */
123: public void testRemoveRequestHeader() throws Exception {
124: this .server.setHttpService(new HeaderDumpService());
125:
126: GetMethod method = new GetMethod("/");
127: method.setRequestHeader(new Header("XXX-A-HEADER", "true"));
128: method.removeRequestHeader("XXX-A-HEADER");
129:
130: try {
131: this .client.executeMethod(method);
132: String s = method.getResponseBodyAsString();
133: assertTrue(!(s.indexOf("xxx-a-header") >= 0));
134: } finally {
135: method.releaseConnection();
136: }
137: }
138:
139: /**
140: * Test {@link HttpMethod#setRequestHeader}.
141: */
142: public void testOverwriteRequestHeader() throws Exception {
143: this .server.setHttpService(new HeaderDumpService());
144:
145: GetMethod method = new GetMethod("/");
146: method.setRequestHeader(new Header("xxx-a-header", "one"));
147: method.setRequestHeader("XXX-A-HEADER", "two");
148:
149: try {
150: this .client.executeMethod(method);
151: String s = method.getResponseBodyAsString();
152: assertTrue(s.indexOf("name=\"xxx-a-header\";value=\"two\"") >= 0);
153: } finally {
154: method.releaseConnection();
155: }
156: }
157:
158: /**
159: * Test {@link HttpMethod#getResponseHeader}.
160: */
161: public void testGetResponseHeader() throws Exception {
162: this .server.setHttpService(new HeaderDumpService());
163:
164: GetMethod method = new GetMethod("/");
165: try {
166: this .client.executeMethod(method);
167: Header h = new Header("HeaderSetByServlet", "Yes");
168: assertEquals(h, method
169: .getResponseHeader("headersetbyservlet"));
170: } finally {
171: method.releaseConnection();
172: }
173: }
174:
175: /**
176: * Test {@link HttpMethodBase.addHostRequestHeader}.
177: */
178: public void testHostRequestHeader() throws Exception {
179: this .server.setHttpService(new HeaderDumpService());
180:
181: String hostname = this .server.getLocalAddress();
182: int port = this .server.getLocalPort();
183:
184: InetAddress addr = InetAddress.getByName(hostname);
185: String ip = addr.getHostAddress();
186:
187: GetMethod get = new GetMethod("/");
188:
189: // Open connection using IP. Host header should be sent
190: // Note: RFC 2616 is somewhat unclear on whether a host should
191: // be sent in this context - however, both Mozilla and IE send
192: // the header for an IP address, instead of sending blank.
193: this .client.getHostConfiguration().setHost(ip, port);
194: try {
195: this .client.executeMethod(get);
196: Header hostHeader = get.getRequestHeader("Host");
197: assertTrue(hostHeader != null);
198: if (port == Protocol.getProtocol("http").getDefaultPort()) {
199: // no port information should be in the value
200: assertTrue(hostHeader.getValue().equals(ip));
201: } else {
202: assertTrue(hostHeader.getValue()
203: .equals(ip + ":" + port));
204: }
205: } finally {
206: get.releaseConnection();
207: }
208:
209: get = new GetMethod("/");
210: // Open connection using Host. Host header should
211: // contain this value (this test will fail if DNS
212: // is not available. Additionally, if the port is
213: // something other that 80, then the port value
214: // should also be present in the header.
215: this .client.getHostConfiguration().setHost(hostname, port);
216: try {
217: this .client.executeMethod(get);
218: Header hostHeader = get.getRequestHeader("Host");
219: assertTrue(hostHeader != null);
220: if (port == Protocol.getProtocol("http").getDefaultPort()) {
221: // no port information should be in the value
222: assertTrue(hostHeader.getValue().equals(hostname));
223: } else {
224: assertTrue(hostHeader.getValue().equals(
225: hostname + ":" + port));
226: }
227: } finally {
228: get.releaseConnection();
229: }
230: }
231: }
|