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 org.apache.hc.core5.http.HeaderElement;
31 import org.apache.hc.core5.http.NameValuePair;
32 import org.apache.hc.core5.util.CharArrayBuffer;
33 import org.junit.Assert;
34 import org.junit.Test;
35
36
37
38
39 public class TestHeaderElement {
40
41 @Test
42 public void testConstructor3() throws Exception {
43 final HeaderElement element = new BasicHeaderElement("name", "value",
44 new NameValuePair[] {
45 new BasicNameValuePair("param1", "value1"),
46 new BasicNameValuePair("param2", "value2")
47 } );
48 Assert.assertEquals("name", element.getName());
49 Assert.assertEquals("value", element.getValue());
50 Assert.assertEquals(2, element.getParameters().length);
51 Assert.assertEquals("value1", element.getParameterByName("param1").getValue());
52 Assert.assertEquals("value2", element.getParameterByName("param2").getValue());
53 }
54
55 @Test
56 public void testConstructor2() throws Exception {
57 final HeaderElement element = new BasicHeaderElement("name", "value");
58 Assert.assertEquals("name", element.getName());
59 Assert.assertEquals("value", element.getValue());
60 Assert.assertEquals(0, element.getParameters().length);
61 }
62
63
64 @Test
65 public void testInvalidName() {
66 try {
67 new BasicHeaderElement(null, null, null);
68 Assert.fail("NullPointerException should have been thrown");
69 } catch (final NullPointerException ex) {
70
71 }
72 }
73
74 @Test
75 public void testParamByName() throws Exception {
76 final String s = "name = value; param1 = value1; param2 = value2";
77 final CharArrayBuffer buf = new CharArrayBuffer(64);
78 buf.append(s);
79 final ParserCursor cursor = new ParserCursor(0, buf.length());
80 final HeaderElement element = BasicHeaderValueParser.INSTANCE.parseHeaderElement(buf, cursor);
81 Assert.assertEquals("value1", element.getParameterByName("param1").getValue());
82 Assert.assertEquals("value2", element.getParameterByName("param2").getValue());
83 Assert.assertNull(element.getParameterByName("param3"));
84 try {
85 element.getParameterByName(null);
86 Assert.fail("NullPointerException should have been thrown");
87 } catch (final NullPointerException ex) {
88
89 }
90 }
91
92 @Test
93 public void testToString() {
94 final BasicHeaderElement element = new BasicHeaderElement("name", "value",
95 new NameValuePair[] {
96 new BasicNameValuePair("param1", "value1"),
97 new BasicNameValuePair("param2", "value2")
98 } );
99 Assert.assertEquals("name=value; param1=value1; param2=value2", element.toString());
100 }
101
102 }