View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.http;
18  
19  import static org.apache.juneau.http.HttpHeaders.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.function.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.commons.lang.*;
27  import org.apache.juneau.http.header.*;
28  import org.junit.jupiter.api.*;
29  
30  class BasicHeader_Test extends TestBase {
31  
32  	@Test void a01_ofPair() {
33  		var x = stringHeader("Foo:bar");
34  		assertEquals("Foo", x.getName());
35  		assertEquals("bar", x.getValue());
36  
37  		x = stringHeader(" Foo : bar ");
38  		assertEquals("Foo", x.getName());
39  		assertEquals("bar", x.getValue());
40  
41  		x = stringHeader(" Foo : bar : baz ");
42  		assertEquals("Foo", x.getName());
43  		assertEquals("bar : baz", x.getValue());
44  
45  		x = stringHeader("Foo");
46  		assertEquals("Foo", x.getName());
47  		assertEquals("", x.getValue());
48  
49  		assertNull(stringHeader((String)null));
50  	}
51  
52  	@Test void a02_of() {
53  		var x = header("Foo","bar");
54  		assertString("Foo: bar", x);
55  		x = header("Foo",()->"bar");
56  		assertString("Foo: bar", x);
57  	}
58  
59  	@Test void a05_assertions() {
60  		var x = header("X1","1");
61  		x.assertName().is("X1").assertStringValue().is("1");
62  	}
63  
64  	@Test void a07_eqIC() {
65  		var x = header("X1","1");
66  		assertTrue(x.equalsIgnoreCase("1"));
67  		assertFalse(x.equalsIgnoreCase("2"));
68  		assertFalse(x.equalsIgnoreCase(null));
69  	}
70  
71  	@Test void a08_getElements() {
72  		var m = Value.of(1);
73  		var h1 = header("X1","1");
74  		var h2 = header("X2",()->m);
75  		var h3 = header("X3",null);
76  
77  		var x = h1.getElements();
78  
79  		assertEquals(1, x.length);
80  		assertEquals("1", x[0].getName());
81  		x = h1.getElements();
82  		assertEquals(1, x.length);
83  		assertEquals("1", x[0].getName());
84  
85  		x = h2.getElements();
86  		assertEquals(1, x.length);
87  		assertEquals("Value(1)", x[0].getName());
88  		m.set(2);
89  		x = h2.getElements();
90  		assertEquals(1, x.length);
91  		assertEquals("Value(2)", x[0].getName());
92  
93  		x = h3.getElements();
94  		assertEquals(0, x.length);
95  	}
96  
97  	@Test void a09_equals() {
98  		var h1 = header("Foo","bar");
99  		var h2 = header("Foo","bar");
100 		var h3 = header("Bar","bar");
101 		var h4 = header("Foo","baz");
102 		assertEquals(h1, h2);
103 		assertNotEquals(h1, h3);
104 		assertNotEquals(h1, h4);
105 		assertNotEquals(h1, "foo");
106 	}
107 
108 	//------------------------------------------------------------------------------------------------------------------
109 	// Utility methods
110 	//------------------------------------------------------------------------------------------------------------------
111 
112 	private static BasicHeader header(String name, Object val) {
113 		return basicHeader(name, val);
114 	}
115 
116 	private static BasicHeader header(String name, Supplier<?> val) {
117 		return basicHeader(name, val);
118 	}
119 }