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.Assert;
34  import org.junit.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          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              //expected
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              //expected
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 }