|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
1 | 14 | package org.asynchttpclient.request.body.generator;
|
2 | 15 |
|
3 | 16 | import org.asynchttpclient.request.body.Body;
|
|
12 | 25 |
|
13 | 26 | public class FeedableBodyGeneratorTest {
|
14 | 27 |
|
15 |
| - private FeedableBodyGenerator feedableBodyGenerator; |
16 |
| - private TestFeedListener listener; |
17 |
| - |
18 |
| - @BeforeMethod |
19 |
| - public void setUp() throws Exception { |
20 |
| - feedableBodyGenerator = new FeedableBodyGenerator(); |
21 |
| - listener = new TestFeedListener(); |
22 |
| - feedableBodyGenerator.setListener(listener); |
23 |
| - } |
24 |
| - |
25 |
| - @Test(groups = "standalone") |
26 |
| - public void feedNotifiesListener() throws Exception { |
27 |
| - feedableBodyGenerator.feed(ByteBuffer.allocate(0), false); |
28 |
| - feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); |
29 |
| - assertEquals(listener.getCalls(), 2); |
30 |
| - } |
31 |
| - |
32 |
| - @Test(groups = "standalone") |
33 |
| - public void readingBytesReturnsFedContentWithEmptyLastBuffer() throws Exception { |
34 |
| - byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
35 |
| - feedableBodyGenerator.feed(ByteBuffer.wrap(content), false); |
36 |
| - feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); |
37 |
| - Body body = feedableBodyGenerator.createBody(); |
38 |
| - assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); |
39 |
| - assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); |
40 |
| - assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
41 |
| - |
42 |
| - } |
43 |
| - |
44 |
| - @Test(groups = "standalone") |
45 |
| - public void readingBytesReturnsFedContentWithFilledLastBuffer() throws Exception { |
46 |
| - byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
47 |
| - feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); |
48 |
| - Body body = feedableBodyGenerator.createBody(); |
49 |
| - assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); |
50 |
| - assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); |
51 |
| - assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
52 |
| - |
53 |
| - } |
54 |
| - |
55 |
| - @Test(groups = "standalone") |
56 |
| - public void readingBytesReturnsFedContentWithoutChunkBoundariesWhenDisabled() throws Exception { |
57 |
| - byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
58 |
| - feedableBodyGenerator.setWriteChunkBoundaries(false); |
59 |
| - feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); |
60 |
| - Body body = feedableBodyGenerator.createBody(); |
61 |
| - assertEquals(readFromBody(body), "Test123".getBytes(StandardCharsets.US_ASCII)); |
62 |
| - assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
63 |
| - |
64 |
| - } |
65 |
| - |
66 |
| - private byte[] readFromBody(Body body) throws IOException { |
67 |
| - ByteBuffer byteBuffer = ByteBuffer.allocate(512); |
68 |
| - body.read(byteBuffer); |
69 |
| - byteBuffer.flip(); |
70 |
| - byte[] readBytes = new byte[byteBuffer.remaining()]; |
71 |
| - byteBuffer.get(readBytes); |
72 |
| - return readBytes; |
73 |
| - } |
74 |
| - |
75 |
| - private static class TestFeedListener implements FeedableBodyGenerator.FeedListener { |
76 |
| - |
77 |
| - private int calls; |
78 |
| - @Override |
79 |
| - public void onContentAdded() { |
80 |
| - calls++; |
| 28 | + private FeedableBodyGenerator feedableBodyGenerator; |
| 29 | + private TestFeedListener listener; |
| 30 | + |
| 31 | + @BeforeMethod |
| 32 | + public void setUp() throws Exception { |
| 33 | + feedableBodyGenerator = new FeedableBodyGenerator(); |
| 34 | + listener = new TestFeedListener(); |
| 35 | + feedableBodyGenerator.setListener(listener); |
| 36 | + } |
| 37 | + |
| 38 | + @Test(groups = "standalone") |
| 39 | + public void feedNotifiesListener() throws Exception { |
| 40 | + feedableBodyGenerator.feed(ByteBuffer.allocate(0), false); |
| 41 | + feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); |
| 42 | + assertEquals(listener.getCalls(), 2); |
| 43 | + } |
| 44 | + |
| 45 | + @Test(groups = "standalone") |
| 46 | + public void readingBytesReturnsFedContentWithEmptyLastBufferWhenChunkBoundariesEnabled() throws Exception { |
| 47 | + feedableBodyGenerator.writeChunkBoundaries(); |
| 48 | + byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
| 49 | + feedableBodyGenerator.feed(ByteBuffer.wrap(content), false); |
| 50 | + feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); |
| 51 | + Body body = feedableBodyGenerator.createBody(); |
| 52 | + assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); |
| 53 | + assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); |
| 54 | + assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test(groups = "standalone") |
| 59 | + public void readingBytesReturnsFedContentWithFilledLastBufferWhenChunkBoundariesEnabled() throws Exception { |
| 60 | + feedableBodyGenerator.writeChunkBoundaries(); |
| 61 | + byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
| 62 | + feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); |
| 63 | + Body body = feedableBodyGenerator.createBody(); |
| 64 | + assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); |
| 65 | + assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); |
| 66 | + assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @Test(groups = "standalone") |
| 71 | + public void readingBytesReturnsFedContentWithoutChunkBoundariesWhenNotEnabled() throws Exception { |
| 72 | + byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); |
| 73 | + feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); |
| 74 | + Body body = feedableBodyGenerator.createBody(); |
| 75 | + assertEquals(readFromBody(body), "Test123".getBytes(StandardCharsets.US_ASCII)); |
| 76 | + assertEquals(body.read(ByteBuffer.allocate(1)), -1); |
| 77 | + } |
| 78 | + |
| 79 | + private byte[] readFromBody(Body body) throws IOException { |
| 80 | + ByteBuffer byteBuffer = ByteBuffer.allocate(512); |
| 81 | + body.read(byteBuffer); |
| 82 | + byteBuffer.flip(); |
| 83 | + byte[] readBytes = new byte[byteBuffer.remaining()]; |
| 84 | + byteBuffer.get(readBytes); |
| 85 | + return readBytes; |
81 | 86 | }
|
82 | 87 |
|
83 |
| - public int getCalls() { |
84 |
| - return calls; |
| 88 | + private static class TestFeedListener implements FeedableBodyGenerator.FeedListener { |
| 89 | + |
| 90 | + private int calls; |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onContentAdded() { |
| 94 | + calls++; |
| 95 | + } |
| 96 | + |
| 97 | + public int getCalls() { |
| 98 | + return calls; |
| 99 | + } |
85 | 100 | }
|
86 |
| - } |
87 | 101 | }
|
0 commit comments