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.entity.ContentLengthStrategy;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  public class TestLaxContentLengthStrategy {
36  
37      @Test
38      public void testEntityWithTransferEncoding() throws Exception {
39          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
40          final HttpMessage message = new DummyHttpMessage();
41  
42          message.addHeader("Content-Type", "unknown");
43          message.addHeader("Transfer-Encoding", "identity, chunked");
44          message.addHeader("Content-Length", "plain wrong");
45          Assert.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
46      }
47  
48      @Test
49      public void testEntityWithIdentityTransferEncoding() throws Exception {
50          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
51          final HttpMessage message = new DummyHttpMessage();
52  
53          message.addHeader("Content-Type", "unknown");
54          message.addHeader("Transfer-Encoding", "identity");
55          message.addHeader("Content-Length", "plain wrong");
56          Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
57      }
58  
59      @Test
60      public void testEntityWithUnsupportedTransferEncoding() throws Exception {
61          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
62          final HttpMessage message = new DummyHttpMessage();
63  
64          message.addHeader("Content-Type", "unknown");
65          message.addHeader("Transfer-Encoding", "whatever; param=value, chunked");
66          message.addHeader("Content-Length", "plain wrong");
67          Assert.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
68      }
69  
70      @Test
71      public void testChunkedTransferEncodingMustBeLast() throws Exception {
72          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
73          final HttpMessage message = new DummyHttpMessage();
74  
75          message.addHeader("Content-Type", "unknown");
76          message.addHeader("Transfer-Encoding", "chunked, identity");
77          message.addHeader("Content-Length", "plain wrong");
78          Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
79      }
80  
81      @Test
82      public void testEntityWithContentLength() throws Exception {
83          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
84          final HttpMessage message = new DummyHttpMessage();
85  
86          message.addHeader("Content-Type", "unknown");
87          message.addHeader("Content-Length", "0");
88          Assert.assertEquals(0, lenStrategy.determineLength(message));
89      }
90  
91      @Test
92      public void testEntityWithMultipleContentLength() throws Exception {
93          final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
94          final HttpMessage message = new DummyHttpMessage();
95  
96          message.addHeader("Content-Type", "unknown");
97          message.addHeader("Content-Length", "0");
98          message.addHeader("Content-Length", "0");
99          message.addHeader("Content-Length", "1");
100         Assert.assertEquals(1, lenStrategy.determineLength(message));
101     }
102 
103     @Test
104     public void testEntityWithMultipleContentLengthSomeWrong() throws Exception {
105         final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
106         final HttpMessage message = new DummyHttpMessage();
107 
108         message.addHeader("Content-Type", "unknown");
109         message.addHeader("Content-Length", "1");
110         message.addHeader("Content-Length", "yyy");
111         message.addHeader("Content-Length", "xxx");
112         Assert.assertEquals(1, lenStrategy.determineLength(message));
113     }
114 
115     @Test
116     public void testEntityWithMultipleContentLengthAllWrong() throws Exception {
117         final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
118         final HttpMessage message = new DummyHttpMessage();
119 
120         message.addHeader("Content-Type", "unknown");
121         message.addHeader("Content-Length", "yyy");
122         message.addHeader("Content-Length", "xxx");
123         Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
124     }
125 
126     @Test
127     public void testEntityWithInvalidContentLength() throws Exception {
128         final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
129         final HttpMessage message = new DummyHttpMessage();
130 
131         message.addHeader("Content-Type", "unknown");
132         message.addHeader("Content-Length", "xxx");
133         Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
134     }
135 
136     @Test
137     public void testEntityNeitherContentLengthNorTransferEncoding() throws Exception {
138         final ContentLengthStrategy lenStrategy = new LaxContentLengthStrategy();
139         final HttpMessage message = new DummyHttpMessage();
140 
141         // lenient mode
142         Assert.assertEquals(ContentLengthStrategy.IDENTITY, lenStrategy.determineLength(message));
143     }
144 
145 }
146