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
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
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
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
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
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
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
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
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
132 }
133 }
134
135 }
136