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.nio.entity;
29
30 import java.nio.charset.StandardCharsets;
31
32 import org.apache.hc.core5.http.ContentType;
33 import org.apache.hc.core5.http.WritableByteChannelMock;
34 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
35 import org.apache.hc.core5.http.nio.BasicDataStreamChannel;
36 import org.apache.hc.core5.http.nio.DataStreamChannel;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39
40 public class TestStringAsyncEntityProducer {
41
42 @Test
43 public void testTextContent() throws Exception {
44
45 final AsyncEntityProducer producer = new StringAsyncEntityProducer(
46 "abc", ContentType.TEXT_PLAIN);
47
48 Assertions.assertEquals(-1, producer.getContentLength());
49 Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
50 Assertions.assertNull(producer.getContentEncoding());
51
52 final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
53 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
54
55 producer.produce(streamChannel);
56
57 Assertions.assertFalse(byteChannel.isOpen());
58 Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
59 }
60
61 @Test
62 public void testTextContentRepeatable() throws Exception {
63 final AsyncEntityProducer producer = new StringAsyncEntityProducer(
64 "abc", ContentType.TEXT_PLAIN);
65
66 Assertions.assertEquals(-1, producer.getContentLength());
67 Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
68 Assertions.assertNull(producer.getContentEncoding());
69
70 for (int i = 0; i < 3; i++) {
71 final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
72 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
73
74 producer.produce(streamChannel);
75
76 Assertions.assertFalse(byteChannel.isOpen());
77 Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
78
79 producer.releaseResources();
80 }
81 }
82
83 }