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.Header;
36 import org.junit.Assert;
37 import org.junit.Test;
38
39
40
41
42 public class TestHeader {
43
44 @Test
45 public void testBasicConstructor() {
46 final Header header = new BasicHeader("name", "value");
47 Assert.assertEquals("name", header.getName());
48 Assert.assertEquals("value", header.getValue());
49 }
50
51 @Test
52 public void testBasicConstructorNullValue() {
53 final Header header = new BasicHeader("name", null);
54 Assert.assertEquals("name", header.getName());
55 Assert.assertEquals(null, header.getValue());
56 }
57
58 @Test
59 public void testInvalidName() {
60 try {
61 new BasicHeader(null, null);
62 Assert.fail("NullPointerException should have been thrown");
63 } catch (final NullPointerException ex) {
64
65 }
66 }
67
68 @Test
69 public void testToString() {
70 final Header header1 = new BasicHeader("name1", "value1");
71 Assert.assertEquals("name1: value1", header1.toString());
72 final Header header2 = new BasicHeader("name2", null);
73 Assert.assertEquals("name2: ", header2.toString());
74 }
75
76 @Test
77 public void testSerialization() throws Exception {
78 final BasicHeader orig = new BasicHeader("name1", "value1");
79 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
80 final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
81 outStream.writeObject(orig);
82 outStream.close();
83 final byte[] raw = outbuffer.toByteArray();
84 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
85 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
86 final BasicHeader clone = (BasicHeader) inStream.readObject();
87 Assert.assertEquals(orig.getName(), clone.getName());
88 Assert.assertEquals(orig.getValue(), clone.getValue());
89 }
90
91 }
92