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  
28  package org.apache.hc.core5.http.message;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.apache.hc.core5.http.ParseException;
36  import org.apache.hc.core5.util.CharArrayBuffer;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Unit tests for {@link BufferedHeader}.
42   *
43   */
44  public class TestBufferedHeader {
45  
46      @Test
47      public void testBasicConstructor() throws Exception {
48          final CharArrayBuffer buf = new CharArrayBuffer(32);
49          buf.append("name: value");
50          final BufferedHeader header = new BufferedHeader(buf, false);
51          Assertions.assertEquals("name", header.getName());
52          Assertions.assertEquals("value", header.getValue());
53          Assertions.assertSame(buf, header.getBuffer());
54          Assertions.assertEquals(5, header.getValuePos());
55      }
56  
57      @Test
58      public void testSerialization() throws Exception {
59          final CharArrayBuffer buf = new CharArrayBuffer(32);
60          buf.append("name: value");
61          final BufferedHeader orig = new BufferedHeader(buf, false);
62          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
63          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
64          outStream.writeObject(orig);
65          outStream.close();
66          final byte[] raw = outbuffer.toByteArray();
67          final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
68          final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
69          final BufferedHeader clone = (BufferedHeader) inStream.readObject();
70          Assertions.assertEquals(orig.getName(), clone.getName());
71          Assertions.assertEquals(orig.getValue(), clone.getValue());
72      }
73  
74      @Test
75      public void testInvalidHeaderParsing() throws Exception {
76          final CharArrayBuffer buf = new CharArrayBuffer(16);
77          buf.clear();
78          buf.append("");
79          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
80          buf.clear();
81          buf.append("blah");
82          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
83          buf.clear();
84          buf.append(":");
85          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
86          buf.clear();
87          buf.append("   :");
88          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
89          buf.clear();
90          buf.append(": blah");
91          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
92          buf.clear();
93          buf.append(" : blah");
94          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, false));
95          buf.clear();
96          buf.append("header : blah");
97          Assertions.assertThrows(ParseException.class, () -> new BufferedHeader(buf, true));
98      }
99  
100 }
101