1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/params/TestHttpParams.java,v 1.2 2004/09/15 20:45:48 olegk Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/09/15 20:45:48 $
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.params;
32  
33  import java.io.IOException;
34  import java.util.ArrayList;
35  
36  import junit.framework.Test;
37  import junit.framework.TestSuite;
38  
39  import org.apache.commons.httpclient.Header;
40  import org.apache.commons.httpclient.HostConfiguration;
41  import org.apache.commons.httpclient.HttpClientTestBase;
42  import org.apache.commons.httpclient.HttpStatus;
43  import org.apache.commons.httpclient.HttpVersion;
44  import org.apache.commons.httpclient.methods.GetMethod;
45  import org.apache.commons.httpclient.params.HostParams;
46  import org.apache.commons.httpclient.protocol.Protocol;
47  import org.apache.commons.httpclient.server.HttpService;
48  import org.apache.commons.httpclient.server.SimpleRequest;
49  import org.apache.commons.httpclient.server.SimpleResponse;
50  
51  /***
52   * HTTP preference framework tests.
53   *
54   * @author Oleg Kalnichevski
55   * 
56   * @version $Revision: 1.2 $
57   */
58  public class TestHttpParams extends HttpClientTestBase {
59  
60      // ------------------------------------------------------------ Constructor
61      public TestHttpParams(String testName) {
62          super(testName);
63      }
64  
65      // ------------------------------------------------------------------- Main
66      public static void main(String args[]) {
67          String[] testCaseName = { TestHttpParams.class.getName() };
68          junit.textui.TestRunner.main(testCaseName);
69      }
70  
71      // ------------------------------------------------------- TestCase Methods
72  
73      public static Test suite() {
74          return new TestSuite(TestHttpParams.class);
75      }
76  
77      private class SimpleService implements HttpService {
78  
79          public SimpleService() {
80              super();
81          }
82  
83          public boolean process(final SimpleRequest request, final SimpleResponse response)
84              throws IOException
85          {
86              String uri = request.getRequestLine().getUri();  
87          	HttpVersion httpversion = request.getRequestLine().getHttpVersion();
88          	
89          	if ("/miss/".equals(uri)) {
90                  response.setStatusLine(httpversion, HttpStatus.SC_MOVED_TEMPORARILY);
91                  response.addHeader(new Header("Location", "/hit/"));
92                  response.setBodyString("Missed!");
93          	} else if ("/hit/".equals(uri)) {
94                  response.setStatusLine(httpversion, HttpStatus.SC_OK);
95                  response.setBodyString("Hit!");
96          	} else {
97                  response.setStatusLine(httpversion, HttpStatus.SC_NOT_FOUND);
98                  response.setBodyString(uri + " not found");
99          	}
100             return true;
101         }
102     }
103 
104     public void testDefaultHeaders() throws IOException {
105         this.server.setHttpService(new SimpleService());
106 
107         ArrayList defaults = new ArrayList();
108         defaults.add(new Header("this-header", "value1"));
109         defaults.add(new Header("that-header", "value1"));
110         defaults.add(new Header("that-header", "value2"));
111         defaults.add(new Header("User-Agent", "test"));
112 
113         HostConfiguration hostconfig = new HostConfiguration();
114         hostconfig.setHost(
115                 this.server.getLocalAddress(), 
116 	            this.server.getLocalPort(),
117 	            Protocol.getProtocol("http"));
118         hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults);
119         
120         GetMethod httpget = new GetMethod("/miss/");
121         try {
122             this.client.executeMethod(hostconfig, httpget);
123         } finally {
124             httpget.releaseConnection();
125         }
126         assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
127         Header[] thisheader = httpget.getRequestHeaders("this-header");
128         assertEquals(1, thisheader.length);
129         Header[] thatheader = httpget.getRequestHeaders("that-header");
130         assertEquals(2, thatheader.length);
131         assertEquals("test", httpget.getRequestHeader("User-Agent").getValue());
132     }
133 }