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