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.jupiter.api.Assertions;
37 import org.junit.jupiter.api.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 Assertions.assertEquals("name", header.getName());
48 Assertions.assertEquals("value", header.getValue());
49 }
50
51 @Test
52 public void testBasicConstructorNullValue() {
53 final Header header = new BasicHeader("name", null);
54 Assertions.assertEquals("name", header.getName());
55 Assertions.assertNull(header.getValue());
56 }
57
58 @Test
59 public void testInvalidName() {
60 Assertions.assertThrows(NullPointerException.class, () -> new BasicHeader(null, null));
61 }
62
63 @Test
64 public void testToString() {
65 final Header header1 = new BasicHeader("name1", "value1");
66 Assertions.assertEquals("name1: value1", header1.toString());
67 final Header header2 = new BasicHeader("name2", null);
68 Assertions.assertEquals("name2: ", header2.toString());
69 }
70
71 @Test
72 public void testSerialization() throws Exception {
73 final BasicHeader orig = new BasicHeader("name1", "value1");
74 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
75 final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
76 outStream.writeObject(orig);
77 outStream.close();
78 final byte[] raw = outbuffer.toByteArray();
79 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
80 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
81 final BasicHeader clone = (BasicHeader) inStream.readObject();
82 Assertions.assertEquals(orig.getName(), clone.getName());
83 Assertions.assertEquals(orig.getValue(), clone.getValue());
84 }
85
86
87 @Test
88 public void testClone() throws Exception {
89 final BasicHeader orig = new BasicHeader("name1", "value1");
90 final BasicHeader clone = orig.clone();
91 Assertions.assertEquals(orig.getName(), clone.getName());
92 Assertions.assertEquals(orig.getValue(), clone.getValue());
93 }
94
95 }
96