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.nio.codecs;
29  
30  import java.nio.ByteBuffer;
31  
32  import org.apache.http.Consts;
33  import org.apache.http.WritableByteChannelMock;
34  import org.apache.http.impl.io.HttpTransportMetricsImpl;
35  import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
36  import org.apache.http.nio.reactor.SessionOutputBuffer;
37  import org.junit.Assert;
38  import org.junit.Test;
39  import org.mockito.Matchers;
40  import org.mockito.Mockito;
41  
42  /**
43   * Simple tests for {@link ChunkEncoder}.
44   */
45  public class TestChunkEncoder {
46  
47      @Test
48      public void testBasicCoding() throws Exception {
49          final WritableByteChannelMock channel = new WritableByteChannelMock(64);
50          final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
51          final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
52          final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
53  
54          encoder.write(CodecTestUtils.wrap("12345"));
55          encoder.write(CodecTestUtils.wrap("678"));
56          encoder.write(CodecTestUtils.wrap("90"));
57          encoder.complete();
58  
59          outbuf.flush(channel);
60  
61          final String s = channel.dump(Consts.ASCII);
62  
63          Assert.assertTrue(encoder.isCompleted());
64          Assert.assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
65          Assert.assertEquals("[chunk-coded; completed: true]", encoder.toString());
66      }
67  
68      @Test
69      public void testChunkNoExceed() throws Exception {
70          final WritableByteChannelMock channel = new WritableByteChannelMock(64);
71          final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
72          final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
73          final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
74          encoder.write(CodecTestUtils.wrap("1234"));
75          encoder.complete();
76  
77          outbuf.flush(channel);
78  
79          final String s = channel.dump(Consts.ASCII);
80  
81          Assert.assertTrue(encoder.isCompleted());
82          Assert.assertEquals("4\r\n1234\r\n0\r\n\r\n", s);
83      }
84  
85      @Test // See HTTPCORE-239
86      public void testLimitedChannel() throws Exception {
87          final WritableByteChannelMock channel = new WritableByteChannelMock(16, 16);
88          final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
89          final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
90          final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
91  
92          // fill up the channel
93          channel.write(CodecTestUtils.wrap("0123456789ABCDEF"));
94          // fill up the out buffer
95          outbuf.write(CodecTestUtils.wrap("0123456789ABCDEF"));
96  
97          final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
98          Assert.assertEquals(0, encoder.write(src));
99          Assert.assertEquals(0, encoder.write(src));
100         Assert.assertEquals(0, encoder.write(src));
101 
102         // should not be able to copy any bytes, until we flush the channel and buffer
103         channel.reset();
104         outbuf.flush(channel);
105         channel.reset();
106 
107         Assert.assertEquals(10, encoder.write(src));
108         channel.flush();
109         Assert.assertEquals(6, encoder.write(src));
110         channel.flush();
111         Assert.assertEquals(0, encoder.write(src));
112 
113         outbuf.flush(channel);
114         final String s = channel.dump(Consts.ASCII);
115         Assert.assertEquals("4\r\n0123\r\n4\r\n4567\r\n2\r\n89\r\n4\r\nABCD\r\n2\r\nEF\r\n", s);
116     }
117 
118     @Test
119     public void testBufferFragments() throws Exception {
120         final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(1024));
121         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 1024);
122         final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
123         final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics, 1024);
124 
125         Assert.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
126         Assert.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
127         Assert.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
128 
129         Mockito.verify(channel, Mockito.never()).write(Matchers.<ByteBuffer>any());
130 
131         outbuf.flush(channel);
132         final String s = channel.dump(Consts.ASCII);
133         Assert.assertEquals("10\r\n0123456789ABCDEF\r\n10\r\n0123456789ABCDEF\r\n" +
134                 "10\r\n0123456789ABCDEF\r\n", s);
135     }
136 
137     @Test
138     public void testChunkExceed() throws Exception {
139         final WritableByteChannelMock channel = new WritableByteChannelMock(64);
140         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
141         final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
142         final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
143 
144         final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
145 
146         Assert.assertEquals(16, encoder.write(src));
147         Assert.assertEquals(0, src.remaining());
148 
149         outbuf.flush(channel);
150         final String s = channel.dump(Consts.ASCII);
151         Assert.assertEquals("4\r\n0123\r\n4\r\n4567\r\n4\r\n89AB\r\n4\r\nCDEF\r\n", s);
152 
153     }
154 
155     @Test
156     public void testCodingEmptyBuffer() throws Exception {
157         final WritableByteChannelMock channel = new WritableByteChannelMock(64);
158         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
159         final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
160         final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
161 
162         encoder.write(CodecTestUtils.wrap("12345"));
163         encoder.write(CodecTestUtils.wrap("678"));
164         encoder.write(CodecTestUtils.wrap("90"));
165 
166         final ByteBuffer empty = ByteBuffer.allocate(100);
167         empty.flip();
168         encoder.write(empty);
169         encoder.write(null);
170 
171         encoder.complete();
172 
173         outbuf.flush(channel);
174 
175         final String s = channel.dump(Consts.ASCII);
176 
177         Assert.assertTrue(encoder.isCompleted());
178         Assert.assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
179     }
180 
181     @Test
182     public void testCodingCompleted() throws Exception {
183         final WritableByteChannelMock channel = new WritableByteChannelMock(64);
184         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
185         final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
186         final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
187 
188         encoder.write(CodecTestUtils.wrap("12345"));
189         encoder.write(CodecTestUtils.wrap("678"));
190         encoder.write(CodecTestUtils.wrap("90"));
191         encoder.complete();
192 
193         try {
194             encoder.write(CodecTestUtils.wrap("more stuff"));
195             Assert.fail("IllegalStateException should have been thrown");
196         } catch (final IllegalStateException ex) {
197             // ignore
198         }
199         try {
200             encoder.complete();
201             Assert.fail("IllegalStateException should have been thrown");
202         } catch (final IllegalStateException ex) {
203             // ignore
204         }
205     }
206 
207     @Test
208     public void testInvalidConstructor() {
209         final WritableByteChannelMock channel = new WritableByteChannelMock(64);
210         final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
211 
212         try {
213             new ChunkEncoder(null, null, null);
214             Assert.fail("IllegalArgumentException should have been thrown");
215         } catch (final IllegalArgumentException ex) {
216             // ignore
217         }
218         try {
219             new ChunkEncoder(channel, null, null);
220             Assert.fail("IllegalArgumentException should have been thrown");
221         } catch (final IllegalArgumentException ex) {
222             // ignore
223         }
224         try {
225             new ChunkEncoder(channel, outbuf, null);
226             Assert.fail("IllegalArgumentException should have been thrown");
227         } catch (final IllegalArgumentException ex) {
228             // ignore
229         }
230     }
231 
232 }