001: /*
002: * $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 $
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.params;
031:
032: import java.io.IOException;
033: import java.util.ArrayList;
034:
035: import junit.framework.Test;
036: import junit.framework.TestSuite;
037:
038: import org.apache.commons.httpclient.Header;
039: import org.apache.commons.httpclient.HostConfiguration;
040: import org.apache.commons.httpclient.HttpClientTestBase;
041: import org.apache.commons.httpclient.HttpStatus;
042: import org.apache.commons.httpclient.HttpVersion;
043: import org.apache.commons.httpclient.methods.GetMethod;
044: import org.apache.commons.httpclient.params.HostParams;
045: import org.apache.commons.httpclient.protocol.Protocol;
046: import org.apache.commons.httpclient.server.HttpService;
047: import org.apache.commons.httpclient.server.SimpleRequest;
048: import org.apache.commons.httpclient.server.SimpleResponse;
049:
050: /**
051: * HTTP preference framework tests.
052: *
053: * @author Oleg Kalnichevski
054: *
055: * @version $Revision: 480424 $
056: */
057: public class TestHttpParams extends HttpClientTestBase {
058:
059: // ------------------------------------------------------------ Constructor
060: public TestHttpParams(final String testName) throws IOException {
061: super (testName);
062: }
063:
064: // ------------------------------------------------------------------- Main
065: public static void main(String args[]) {
066: String[] testCaseName = { TestHttpParams.class.getName() };
067: junit.textui.TestRunner.main(testCaseName);
068: }
069:
070: // ------------------------------------------------------- TestCase Methods
071:
072: public static Test suite() {
073: return new TestSuite(TestHttpParams.class);
074: }
075:
076: private class SimpleService implements HttpService {
077:
078: public SimpleService() {
079: super ();
080: }
081:
082: public boolean process(final SimpleRequest request,
083: final SimpleResponse response) throws IOException {
084: String uri = request.getRequestLine().getUri();
085: HttpVersion httpversion = request.getRequestLine()
086: .getHttpVersion();
087:
088: if ("/miss/".equals(uri)) {
089: response.setStatusLine(httpversion,
090: HttpStatus.SC_MOVED_TEMPORARILY);
091: response.addHeader(new Header("Location", "/hit/"));
092: response.setBodyString("Missed!");
093: } else if ("/hit/".equals(uri)) {
094: response.setStatusLine(httpversion, HttpStatus.SC_OK);
095: response.setBodyString("Hit!");
096: } else {
097: response.setStatusLine(httpversion,
098: HttpStatus.SC_NOT_FOUND);
099: response.setBodyString(uri + " not found");
100: }
101: return true;
102: }
103: }
104:
105: public void testDefaultHeaders() throws IOException {
106: this .server.setHttpService(new SimpleService());
107:
108: ArrayList defaults = new ArrayList();
109: defaults.add(new Header("this-header", "value1"));
110: defaults.add(new Header("that-header", "value1"));
111: defaults.add(new Header("that-header", "value2"));
112: defaults.add(new Header("User-Agent", "test"));
113:
114: HostConfiguration hostconfig = new HostConfiguration();
115: hostconfig.setHost(this .server.getLocalAddress(), this .server
116: .getLocalPort(), Protocol.getProtocol("http"));
117: hostconfig.getParams().setParameter(HostParams.DEFAULT_HEADERS,
118: 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[] this header = httpget.getRequestHeaders("this-header");
128: assertEquals(1, this header.length);
129: Header[] thatheader = httpget.getRequestHeaders("that-header");
130: assertEquals(2, thatheader.length);
131: assertEquals("test", httpget.getRequestHeader("User-Agent")
132: .getValue());
133: }
134:
135: public void testDefaults() throws IOException {
136: this .server.setHttpService(new SimpleService());
137:
138: this .client.getParams().setParameter(
139: HttpMethodParams.USER_AGENT, "test");
140: HostConfiguration hostconfig = new HostConfiguration();
141: hostconfig.setHost(this .server.getLocalAddress(), this .server
142: .getLocalPort(), Protocol.getProtocol("http"));
143:
144: GetMethod httpget = new GetMethod("/miss/");
145: try {
146: this .client.executeMethod(hostconfig, httpget);
147: } finally {
148: httpget.releaseConnection();
149: }
150: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
151: assertEquals("test", httpget.getRequestHeader("User-Agent")
152: .getValue());
153: assertEquals("test", httpget.getParams().getParameter(
154: HttpMethodParams.USER_AGENT));
155: assertEquals("test", hostconfig.getParams().getParameter(
156: HttpMethodParams.USER_AGENT));
157: assertEquals("test", client.getParams().getParameter(
158: HttpMethodParams.USER_AGENT));
159: }
160: }
|