2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java $
4
* $Date: 2009-02-14 17:36:56 +0100 (Sat, 14 Feb 2009) $
5
* ====================================================================
6
* Licensed to the Apache Software Foundation (ASF) under one
7
* or more contributor license agreements. See the NOTICE file
8
* distributed with this work for additional information
9
* regarding copyright ownership. The ASF licenses this file
10
* to you under the Apache License, Version 2.0 (the
11
* "License"); you may not use this file except in compliance
12
* with the License. You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing,
17
* software distributed under the License is distributed on an
18
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19
* KIND, either express or implied. See the License for the
20
* specific language governing permissions and limitations
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.http.impl.nio.codecs;
33
import java.io.ByteArrayOutputStream;
34
import java.nio.ByteBuffer;
35
import java.nio.channels.Channels;
36
import java.nio.channels.WritableByteChannel;
38
import junit.framework.Test;
39
import junit.framework.TestCase;
40
import junit.framework.TestSuite;
42
import org.apache.http.impl.io.HttpTransportMetricsImpl;
43
import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
44
import org.apache.http.nio.reactor.SessionOutputBuffer;
45
import org.apache.http.params.BasicHttpParams;
46
import org.apache.http.params.HttpParams;
47
import org.apache.http.util.EncodingUtils;
50
* Simple tests for {@link ChunkEncoder}.
53
* @version $Id: TestChunkEncoder.java 744515 2009-02-14 16:36:56Z sebb $
55
public class TestChunkEncoder extends TestCase {
57
// ------------------------------------------------------------ Constructor
58
public TestChunkEncoder(String testName) {
62
// ------------------------------------------------------------------- Main
63
public static void main(String args[]) {
64
String[] testCaseName = { TestChunkEncoder.class.getName() };
65
junit.textui.TestRunner.main(testCaseName);
68
// ------------------------------------------------------- TestCase Methods
70
public static Test suite() {
71
return new TestSuite(TestChunkEncoder.class);
74
private static ByteBuffer wrap(final String s) {
75
return ByteBuffer.wrap(EncodingUtils.getAsciiBytes(s));
78
private static WritableByteChannel newChannel(final ByteArrayOutputStream baos) {
79
return Channels.newChannel(baos);
82
public void testBasicCoding() throws Exception {
83
ByteArrayOutputStream baos = new ByteArrayOutputStream();
84
WritableByteChannel channel = newChannel(baos);
85
HttpParams params = new BasicHttpParams();
86
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
87
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
88
ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
90
encoder.write(wrap("12345"));
91
encoder.write(wrap("678"));
92
encoder.write(wrap("90"));
95
outbuf.flush(channel);
97
String s = baos.toString("US-ASCII");
99
assertTrue(encoder.isCompleted());
100
assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
103
public void testChunkNoExceed() throws Exception {
104
ByteArrayOutputStream baos = new ByteArrayOutputStream();
105
WritableByteChannel channel = newChannel(baos);
106
HttpParams params = new BasicHttpParams();
107
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
108
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
109
ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
110
encoder.write(wrap("1234"));
113
outbuf.flush(channel);
115
String s = baos.toString("US-ASCII");
117
assertTrue(encoder.isCompleted());
118
assertEquals("4\r\n1234\r\n0\r\n\r\n", s);
122
public void testChunkExceed() throws Exception {
123
ByteArrayOutputStream baos = new ByteArrayOutputStream();
124
WritableByteChannel channel = newChannel(baos);
125
HttpParams params = new BasicHttpParams();
126
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, params);
127
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
128
ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
130
ByteBuffer src = wrap("0123456789ABCDEF");
133
assertTrue(src.hasRemaining());
134
assertEquals(10, src.remaining());
137
assertTrue(src.hasRemaining());
138
assertEquals(4, src.remaining());
141
assertFalse(src.hasRemaining());
143
outbuf.flush(channel);
144
String s = baos.toString("US-ASCII");
145
assertEquals("6\r\n012345\r\n6\r\n6789AB\r\n4\r\nCDEF\r\n", s);
149
public void testCodingEmptyBuffer() throws Exception {
150
ByteArrayOutputStream baos = new ByteArrayOutputStream();
151
WritableByteChannel channel = newChannel(baos);
152
HttpParams params = new BasicHttpParams();
153
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
154
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
155
ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
157
encoder.write(wrap("12345"));
158
encoder.write(wrap("678"));
159
encoder.write(wrap("90"));
161
ByteBuffer empty = ByteBuffer.allocate(100);
163
encoder.write(empty);
168
outbuf.flush(channel);
170
String s = baos.toString("US-ASCII");
172
assertTrue(encoder.isCompleted());
173
assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
176
public void testCodingCompleted() throws Exception {
177
ByteArrayOutputStream baos = new ByteArrayOutputStream();
178
WritableByteChannel channel = newChannel(baos);
179
HttpParams params = new BasicHttpParams();
180
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
181
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
182
ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
184
encoder.write(wrap("12345"));
185
encoder.write(wrap("678"));
186
encoder.write(wrap("90"));
190
encoder.write(wrap("more stuff"));
191
fail("IllegalStateException should have been thrown");
192
} catch (IllegalStateException ex) {
197
fail("IllegalStateException should have been thrown");
198
} catch (IllegalStateException ex) {
203
public void testInvalidConstructor() {
204
ByteArrayOutputStream baos = new ByteArrayOutputStream();
205
WritableByteChannel channel = newChannel(baos);
206
HttpParams params = new BasicHttpParams();
207
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
210
new ChunkEncoder(null, null, null);
211
fail("IllegalArgumentException should have been thrown");
212
} catch (IllegalArgumentException ex) {
216
new ChunkEncoder(channel, null, null);
217
fail("IllegalArgumentException should have been thrown");
218
} catch (IllegalArgumentException ex) {
222
new ChunkEncoder(channel, outbuf, null);
223
fail("IllegalArgumentException should have been thrown");
224
} catch (IllegalArgumentException ex) {