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.apache.juneau.http.HttpParts.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.util.*;
25  
26  import org.apache.http.*;
27  import org.apache.juneau.*;
28  import org.apache.juneau.http.header.*;
29  import org.apache.juneau.http.part.*;
30  import org.apache.juneau.internal.*;
31  import org.apache.juneau.marshaller.*;
32  import org.junit.jupiter.api.*;
33  
34  class HttpHeaders_Test extends TestBase {
35  
36  	@Test void a01_cast() {
37  		var x1 = part("X1","1");
38  		var x2 = serializedPart("X2","2");
39  		var x3 = header("X3","3");
40  		var x4 = serializedHeader("X4","4");
41  		Map.Entry<String,Object> x5 = CollectionUtils.map("X5",(Object)"5").entrySet().iterator().next();
42  		org.apache.http.message.BasicNameValuePair x6 = new org.apache.http.message.BasicNameValuePair("X6","6");
43  		NameValuePairable x7 = () -> part("X7","7");
44  		Headerable x8 = () -> header("X8","8");
45  		SerializedPart x9 = serializedPart("X9",()->"9");
46  
47  		assertTypeAndJson(Header.class, "'X1: 1'", HttpHeaders.cast(x1));
48  		assertTypeAndJson(Header.class, "'X2: 2'", HttpHeaders.cast(x2));
49  		assertTypeAndJson(Header.class, "'X3: 3'", HttpHeaders.cast(x3));
50  		assertTypeAndJson(Header.class, "'X4: 4'", HttpHeaders.cast(x4));
51  		assertTypeAndJson(Header.class, "'X5: 5'", HttpHeaders.cast(x5));
52  		assertTypeAndJson(Header.class, "'X6: 6'", HttpHeaders.cast(x6));
53  		assertTypeAndJson(Header.class, "'X7: 7'", HttpHeaders.cast(x7));
54  		assertTypeAndJson(Header.class, "'X8: 8'", HttpHeaders.cast(x8));
55  		assertTypeAndJson(Header.class, "'X9: 9'", HttpHeaders.cast(x9));
56  
57  		assertThrowsWithMessage(BasicRuntimeException.class, "Object of type java.lang.String could not be converted to a Header.", ()->HttpHeaders.cast("X"));
58  		assertThrowsWithMessage(BasicRuntimeException.class, "Object of type null could not be converted to a Header.", ()->HttpHeaders.cast(null));
59  
60  		assertTrue(HttpHeaders.canCast(x1));
61  		assertTrue(HttpHeaders.canCast(x2));
62  		assertTrue(HttpHeaders.canCast(x3));
63  		assertTrue(HttpHeaders.canCast(x4));
64  		assertTrue(HttpHeaders.canCast(x5));
65  		assertTrue(HttpHeaders.canCast(x6));
66  		assertTrue(HttpHeaders.canCast(x7));
67  		assertTrue(HttpHeaders.canCast(x8));
68  		assertTrue(HttpHeaders.canCast(x9));
69  
70  		assertFalse(HttpHeaders.canCast("X"));
71  		assertFalse(HttpHeaders.canCast(null));
72  	}
73  
74  	//------------------------------------------------------------------------------------------------------------------
75  	// Utility methods
76  	//------------------------------------------------------------------------------------------------------------------
77  
78  	private BasicHeader header(String name, Object val) {
79  		return basicHeader(name, val);
80  	}
81  
82  	private BasicPart part(String name, Object val) {
83  		return basicPart(name, val);
84  	}
85  
86  	/**
87  	 * Asserts the JSON5 representation of the specified object.
88  	 */
89  	private void assertTypeAndJson(Class<?> c, String json, Object value) {
90  		assertInstanceOf(c, value);
91  		assertEquals(json, Json5.DEFAULT.write(value));
92  	}
93  }