1   /*
2    * $HeadURL: http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHeaderOps.java $
3    * $Revision: 192986 $
4    * $Date: 2005-06-22 15:51:04 -0400 (Wed, 22 Jun 2005) $
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   */
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      // ------------------------------------------------------------------ Tests
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         // Open connection using IP.  Host header should be sent
183         // Note: RFC 2616 is somewhat unclear on whether a host should
184         // be sent in this context - however, both Mozilla and IE send
185         // the header for an IP address, instead of sending blank.
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                 // no port information should be in the value
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         // Open connection using Host.  Host header should
203         // contain this value (this test will fail if DNS
204         // is not available. Additionally, if the port is
205         // something other that 80, then the port value
206         // should also be present in the header.
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                 // no port information should be in the value
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