001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/entity/TestLaxContentLengthStrategy.java $
003: * $Revision: 576073 $
004: * $Date: 2007-09-16 12:53:13 +0200 (Sun, 16 Sep 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with 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,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.impl.entity;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: import org.apache.http.HttpMessage;
038: import org.apache.http.ProtocolException;
039: import org.apache.http.entity.ContentLengthStrategy;
040: import org.apache.http.mockup.HttpMessageMockup;
041: import org.apache.http.params.CoreProtocolPNames;
042:
043: public class TestLaxContentLengthStrategy extends TestCase {
044:
045: public TestLaxContentLengthStrategy(String testName) {
046: super (testName);
047: }
048:
049: // ------------------------------------------------------- TestCase Methods
050:
051: public static Test suite() {
052: return new TestSuite(TestLaxContentLengthStrategy.class);
053: }
054:
055: // ------------------------------------------------------------------- Main
056: public static void main(String args[]) {
057: String[] testCaseName = { TestLaxContentLengthStrategy.class
058: .getName() };
059: junit.textui.TestRunner.main(testCaseName);
060: }
061:
062: public void testEntityWithTransferEncoding() throws Exception {
063: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
064: HttpMessage message = new HttpMessageMockup();
065:
066: // lenient mode
067: message.getParams().setBooleanParameter(
068: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
069: message.addHeader("Content-Type", "unknown");
070: message.addHeader("Transfer-Encoding", "identity, chunked");
071: message.addHeader("Content-Length", "plain wrong");
072: assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy
073: .determineLength(message));
074:
075: // strict mode
076: message.getParams().setBooleanParameter(
077: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
078: assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy
079: .determineLength(message));
080: }
081:
082: public void testEntityWithIdentityTransferEncoding()
083: throws Exception {
084: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
085: HttpMessage message = new HttpMessageMockup();
086:
087: // lenient mode
088: message.getParams().setBooleanParameter(
089: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
090: message.addHeader("Content-Type", "unknown");
091: message.addHeader("Transfer-Encoding", "identity");
092: message.addHeader("Content-Length", "plain wrong");
093: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
094: .determineLength(message));
095: }
096:
097: public void testEntityWithUnsupportedTransferEncoding()
098: throws Exception {
099: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
100: HttpMessage message = new HttpMessageMockup();
101:
102: // lenient mode
103: message.getParams().setBooleanParameter(
104: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
105: message.addHeader("Content-Type", "unknown");
106: message.addHeader("Transfer-Encoding",
107: "whatever; param=value, chunked");
108: message.addHeader("Content-Length", "plain wrong");
109: assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy
110: .determineLength(message));
111:
112: // strict mode
113: message.getParams().setBooleanParameter(
114: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
115: try {
116: lenStrategy.determineLength(message);
117: fail("ProtocolException should have been thrown");
118: } catch (ProtocolException ex) {
119: // expected
120: }
121: }
122:
123: public void testChunkedTransferEncodingMustBeLast()
124: throws Exception {
125: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
126: HttpMessage message = new HttpMessageMockup();
127:
128: // lenient mode
129: message.getParams().setBooleanParameter(
130: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
131: message.addHeader("Content-Type", "unknown");
132: message.addHeader("Transfer-Encoding", "chunked, identity");
133: message.addHeader("Content-Length", "plain wrong");
134: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
135: .determineLength(message));
136:
137: // strict mode
138: message.getParams().setBooleanParameter(
139: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
140: try {
141: lenStrategy.determineLength(message);
142: fail("ProtocolException should have been thrown");
143: } catch (ProtocolException ex) {
144: // expected
145: }
146: }
147:
148: public void testEntityWithContentLength() throws Exception {
149: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
150: HttpMessage message = new HttpMessageMockup();
151:
152: // lenient mode
153: message.getParams().setBooleanParameter(
154: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
155: message.addHeader("Content-Type", "unknown");
156: message.addHeader("Content-Length", "0");
157: assertEquals(0, lenStrategy.determineLength(message));
158: }
159:
160: public void testEntityWithMultipleContentLength() throws Exception {
161: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
162: HttpMessage message = new HttpMessageMockup();
163:
164: // lenient mode
165: message.getParams().setBooleanParameter(
166: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
167: message.addHeader("Content-Type", "unknown");
168: message.addHeader("Content-Length", "0");
169: message.addHeader("Content-Length", "0");
170: message.addHeader("Content-Length", "1");
171: assertEquals(1, lenStrategy.determineLength(message));
172:
173: // strict mode
174: message.getParams().setBooleanParameter(
175: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
176: try {
177: lenStrategy.determineLength(message);
178: fail("ProtocolException should have been thrown");
179: } catch (ProtocolException ex) {
180: // expected
181: }
182: }
183:
184: public void testEntityWithMultipleContentLengthSomeWrong()
185: throws Exception {
186: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
187: HttpMessage message = new HttpMessageMockup();
188:
189: // lenient mode
190: message.getParams().setBooleanParameter(
191: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
192: message.addHeader("Content-Type", "unknown");
193: message.addHeader("Content-Length", "1");
194: message.addHeader("Content-Length", "yyy");
195: message.addHeader("Content-Length", "xxx");
196: assertEquals(1, lenStrategy.determineLength(message));
197:
198: // strict mode
199: message.getParams().setBooleanParameter(
200: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
201: try {
202: lenStrategy.determineLength(message);
203: fail("ProtocolException should have been thrown");
204: } catch (ProtocolException ex) {
205: // expected
206: }
207: }
208:
209: public void testEntityWithMultipleContentLengthAllWrong()
210: throws Exception {
211: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
212: HttpMessage message = new HttpMessageMockup();
213:
214: // lenient mode
215: message.getParams().setBooleanParameter(
216: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
217: message.addHeader("Content-Type", "unknown");
218: message.addHeader("Content-Length", "yyy");
219: message.addHeader("Content-Length", "xxx");
220: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
221: .determineLength(message));
222:
223: // strict mode
224: message.getParams().setBooleanParameter(
225: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
226: try {
227: lenStrategy.determineLength(message);
228: fail("ProtocolException should have been thrown");
229: } catch (ProtocolException ex) {
230: // expected
231: }
232: }
233:
234: public void testEntityWithInvalidContentLength() throws Exception {
235: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
236: HttpMessage message = new HttpMessageMockup();
237:
238: // lenient mode
239: message.getParams().setBooleanParameter(
240: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
241: message.addHeader("Content-Type", "unknown");
242: message.addHeader("Content-Length", "xxx");
243: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
244: .determineLength(message));
245:
246: // strict mode
247: message.getParams().setBooleanParameter(
248: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
249: try {
250: lenStrategy.determineLength(message);
251: fail("ProtocolException should have been thrown");
252: } catch (ProtocolException ex) {
253: // expected
254: }
255: }
256:
257: public void testEntityNeitherContentLengthNorTransferEncoding()
258: throws Exception {
259: ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
260: HttpMessage message = new HttpMessageMockup();
261:
262: // lenient mode
263: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
264: .determineLength(message));
265: }
266:
267: }
|