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.hc.core5.http.impl;
29  
30  import org.apache.hc.core5.http.ContentLengthStrategy;
31  import org.apache.hc.core5.http.HttpMessage;
32  import org.apache.hc.core5.http.NotImplementedException;
33  import org.apache.hc.core5.http.ProtocolException;
34  import org.apache.hc.core5.http.ProtocolVersion;
35  import org.apache.hc.core5.http.message.BasicHeader;
36  import org.apache.hc.core5.http.message.HeaderGroup;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  public class TestDefaultContentLengthStrategy {
41  
42      static class TestHttpMessage extends HeaderGroup implements HttpMessage {
43  
44          private static final long serialVersionUID = 1L;
45  
46          @Override
47          public ProtocolVersion getVersion() {
48              return null;
49          }
50  
51          @Override
52          public void addHeader(final String name, final Object value) {
53              addHeader(new BasicHeader(name, value));
54          }
55  
56          @Override
57          public void setHeader(final String name, final Object value) {
58              setHeader(new BasicHeader(name, value));
59          }
60  
61          @Override
62          public void setVersion(final ProtocolVersion version) {
63          }
64  
65      }
66  
67      @Test
68      public void testEntityWithChunkTransferEncoding() throws Exception {
69          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
70          final HttpMessage message = new TestHttpMessage();
71          message.addHeader("Transfer-Encoding", "Chunked");
72  
73          Assert.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
74      }
75  
76      @Test(expected = NotImplementedException.class)
77      public void testEntityWithIdentityTransferEncoding() throws Exception {
78          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
79          final HttpMessage message = new TestHttpMessage();
80          message.addHeader("Transfer-Encoding", "Identity");
81          lenStrategy.determineLength(message);
82      }
83  
84      @Test(expected=ProtocolException.class)
85      public void testEntityWithInvalidTransferEncoding() throws Exception {
86          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
87          final HttpMessage message = new TestHttpMessage();
88          message.addHeader("Transfer-Encoding", "whatever");
89          lenStrategy.determineLength(message);
90      }
91  
92      @Test
93      public void testEntityWithContentLength() throws Exception {
94          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
95          final HttpMessage message = new TestHttpMessage();
96          message.addHeader("Content-Length", "100");
97          Assert.assertEquals(100, lenStrategy.determineLength(message));
98      }
99  
100     @Test(expected=ProtocolException.class)
101     public void testEntityWithInvalidContentLength() throws Exception {
102         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
103         final HttpMessage message = new TestHttpMessage();
104         message.addHeader("Content-Length", "whatever");
105         lenStrategy.determineLength(message);
106     }
107 
108     @Test(expected=ProtocolException.class)
109     public void testEntityWithNegativeContentLength() throws Exception {
110         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
111         final HttpMessage message = new TestHttpMessage();
112         message.addHeader("Content-Length", "-10");
113         lenStrategy.determineLength(message);
114     }
115 
116     @Test
117     public void testEntityNoContentDelimiter() throws Exception {
118         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
119         final HttpMessage message = new TestHttpMessage();
120         Assert.assertEquals(ContentLengthStrategy.UNDEFINED, lenStrategy.determineLength(message));
121     }
122 
123 }
124