View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.jupiter.api.Assertions;
36  import org.junit.jupiter.api.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          Assertions.assertEquals(FrameType.DATA.value, dataFrame.getType());
49          Assertions.assertEquals(23, dataFrame.getStreamId());
50          Assertions.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          Assertions.assertEquals(FrameType.SETTINGS.value, settingsFrame.getType());
62          Assertions.assertEquals(0, settingsFrame.getStreamId());
63          Assertions.assertEquals(0, settingsFrame.getFlags());
64          final ByteBuffer payload = settingsFrame.getPayload();
65          Assertions.assertNotNull(payload);
66          Assertions.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          Assertions.assertEquals(FrameType.RST_STREAM.value, rstStreamFrame.getType());
76          Assertions.assertEquals(12, rstStreamFrame.getStreamId());
77          Assertions.assertEquals(0, rstStreamFrame.getFlags());
78          final ByteBuffer payload = rstStreamFrame.getPayload();
79          Assertions.assertNotNull(payload);
80          Assertions.assertEquals(4, payload.remaining());
81          Assertions.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          Assertions.assertEquals(FrameType.GOAWAY.value, goAwayFrame.getType());
91          Assertions.assertEquals(0, goAwayFrame.getStreamId());
92          Assertions.assertEquals(0, goAwayFrame.getFlags());
93          final ByteBuffer payload = goAwayFrame.getPayload();
94          Assertions.assertNotNull(payload);
95          Assertions.assertEquals(13, payload.getInt());
96          Assertions.assertEquals(H2Error.INTERNAL_ERROR.getCode(), payload.getInt());
97          final byte[] tmp = new byte[payload.remaining()];
98          payload.get(tmp);
99          Assertions.assertEquals("Oopsie", new String(tmp, StandardCharsets.US_ASCII));
100     }
101 
102 }