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 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.jupiter.api.Assertions;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Simple tests for {@link HeaderElement}.
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          Assertions.assertEquals("name", element.getName());
49          Assertions.assertEquals("value", element.getValue());
50          Assertions.assertEquals(2, element.getParameters().length);
51          Assertions.assertEquals("value1", element.getParameterByName("param1").getValue());
52          Assertions.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          Assertions.assertEquals("name", element.getName());
59          Assertions.assertEquals("value", element.getValue());
60          Assertions.assertEquals(0, element.getParameters().length);
61      }
62  
63  
64      @Test
65      public void testInvalidName() {
66          Assertions.assertThrows(NullPointerException.class, () -> new BasicHeaderElement(null, null, null));
67      }
68  
69      @Test
70      public void testParamByName() throws Exception {
71          final String s = "name = value; param1 = value1; param2 = value2";
72          final CharArrayBuffer buf = new CharArrayBuffer(64);
73          buf.append(s);
74          final ParserCursor cursor = new ParserCursor(0, buf.length());
75          final HeaderElement element = BasicHeaderValueParser.INSTANCE.parseHeaderElement(buf, cursor);
76          Assertions.assertEquals("value1", element.getParameterByName("param1").getValue());
77          Assertions.assertEquals("value2", element.getParameterByName("param2").getValue());
78          Assertions.assertNull(element.getParameterByName("param3"));
79          Assertions.assertThrows(NullPointerException.class, () -> element.getParameterByName(null));
80      }
81  
82      @Test
83      public void testToString() {
84          final BasicHeaderElement element = new BasicHeaderElement("name", "value",
85                  new NameValuePair[] {
86                          new BasicNameValuePair("param1", "value1"),
87                          new BasicNameValuePair("param2", "value2")
88                  } );
89          Assertions.assertEquals("name=value; param1=value1; param2=value2", element.toString());
90      }
91  
92  }