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.TestUtils.*;
20  import static org.apache.juneau.http.HttpHeaders.*;
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.http.header.*;
27  import org.junit.jupiter.api.*;
28  
29  class BasicHeader_Test extends TestBase {
30  
31  	@Test void a01_ofPair() {
32  		var x = stringHeader("Foo:bar");
33  		assertEquals("Foo", x.getName());
34  		assertEquals("bar", x.getValue());
35  
36  		x = stringHeader(" Foo : bar ");
37  		assertEquals("Foo", x.getName());
38  		assertEquals("bar", x.getValue());
39  
40  		x = stringHeader(" Foo : bar : baz ");
41  		assertEquals("Foo", x.getName());
42  		assertEquals("bar : baz", x.getValue());
43  
44  		x = stringHeader("Foo");
45  		assertEquals("Foo", x.getName());
46  		assertEquals("", x.getValue());
47  
48  		assertNull(stringHeader((String)null));
49  	}
50  
51  	@Test void a02_of() {
52  		var x = header("Foo","bar");
53  		assertString("Foo: bar", x);
54  		x = header("Foo",()->"bar");
55  		assertString("Foo: bar", x);
56  	}
57  
58  	@Test void a05_assertions() {
59  		var x = header("X1","1");
60  		x.assertName().is("X1").assertStringValue().is("1");
61  	}
62  
63  	@Test void a07_eqIC() {
64  		var x = header("X1","1");
65  		assertTrue(x.equalsIgnoreCase("1"));
66  		assertFalse(x.equalsIgnoreCase("2"));
67  		assertFalse(x.equalsIgnoreCase(null));
68  	}
69  
70  	@Test void a08_getElements() {
71  		var m = Value.of(1);
72  		var h1 = header("X1","1");
73  		var h2 = header("X2",()->m);
74  		var h3 = header("X3",null);
75  
76  		var x = h1.getElements();
77  
78  		assertEquals(1, x.length);
79  		assertEquals("1", x[0].getName());
80  		x = h1.getElements();
81  		assertEquals(1, x.length);
82  		assertEquals("1", x[0].getName());
83  
84  		x = h2.getElements();
85  		assertEquals(1, x.length);
86  		assertEquals("Value(1)", x[0].getName());
87  		m.set(2);
88  		x = h2.getElements();
89  		assertEquals(1, x.length);
90  		assertEquals("Value(2)", x[0].getName());
91  
92  		x = h3.getElements();
93  		assertEquals(0, x.length);
94  	}
95  
96  	@Test void a09_equals() {
97  		var h1 = header("Foo","bar");
98  		var h2 = header("Foo","bar");
99  		var h3 = header("Bar","bar");
100 		var h4 = header("Foo","baz");
101 		assertEquals(h1, h2);
102 		assertNotEquals(h1, h3);
103 		assertNotEquals(h1, h4);
104 		assertNotEquals(h1, "foo");
105 	}
106 
107 	//------------------------------------------------------------------------------------------------------------------
108 	// Utility methods
109 	//------------------------------------------------------------------------------------------------------------------
110 
111 	private BasicHeader header(String name, Object val) {
112 		return basicHeader(name, val);
113 	}
114 
115 	private BasicHeader header(String name, Supplier<?> val) {
116 		return basicHeader(name, val);
117 	}
118 }