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 package org.apache.hc.core5.http2.frame;
28
29 import java.nio.ByteBuffer;
30 import java.nio.charset.StandardCharsets;
31
32 import org.apache.hc.core5.http2.H2Error;
33 import org.apache.hc.core5.http2.config.H2Param;
34 import org.apache.hc.core5.http2.config.H2Setting;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38 public class TestDefaultFrameFactory {
39
40 @Test
41 public void testDataFrame() throws Exception {
42
43 final FrameFactory frameFactory = new DefaultFrameFactory();
44
45 final byte[] data = new byte[]{'a', 'b', 'c', 'd', 'e', 'f'};
46 final Frame<ByteBuffer> dataFrame = frameFactory.createData(23, ByteBuffer.wrap(data), true);
47
48 Assert.assertEquals(FrameType.DATA.value, dataFrame.getType());
49 Assert.assertEquals(23, dataFrame.getStreamId());
50 Assert.assertEquals(1L, dataFrame.getFlags());
51 }
52
53 @Test
54 public void testSettingFrame() throws Exception {
55
56 final FrameFactory frameFactory = new DefaultFrameFactory();
57 final Frame<ByteBuffer> settingsFrame = frameFactory.createSettings(
58 new H2Setting(H2Param.HEADER_TABLE_SIZE, 1024),
59 new H2Setting(H2Param.MAX_CONCURRENT_STREAMS, 1));
60
61 Assert.assertEquals(FrameType.SETTINGS.value, settingsFrame.getType());
62 Assert.assertEquals(0, settingsFrame.getStreamId());
63 Assert.assertEquals(0, settingsFrame.getFlags());
64 final ByteBuffer payload = settingsFrame.getPayload();
65 Assert.assertNotNull(payload);
66 Assert.assertEquals(12, payload.remaining());
67 }
68
69 @Test
70 public void testResetStreamFrame() throws Exception {
71
72 final FrameFactory frameFactory = new DefaultFrameFactory();
73 final Frame<ByteBuffer> rstStreamFrame = frameFactory.createResetStream(12, H2Error.INTERNAL_ERROR);
74
75 Assert.assertEquals(FrameType.RST_STREAM.value, rstStreamFrame.getType());
76 Assert.assertEquals(12, rstStreamFrame.getStreamId());
77 Assert.assertEquals(0, rstStreamFrame.getFlags());
78 final ByteBuffer payload = rstStreamFrame.getPayload();
79 Assert.assertNotNull(payload);
80 Assert.assertEquals(4, payload.remaining());
81 Assert.assertEquals(H2Error.INTERNAL_ERROR.getCode(), payload.getInt());
82 }
83
84 @Test
85 public void testGoAwayFrame() throws Exception {
86
87 final FrameFactory frameFactory = new DefaultFrameFactory();
88 final Frame<ByteBuffer> goAwayFrame = frameFactory.createGoAway(13, H2Error.INTERNAL_ERROR, "Oopsie");
89
90 Assert.assertEquals(FrameType.GOAWAY.value, goAwayFrame.getType());
91 Assert.assertEquals(0, goAwayFrame.getStreamId());
92 Assert.assertEquals(0, goAwayFrame.getFlags());
93 final ByteBuffer payload = goAwayFrame.getPayload();
94 Assert.assertNotNull(payload);
95 Assert.assertEquals(13, payload.getInt());
96 Assert.assertEquals(H2Error.INTERNAL_ERROR.getCode(), payload.getInt());
97 final byte[] tmp = new byte[payload.remaining()];
98 payload.get(tmp);
99 Assert.assertEquals("Oopsie", new String(tmp, StandardCharsets.US_ASCII));
100 }
101
102 }