|
| 1 | +package com.github.dockerjava.netty.handler; |
| 2 | + |
| 3 | +import com.github.dockerjava.core.async.ResultCallbackTemplate; |
| 4 | +import io.netty.buffer.ByteBuf; |
| 5 | +import io.netty.buffer.ByteBufInputStream; |
| 6 | +import io.netty.buffer.Unpooled; |
| 7 | +import io.netty.channel.ChannelHandlerContext; |
| 8 | +import org.apache.commons.io.IOUtils; |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.testng.Assert; |
| 11 | +import org.testng.annotations.Test; |
| 12 | + |
| 13 | +import java.io.InputStream; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Alexander Koshevoy |
| 17 | + */ |
| 18 | +public class HttpResponseStreamHandlerTest { |
| 19 | + @Test |
| 20 | + public void testNoBytesSkipped() throws Exception { |
| 21 | + ResultCallbackTest callback = new ResultCallbackTest(); |
| 22 | + HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback); |
| 23 | + ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); |
| 24 | + ByteBuf buffer = generateByteBuf(); |
| 25 | + streamHandler.channelRead0(ctx, buffer); |
| 26 | + streamHandler.channelReadComplete(ctx); |
| 27 | + |
| 28 | + Assert.assertTrue(IOUtils.contentEquals(callback.getInputStream(), new ByteBufInputStream(buffer))); |
| 29 | + } |
| 30 | + |
| 31 | + private ByteBuf generateByteBuf() { |
| 32 | + byte[] array = new byte[256]; |
| 33 | + for (int i = 0; i < array.length; i++) { |
| 34 | + array[i] = (byte) i; |
| 35 | + } |
| 36 | + return Unpooled.copiedBuffer(array); |
| 37 | + } |
| 38 | + |
| 39 | + private static class ResultCallbackTest extends ResultCallbackTemplate<ResultCallbackTest, InputStream> { |
| 40 | + private InputStream stream; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onNext(InputStream stream) { |
| 44 | + this.stream = stream; |
| 45 | + } |
| 46 | + |
| 47 | + public InputStream getInputStream() { |
| 48 | + return stream; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments