View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.entity;
29  
30  import org.apache.http.HttpMessage;
31  import org.apache.http.HttpVersion;
32  import org.apache.http.ProtocolException;
33  import org.apache.http.entity.ContentLengthStrategy;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  public class TestStrictContentLengthStrategy {
38  
39      @Test
40      public void testEntityWithChunkTransferEncoding() throws Exception {
41          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
42          final HttpMessage message = new DummyHttpMessage();
43          message.addHeader("Transfer-Encoding", "Chunked");
44  
45          Assert.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
46      }
47  
48      @Test
49      public void testEntityWithIdentityTransferEncoding() throws Exception {
50          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
51          final HttpMessage message = new DummyHttpMessage();
52          message.addHeader("Transfer-Encoding", "Identity");
53  
54          Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
55      }
56  
57      @Test(expected=ProtocolException.class)
58      public void testEntityWithInvalidTransferEncoding() throws Exception {
59          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
60          final HttpMessage message = new DummyHttpMessage();
61          message.addHeader("Transfer-Encoding", "whatever");
62          lenStrategy.determineLength(message);
63      }
64  
65      @Test(expected=ProtocolException.class)
66      public void testEntityWithInvalidChunkEncodingAndHTTP10() throws Exception {
67          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
68          final HttpMessage message = new DummyHttpMessage(HttpVersion.HTTP_1_0);
69          message.addHeader("Transfer-Encoding", "chunked");
70          lenStrategy.determineLength(message);
71      }
72  
73      @Test
74      public void testEntityWithContentLength() throws Exception {
75          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
76          final HttpMessage message = new DummyHttpMessage();
77          message.addHeader("Content-Length", "100");
78          Assert.assertEquals(100, lenStrategy.determineLength(message));
79      }
80  
81      @Test(expected=ProtocolException.class)
82      public void testEntityWithInvalidContentLength() throws Exception {
83          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
84          final HttpMessage message = new DummyHttpMessage();
85          message.addHeader("Content-Length", "whatever");
86          lenStrategy.determineLength(message);
87      }
88  
89      @Test(expected=ProtocolException.class)
90      public void testEntityWithNegativeContentLength() throws Exception {
91          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
92          final HttpMessage message = new DummyHttpMessage();
93          message.addHeader("Content-Length", "-10");
94          lenStrategy.determineLength(message);
95      }
96  
97      @Test
98      public void testEntityNoContentDelimiter() throws Exception {
99          final ContentLengthStrategy lenStrategy = new StrictContentLengthStrategy();
100         final HttpMessage message = new DummyHttpMessage();
101         Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
102     }
103 
104 }
105