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.rest.mock.*;
21  import org.junit.jupiter.api.*;
22  
23  class Rest_AllowedMethodHeaders_Test extends TestBase {
24  	//------------------------------------------------------------------------------------------------------------------
25  	// @Rest(allowedMethodHeaders)
26  	//------------------------------------------------------------------------------------------------------------------
27  
28  	public static class A {
29  		@RestOp
30  		public String get() {
31  			return "GET";
32  		}
33  		@RestOp
34  		public String put() {
35  			return "PUT";
36  		}
37  		@RestOp(method="foo",path="/")
38  		public String foo() {
39  			return "FOO";
40  		}
41  	}
42  
43  	@Rest()
44  	public static class A1 extends A {}
45  
46  	@Rest(allowedMethodHeaders="GET")
47  	public static class A2 extends A {}
48  
49  	@Rest(allowedMethodHeaders="get")
50  	public static class A3 extends A {}
51  
52  	@Rest(allowedMethodHeaders="FOO")
53  	public static class A4 extends A {}
54  
55  	@Rest(allowedMethodHeaders="*")
56  	public static class A5 extends A {}
57  
58  	@Rest(allowedMethodHeaders="NONE")
59  	public static class A6 extends A {}
60  
61  	@Rest(allowedMethodHeaders="None")
62  	public static class A7 extends A {}
63  
64  	@Rest(allowedMethodHeaders="None")
65  	public static class A8 extends A5 {}
66  
67  	@Test void c01_basic() throws Exception {
68  		var a1 = MockRestClient.build(A1.class);
69  		a1.get("/").run().assertContent("GET");
70  		a1.put("/", "").run().assertContent("PUT");
71  		a1.get("/").header("X-Method", "PUT").run().assertContent("GET");
72  		a1.put("/", "").header("X-Method", "GET").run().assertContent("PUT");
73  		a1.request("get","/").header("X-Method","FOO").run().assertContent("GET");
74  
75  		var a2 = MockRestClient.build(A2.class);
76  		a2.get("/").run().assertContent("GET");
77  		a2.put("/", "").run().assertContent("PUT");
78  		a2.get("/").header("X-Method", "PUT").run().assertContent("GET");
79  		a2.put("/", "").header("X-Method", "GET").run().assertContent("GET");
80  		a2.request("get","/").header("X-Method","FOO").run().assertContent("GET");
81  
82  		var a3 = MockRestClient.build(A3.class);
83  		a3.get("/").run().assertContent("GET");
84  		a3.put("/", "").run().assertContent("PUT");
85  		a3.get("/").header("X-Method", "PUT").run().assertContent("GET");
86  		a3.put("/", "").header("X-Method", "GET").run().assertContent("GET");
87  		a3.request("get","/").header("X-Method","FOO").run().assertContent("GET");
88  
89  		var a4 = MockRestClient.build(A4.class);
90  		a4.get("/").run().assertContent("GET");
91  		a4.put("/", "").run().assertContent("PUT");
92  		a4.get("/").header("X-Method", "PUT").run().assertContent("GET");
93  		a4.put("/", "").header("X-Method", "GET").run().assertContent("PUT");
94  		a4.request("get","/").header("X-Method","FOO").run().assertContent("FOO");
95  
96  		var a5 = MockRestClient.build(A5.class);
97  		a5.get("/").run().assertContent("GET");
98  		a5.put("/", "").run().assertContent("PUT");
99  		a5.get("/").header("X-Method", "PUT").run().assertContent("PUT");
100 		a5.put("/", "").header("X-Method", "GET").run().assertContent("GET");
101 		a5.get("/").header("x-method", "PUT").run().assertContent("PUT");
102 		a5.get("/").header("x-method", "FOO").run().assertContent("FOO");
103 		a5.get("/").header("X-Method", "put").run().assertContent("PUT");
104 		a5.get("/").header("X-Method", "foo").run().assertContent("FOO");
105 		a5.request("get","/").header("X-Method","FOO").run().assertContent("FOO");
106 
107 		var a6 = MockRestClient.build(A6.class);
108 		a6.get("/").run().assertContent("GET");
109 		a6.put("/", "").run().assertContent("PUT");
110 		a6.get("/").header("X-Method", "PUT").run().assertContent("GET");
111 		a6.put("/", "").header("X-Method", "GET").run().assertContent("PUT");
112 		a6.request("get","/").header("X-Method","FOO").run().assertContent("GET");
113 
114 		var a7 = MockRestClient.build(A7.class);
115 		a7.get("/").run().assertContent("GET");
116 		a7.put("/", "").run().assertContent("PUT");
117 		a7.get("/").header("X-Method", "PUT").run().assertContent("GET");
118 		a7.put("/", "").header("X-Method", "GET").run().assertContent("PUT");
119 		a7.request("get","/").header("X-Method","FOO").run().assertContent("GET");
120 
121 		var a8 = MockRestClient.build(A8.class);
122 		a8.get("/").run().assertContent("GET");
123 		a8.put("/", "").run().assertContent("PUT");
124 		a8.get("/").header("X-Method", "PUT").run().assertContent("GET");
125 		a8.put("/", "").header("X-Method", "GET").run().assertContent("PUT");
126 		a8.request("get","/").header("X-Method","FOO").run().assertContent("GET");
127 	}
128 }