1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.6 2003/01/23 22:48:27 jsdever Exp $
3 * $Revision: 1.6 $
4 * $Date: 2003/01/23 22:48:27 $
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63 package org.apache.commons.httpclient;
64
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68
69 /***
70 * Tests for reading response headers.
71 *
72 * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
73 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
74 * @version $Id: TestResponseHeaders.java,v 1.6 2003/01/23 22:48:27 jsdever Exp $
75 */
76 public class TestResponseHeaders extends TestCase {
77
78 // ------------------------------------------------------------ Constructor
79 public TestResponseHeaders(String testName) {
80 super(testName);
81 }
82
83 // ------------------------------------------------------------------- Main
84 public static void main(String args[]) {
85 String[] testCaseName = {TestResponseHeaders.class.getName()};
86 junit.textui.TestRunner.main(testCaseName);
87 }
88
89 // ------------------------------------------------------- TestCase Methods
90 public static Test suite() {
91 return new TestSuite(TestResponseHeaders.class);
92 }
93
94
95
96 /***
97 * Simple extension of HttpMethodBase.
98 */
99 private class SimpleHttpMethod extends HttpMethodBase {
100 public SimpleHttpMethod() {
101 super("");
102 }
103 public String getName() {
104 return "simple";
105 }
106 }
107
108 // ----------------------------------------------------------- Test Methods
109 public void testHeaders() throws Exception {
110 String body = "XXX\r\nYYY\r\nZZZ";
111 String headers =
112 "HTTP/1.1 200 OK\r\n" +
113 "Connection: close\r\n" +
114 "Content-Length: " + body.length() + "\r\n" +
115 "Content-Type: text/xml; charset=utf-8\r\n" +
116 "Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" +
117 "Server: UserLand Frontier/7.0-WinNT\r\n";
118 HttpState state = new HttpState();
119 HttpMethod method = new SimpleHttpMethod();
120 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
121 method.execute(state, conn);
122 assertEquals("close", method.getResponseHeader("Connection").getValue());
123 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
124 assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue());
125 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
126 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
127 }
128
129 /***
130 * Tests that having a duplicate content length causes no problems.
131 */
132 public void testDuplicateContentLength() throws Exception {
133
134 String body = "XXX\r\nYYY\r\nZZZ";
135 String headers =
136 "HTTP/1.1 200 OK\r\n" +
137 "Content-Length: " + body.length() + "\r\n" +
138 "Content-Length: " + body.length() + "\r\n";
139 HttpState state = new HttpState();
140 HttpMethod method = new SimpleHttpMethod();
141 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
142 method.execute(state, conn);
143 assertNotNull( "Response body is null.", method.getResponseBodyAsStream() );
144
145 }
146
147 public void testNullHeaders() throws Exception {
148 String body = "XXX\r\nYYY\r\nZZZ";
149 String headers =
150 "HTTP/1.1 200 OK\r\n" +
151 "Content-Length: " + body.length() + "\r\n";
152 HttpState state = new HttpState();
153 HttpMethod method = new SimpleHttpMethod();
154 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
155 method.execute(state, conn);
156 assertEquals(null, method.getResponseHeader(null));
157 assertEquals(null, method.getResponseHeader("bogus"));
158 }
159
160 public void testFoldedHeaders() throws Exception {
161 String body = "XXX\r\nYYY\r\nZZZ";
162 String headers =
163 "HTTP/1.1 200 OK\r\n" +
164 "Connection: close\r\n" +
165 "Content-Length: " + body.length() + "\r\n" +
166 "Content-Type: text/xml; charset=utf-8\r\n" +
167 "\tboundary=XXXX\r\n" +
168 "Date: Wed, 28 Mar 2001\r\n" +
169 " 05:05:04 GMT\r\n" +
170 "Server: UserLand Frontier/7.0-WinNT\r\n";
171 HttpState state = new HttpState();
172 HttpMethod method = new SimpleHttpMethod();
173 SimpleHttpConnection conn = new SimpleHttpConnection(headers, body);
174 method.execute(state, conn);
175 assertEquals("close", method.getResponseHeader("Connection").getValue());
176 assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
177 assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue());
178 assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
179 assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
180 assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1);
181 }
182 }
This page was automatically generated by Maven