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.Assert;
38  import org.junit.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          Assert.assertEquals("name", header.getName());
52          Assert.assertEquals("value", header.getValue());
53          Assert.assertSame(buf, header.getBuffer());
54          Assert.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          Assert.assertEquals(orig.getName(), clone.getName());
71          Assert.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          try {
80              new BufferedHeader(buf, false);
81              Assert.fail("ParseException should have been thrown");
82          } catch (final ParseException e) {
83              //expected
84          }
85          buf.clear();
86          buf.append("blah");
87          try {
88              new BufferedHeader(buf, false);
89              Assert.fail("ParseException should have been thrown");
90          } catch (final ParseException e) {
91              //expected
92          }
93          buf.clear();
94          buf.append(":");
95          try {
96              new BufferedHeader(buf, false);
97              Assert.fail("ParseException should have been thrown");
98          } catch (final ParseException e) {
99              //expected
100         }
101         buf.clear();
102         buf.append("   :");
103         try {
104             new BufferedHeader(buf, false);
105             Assert.fail("ParseException should have been thrown");
106         } catch (final ParseException e) {
107             //expected
108         }
109         buf.clear();
110         buf.append(": blah");
111         try {
112             new BufferedHeader(buf, false);
113             Assert.fail("ParseException should have been thrown");
114         } catch (final ParseException e) {
115             //expected
116         }
117         buf.clear();
118         buf.append(" : blah");
119         try {
120             new BufferedHeader(buf, false);
121             Assert.fail("ParseException should have been thrown");
122         } catch (final ParseException e) {
123             //expected
124         }
125         buf.clear();
126         buf.append("header : blah");
127         try {
128             new BufferedHeader(buf, true);
129             Assert.fail("ParseException should have been thrown");
130         } catch (final ParseException e) {
131             //expected
132         }
133     }
134 
135 }
136