1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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