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.rest.annotation;
18  
19  import org.apache.juneau.*;
20  import org.apache.juneau.collections.*;
21  import org.apache.juneau.rest.httppart.*;
22  import org.apache.juneau.rest.mock.*;
23  import org.junit.jupiter.api.*;
24  
25  class RestOp_ReqHeaders_Test extends TestBase {
26  
27  	//------------------------------------------------------------------------------------------------------------------
28  	// Default values - Default request headers
29  	//------------------------------------------------------------------------------------------------------------------
30  
31  	@Rest
32  	public static class A {
33  		@RestOp(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
34  		public JsonMap a(RequestHeaders headers) {
35  			return JsonMap.create()
36  				.append("h1", headers.get("H1").orElse(null))
37  				.append("h2", headers.get("H2").orElse(null))
38  				.append("h3", headers.get("H3").orElse(null));
39  		}
40  		@RestGet(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
41  		public JsonMap b(RequestHeaders headers) {
42  			return JsonMap.create()
43  				.append("h1", headers.get("H1").orElse(null))
44  				.append("h2", headers.get("H2").orElse(null))
45  				.append("h3", headers.get("H3").orElse(null));
46  		}
47  		@RestPut(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
48  		public JsonMap c(RequestHeaders headers) {
49  			return JsonMap.create()
50  				.append("h1", headers.get("H1").orElse(null))
51  				.append("h2", headers.get("H2").orElse(null))
52  				.append("h3", headers.get("H3").orElse(null));
53  		}
54  		@RestPost(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
55  		public JsonMap d(RequestHeaders headers) {
56  			return JsonMap.create()
57  				.append("h1", headers.get("H1").orElse(null))
58  				.append("h2", headers.get("H2").orElse(null))
59  				.append("h3", headers.get("H3").orElse(null));
60  		}
61  		@RestDelete(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
62  		public JsonMap e(RequestHeaders headers) {
63  			return JsonMap.create()
64  				.append("h1", headers.get("H1").orElse(null))
65  				.append("h2", headers.get("H2").orElse(null))
66  				.append("h3", headers.get("H3").orElse(null));
67  		}
68  	}
69  
70  	@Test void a01_reqHeaders() throws Exception {
71  		var a = MockRestClient.build(A.class);
72  
73  		a.get("/a").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
74  		a.get("/a").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
75  		a.get("/a").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
76  
77  		a.get("/b").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
78  		a.get("/b").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
79  		a.get("/b").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
80  
81  		a.put("/c").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
82  		a.put("/c").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
83  		a.put("/c").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
84  
85  		a.post("/d").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
86  		a.post("/d").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
87  		a.post("/d").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
88  
89  		a.delete("/e").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
90  		a.delete("/e").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
91  		a.delete("/e").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
92  	}
93  
94  	//------------------------------------------------------------------------------------------------------------------
95  	// Default values - Default request headers, case-insensitive matching
96  	//------------------------------------------------------------------------------------------------------------------
97  
98  	@Rest
99  	public static class B {
100 		@RestGet(defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "})
101 		public JsonMap a(RequestHeaders headers) {
102 			return JsonMap.create()
103 				.append("h1", headers.get("h1").orElse(null))
104 				.append("h2", headers.get("h2").orElse(null))
105 				.append("h3", headers.get("h3").orElse(null));
106 		}
107 	}
108 
109 	@Test void b01_reqHeadersCaseInsensitive() throws Exception {
110 		var b = MockRestClient.build(B.class);
111 		b.get("/a").run().assertContent("{h1:'1',h2:'2',h3:'3'}");
112 		b.get("/a").header("H1",4).header("H2",5).header("H3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
113 		b.get("/a").header("h1",4).header("h2",5).header("h3",6).run().assertContent("{h1:'4',h2:'5',h3:'6'}");
114 	}
115 }