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/TestStrictContentLengthStrategy.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.HttpVersion;
039: import org.apache.http.ProtocolException;
040: import org.apache.http.entity.ContentLengthStrategy;
041: import org.apache.http.mockup.HttpMessageMockup;
042: import org.apache.http.params.CoreProtocolPNames;
043:
044: public class TestStrictContentLengthStrategy extends TestCase {
045:
046: public TestStrictContentLengthStrategy(String testName) {
047: super (testName);
048: }
049:
050: // ------------------------------------------------------- TestCase Methods
051:
052: public static Test suite() {
053: return new TestSuite(TestStrictContentLengthStrategy.class);
054: }
055:
056: // ------------------------------------------------------------------- Main
057: public static void main(String args[]) {
058: String[] testCaseName = { TestStrictContentLengthStrategy.class
059: .getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: public void testEntityWithChunkTransferEncoding() throws Exception {
064: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
065: HttpMessage message = new HttpMessageMockup();
066: message.addHeader("Transfer-Encoding", "Chunked");
067:
068: assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy
069: .determineLength(message));
070: }
071:
072: public void testEntityWithIdentityTransferEncoding()
073: throws Exception {
074: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
075: HttpMessage message = new HttpMessageMockup();
076: message.addHeader("Transfer-Encoding", "Identity");
077:
078: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
079: .determineLength(message));
080: }
081:
082: public void testEntityWithInvalidTransferEncoding()
083: throws Exception {
084: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
085: HttpMessage message = new HttpMessageMockup();
086: message.addHeader("Transfer-Encoding", "whatever");
087:
088: try {
089: lenStrategy.determineLength(message);
090: fail("ProtocolException should have been thrown");
091: } catch (ProtocolException ex) {
092: // expected
093: }
094: }
095:
096: public void testEntityWithInvalidChunkEncodingAndHTTP10()
097: throws Exception {
098: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
099: HttpMessage message = new HttpMessageMockup();
100: message.getParams().setParameter(
101: CoreProtocolPNames.PROTOCOL_VERSION,
102: HttpVersion.HTTP_1_0);
103: message.addHeader("Transfer-Encoding", "chunked");
104:
105: try {
106: lenStrategy.determineLength(message);
107: fail("ProtocolException should have been thrown");
108: } catch (ProtocolException ex) {
109: // expected
110: }
111: }
112:
113: public void testEntityWithContentLength() throws Exception {
114: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
115: HttpMessage message = new HttpMessageMockup();
116: message.addHeader("Content-Length", "100");
117: assertEquals(100, lenStrategy.determineLength(message));
118: }
119:
120: public void testEntityWithInvalidContentLength() throws Exception {
121: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
122: HttpMessage message = new HttpMessageMockup();
123: message.addHeader("Content-Length", "whatever");
124:
125: try {
126: lenStrategy.determineLength(message);
127: fail("ProtocolException should have been thrown");
128: } catch (ProtocolException ex) {
129: // expected
130: }
131: }
132:
133: public void testEntityNoContentDelimiter() throws Exception {
134: ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
135: HttpMessage message = new HttpMessageMockup();
136: assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy
137: .determineLength(message));
138: }
139:
140: }
|