KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > params > TestHttpParams


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: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  */

29
30 package org.apache.commons.httpclient.params;
31
32 import java.io.IOException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.apache.commons.httpclient.Header;
39 import org.apache.commons.httpclient.HostConfiguration;
40 import org.apache.commons.httpclient.HttpClientTestBase;
41 import org.apache.commons.httpclient.HttpStatus;
42 import org.apache.commons.httpclient.HttpVersion;
43 import org.apache.commons.httpclient.methods.GetMethod;
44 import org.apache.commons.httpclient.params.HostParams;
45 import org.apache.commons.httpclient.protocol.Protocol;
46 import org.apache.commons.httpclient.server.HttpService;
47 import org.apache.commons.httpclient.server.SimpleRequest;
48 import org.apache.commons.httpclient.server.SimpleResponse;
49
50 /**
51  * HTTP preference framework tests.
52  *
53  * @author Oleg Kalnichevski
54  *
55  * @version $Revision: 480424 $
56  */

57 public class TestHttpParams extends HttpClientTestBase {
58
59     // ------------------------------------------------------------ Constructor
60
public TestHttpParams(final String JavaDoc testName) throws IOException JavaDoc {
61         super(testName);
62     }
63
64     // ------------------------------------------------------------------- Main
65
public static void main(String JavaDoc args[]) {
66         String JavaDoc[] testCaseName = { TestHttpParams.class.getName() };
67         junit.textui.TestRunner.main(testCaseName);
68     }
69
70     // ------------------------------------------------------- TestCase Methods
71

72     public static Test suite() {
73         return new TestSuite(TestHttpParams.class);
74     }
75
76     private class SimpleService implements HttpService {
77
78         public SimpleService() {
79             super();
80         }
81
82         public boolean process(final SimpleRequest request, final SimpleResponse response)
83             throws IOException JavaDoc
84         {
85             String JavaDoc uri = request.getRequestLine().getUri();
86             HttpVersion httpversion = request.getRequestLine().getHttpVersion();
87             
88             if ("/miss/".equals(uri)) {
89                 response.setStatusLine(httpversion, HttpStatus.SC_MOVED_TEMPORARILY);
90                 response.addHeader(new Header("Location", "/hit/"));
91                 response.setBodyString("Missed!");
92             } else if ("/hit/".equals(uri)) {
93                 response.setStatusLine(httpversion, HttpStatus.SC_OK);
94                 response.setBodyString("Hit!");
95             } else {
96                 response.setStatusLine(httpversion, HttpStatus.SC_NOT_FOUND);
97                 response.setBodyString(uri + " not found");
98             }
99             return true;
100         }
101     }
102
103     public void testDefaultHeaders() throws IOException JavaDoc {
104         this.server.setHttpService(new SimpleService());
105
106         ArrayList JavaDoc defaults = new ArrayList JavaDoc();
107         defaults.add(new Header("this-header", "value1"));
108         defaults.add(new Header("that-header", "value1"));
109         defaults.add(new Header("that-header", "value2"));
110         defaults.add(new Header("User-Agent", "test"));
111
112         HostConfiguration hostconfig = new HostConfiguration();
113         hostconfig.setHost(
114                 this.server.getLocalAddress(),
115                 this.server.getLocalPort(),
116                 Protocol.getProtocol("http"));
117         hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS, defaults);
118         
119         GetMethod httpget = new GetMethod("/miss/");
120         try {
121             this.client.executeMethod(hostconfig, httpget);
122         } finally {
123             httpget.releaseConnection();
124         }
125         assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
126         Header[] thisheader = httpget.getRequestHeaders("this-header");
127         assertEquals(1, thisheader.length);
128         Header[] thatheader = httpget.getRequestHeaders("that-header");
129         assertEquals(2, thatheader.length);
130         assertEquals("test", httpget.getRequestHeader("User-Agent").getValue());
131     }
132
133     public void testDefaults() throws IOException JavaDoc {
134         this.server.setHttpService(new SimpleService());
135
136         this.client.getParams().setParameter(HttpMethodParams.USER_AGENT, "test");
137         HostConfiguration hostconfig = new HostConfiguration();
138         hostconfig.setHost(
139                 this.server.getLocalAddress(),
140                 this.server.getLocalPort(),
141                 Protocol.getProtocol("http"));
142         
143         GetMethod httpget = new GetMethod("/miss/");
144         try {
145             this.client.executeMethod(hostconfig, httpget);
146         } finally {
147             httpget.releaseConnection();
148         }
149         assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
150         assertEquals("test", httpget.getRequestHeader("User-Agent").getValue());
151         assertEquals("test", httpget.getParams().
152                 getParameter(HttpMethodParams.USER_AGENT));
153         assertEquals("test", hostconfig.getParams().
154                 getParameter(HttpMethodParams.USER_AGENT));
155         assertEquals("test", client.getParams().
156                 getParameter(HttpMethodParams.USER_AGENT));
157     }
158 }
159
Popular Tags