|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty.internal; |
| 17 | + |
| 18 | +import static org.mockito.Mockito.eq; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.times; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import io.netty.channel.ChannelHandlerContext; |
| 25 | +import io.netty.channel.ChannelPipeline; |
| 26 | +import io.netty.handler.codec.http.HttpContent; |
| 27 | +import io.netty.handler.codec.http.HttpObject; |
| 28 | +import io.netty.handler.codec.http.LastHttpContent; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link LastHttpContentSwallower}. |
| 34 | + */ |
| 35 | +public class LastHttpContentSwallowerTest { |
| 36 | + private final LastHttpContentSwallower lastHttpContentSwallower = LastHttpContentSwallower.getInstance(); |
| 37 | + private ChannelHandlerContext mockCtx; |
| 38 | + private ChannelPipeline mockPipeline; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setUp() { |
| 42 | + mockCtx = mock(ChannelHandlerContext.class); |
| 43 | + mockPipeline = mock(ChannelPipeline.class); |
| 44 | + when(mockCtx.pipeline()).thenReturn(mockPipeline); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testOtherHttpObjectRead_removesSelfFromPipeline() { |
| 49 | + HttpObject contentObject = mock(HttpContent.class); |
| 50 | + lastHttpContentSwallower.channelRead0(mockCtx, contentObject); |
| 51 | + verify(mockPipeline).remove(eq(lastHttpContentSwallower)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testLastHttpContentRead_removesSelfFromPipeline() { |
| 56 | + LastHttpContent lastContent = mock(LastHttpContent.class); |
| 57 | + lastHttpContentSwallower.channelRead0(mockCtx, lastContent); |
| 58 | + verify(mockPipeline).remove(eq(lastHttpContentSwallower)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testLastHttpContentRead_swallowsObject() { |
| 63 | + LastHttpContent lastContent = mock(LastHttpContent.class); |
| 64 | + lastHttpContentSwallower.channelRead0(mockCtx, lastContent); |
| 65 | + verify(mockCtx, times(0)).fireChannelRead(eq(lastContent)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testOtherHttpObjectRead_doesNotSwallowObject() { |
| 70 | + HttpContent content = mock(HttpContent.class); |
| 71 | + lastHttpContentSwallower.channelRead0(mockCtx, content); |
| 72 | + verify(mockCtx).fireChannelRead(eq(content)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCallsReadAfterSwallowingContent() { |
| 77 | + LastHttpContent lastContent = mock(LastHttpContent.class); |
| 78 | + lastHttpContentSwallower.channelRead0(mockCtx, lastContent); |
| 79 | + verify(mockCtx).read(); |
| 80 | + } |
| 81 | +} |
0 commit comments