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.jupiter.api.Assertions;
38  import org.junit.jupiter.api.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          Assertions.assertEquals(ContentLengthStrategy.CHUNKED, lenStrategy.determineLength(message));
74      }
75  
76      @Test
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          Assertions.assertThrows(NotImplementedException.class, () ->
82                  lenStrategy.determineLength(message));
83      }
84  
85      @Test
86      public void testEntityWithInvalidTransferEncoding() throws Exception {
87          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
88          final HttpMessage message = new TestHttpMessage();
89          message.addHeader("Transfer-Encoding", "whatever");
90          Assertions.assertThrows(ProtocolException.class, () ->
91                  lenStrategy.determineLength(message));
92      }
93  
94      @Test
95      public void testEntityWithContentLength() throws Exception {
96          final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
97          final HttpMessage message = new TestHttpMessage();
98          message.addHeader("Content-Length", "100");
99          Assertions.assertEquals(100, lenStrategy.determineLength(message));
100     }
101 
102     @Test
103     public void testEntityWithInvalidContentLength() throws Exception {
104         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
105         final HttpMessage message = new TestHttpMessage();
106         message.addHeader("Content-Length", "whatever");
107         Assertions.assertThrows(ProtocolException.class, () ->
108                 lenStrategy.determineLength(message));
109     }
110 
111     @Test
112     public void testEntityWithNegativeContentLength() throws Exception {
113         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
114         final HttpMessage message = new TestHttpMessage();
115         message.addHeader("Content-Length", "-10");
116         Assertions.assertThrows(ProtocolException.class, () ->
117                 lenStrategy.determineLength(message));
118     }
119 
120     @Test
121     public void testEntityNoContentDelimiter() throws Exception {
122         final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
123         final HttpMessage message = new TestHttpMessage();
124         Assertions.assertEquals(ContentLengthStrategy.UNDEFINED, lenStrategy.determineLength(message));
125     }
126 
127 }
128