Skip to content

Commit 39feaee

Browse files
committed
Add test coverage.
1 parent 84cd307 commit 39feaee

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@
2929

3030
import java.io.IOException;
3131
import java.nio.ByteBuffer;
32+
import java.util.ArrayList;
33+
import java.util.List;
3234

35+
import org.apache.hc.core5.http.Header;
36+
import org.apache.hc.core5.http.HttpException;
3337
import org.apache.hc.core5.http.config.CharCodingConfig;
3438
import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
39+
import org.apache.hc.core5.http.impl.CharCodingSupport;
40+
import org.apache.hc.core5.http.message.BasicHeader;
3541
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
3642
import org.apache.hc.core5.http.nio.HandlerFactory;
3743
import org.apache.hc.core5.http.nio.command.ExecutableCommand;
@@ -45,23 +51,31 @@
4551
import org.apache.hc.core5.http2.frame.FrameType;
4652
import org.apache.hc.core5.http2.frame.RawFrame;
4753
import org.apache.hc.core5.http2.frame.StreamIdGenerator;
54+
import org.apache.hc.core5.http2.hpack.HPackEncoder;
4855
import org.apache.hc.core5.reactor.ProtocolIOSession;
56+
import org.apache.hc.core5.util.ByteArrayBuffer;
4957
import org.junit.jupiter.api.Assertions;
5058
import org.junit.jupiter.api.BeforeEach;
5159
import org.junit.jupiter.api.Test;
60+
import org.mockito.ArgumentCaptor;
5261
import org.mockito.ArgumentMatchers;
62+
import org.mockito.Captor;
5363
import org.mockito.Mock;
5464
import org.mockito.Mockito;
5565
import org.mockito.MockitoAnnotations;
5666

5767
class TestAbstractH2StreamMultiplexer {
68+
private static final FrameFactory FRAME_FACTORY = DefaultFrameFactory.INSTANCE;
69+
private static final H2StreamHandler STREAM_HANDLER = Mockito.mock(H2StreamHandler.class);
5870

5971
@Mock
6072
ProtocolIOSession protocolIOSession;
6173
@Mock
6274
HttpProcessor httpProcessor;
6375
@Mock
6476
H2StreamListener h2StreamListener;
77+
@Captor
78+
ArgumentCaptor<List<Header>> headersCaptor;
6579

6680
@BeforeEach
6781
void prepareMocks() {
@@ -99,7 +113,7 @@ H2StreamHandler createRemotelyInitiatedStream(
99113
final HttpProcessor httpProcessor,
100114
final BasicHttpConnectionMetrics connMetrics,
101115
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
102-
return null;
116+
return STREAM_HANDLER;
103117
}
104118

105119
@Override
@@ -128,7 +142,7 @@ void testInputOneFrame() throws Exception {
128142

129143
final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(
130144
protocolIOSession,
131-
DefaultFrameFactory.INSTANCE,
145+
FRAME_FACTORY,
132146
StreamIdGenerator.ODD,
133147
httpProcessor,
134148
CharCodingConfig.DEFAULT,
@@ -179,7 +193,7 @@ void testInputMultipleFrames() throws Exception {
179193

180194
final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(
181195
protocolIOSession,
182-
DefaultFrameFactory.INSTANCE,
196+
FRAME_FACTORY,
183197
StreamIdGenerator.ODD,
184198
httpProcessor,
185199
CharCodingConfig.DEFAULT,
@@ -212,5 +226,40 @@ void testInputMultipleFrames() throws Exception {
212226
});
213227
}
214228

229+
@Test
230+
void testInputHeaderContinuationFrame() throws IOException, HttpException {
231+
final H2Config h2Config = H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE)
232+
.build();
233+
234+
final ByteArrayBuffer buf = new ByteArrayBuffer(19);
235+
final HPackEncoder encoder = new HPackEncoder(H2Config.INIT.getHeaderTableSize(), CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));
236+
final List<Header> headers = new ArrayList<>();
237+
headers.add(new BasicHeader("test-header-key", "value"));
238+
headers.add(new BasicHeader(":status", "200"));
239+
encoder.encodeHeaders(buf, headers, h2Config.isCompressionEnabled());
240+
241+
final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
242+
final FrameOutputBuffer outBuffer = new FrameOutputBuffer(16 * 1024);
243+
244+
final RawFrame headerFrame = FRAME_FACTORY.createHeaders(2, ByteBuffer.wrap(buf.array(), 0, 10), false, false);
245+
outBuffer.write(headerFrame, writableChannel);
246+
final RawFrame continuationFrame = FRAME_FACTORY.createContinuation(2, ByteBuffer.wrap(buf.array(), 10, 9), true);
247+
outBuffer.write(continuationFrame, writableChannel);
248+
final byte[] bytes = writableChannel.toByteArray();
249+
250+
final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(
251+
protocolIOSession,
252+
FRAME_FACTORY,
253+
StreamIdGenerator.ODD,
254+
httpProcessor,
255+
CharCodingConfig.DEFAULT,
256+
h2Config,
257+
h2StreamListener
258+
);
259+
260+
streamMultiplexer.onInput(ByteBuffer.wrap(bytes));
261+
Mockito.verify(STREAM_HANDLER).consumeHeader(headersCaptor.capture(), ArgumentMatchers.eq(false));
262+
Assertions.assertFalse(headersCaptor.getValue().isEmpty());
263+
}
215264
}
216265

0 commit comments

Comments
 (0)