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_AllowedMethodParams_Test extends TestBase {
24  
25  	//------------------------------------------------------------------------------------------------------------------
26  	// @Rest(allowedMethodParams)
27  	//------------------------------------------------------------------------------------------------------------------
28  
29  	public static class A {
30  		@RestOp
31  		public String get() {
32  			return "GET";
33  		}
34  		@RestOp
35  		public String put() {
36  			return "PUT";
37  		}
38  		@RestOp
39  		public String head() {
40  			// Note that HTTP client is going to ignore this body.
41  			return "HEAD";
42  		}
43  		@RestOp
44  		public String options() {
45  			return "OPTIONS";
46  		}
47  		@RestOp(method="foo",path="/")
48  		public String foo() {
49  			return "FOO";
50  		}
51  	}
52  
53  	@Rest()
54  	public static class A1 extends A {}
55  
56  	@Rest(allowedMethodParams="GET")
57  	public static class A2 extends A {}
58  
59  	@Rest(allowedMethodParams="get")
60  	public static class A3 extends A {}
61  
62  	@Rest(allowedMethodParams="FOO")
63  	public static class A4 extends A {}
64  
65  	@Rest(allowedMethodParams="*")
66  	public static class A5 extends A {}
67  
68  	@Rest(allowedMethodParams="NONE")
69  	public static class A6 extends A {}
70  
71  	@Rest(allowedMethodParams="None")
72  	public static class A7 extends A {}
73  
74  	@Rest(allowedMethodParams="None")
75  	public static class A8 extends A5 {}
76  
77  	@Test void a01_basic() throws Exception {
78  		var a1 = MockRestClient.build(A1.class);
79  		a1.get("/").run().assertContent("GET");
80  		a1.put("/", "").run().assertContent("PUT");
81  		a1.head("/").run().assertContent("");
82  		a1.options("/").run().assertContent("OPTIONS");
83  		a1.get("/?method=PUT").run().assertContent("GET");
84  		a1.put("/?method=GET", "").run().assertContent("PUT");
85  		a1.get("/?method=HEAD").run().assertContent("HEAD");
86  		a1.get("/?method=OPTIONS").run().assertContent("OPTIONS");
87  		a1.request("get","/?method=FOO").run().assertContent("GET");
88  
89  		var a2 = MockRestClient.build(A2.class);
90  		a2.get("/").run().assertContent("GET");
91  		a2.put("/", "").run().assertContent("PUT");
92  		a2.head("/").run().assertContent("");
93  		a2.options("/").run().assertContent("OPTIONS");
94  		a2.get("/?method=PUT").run().assertContent("GET");
95  		a2.put("/?method=GET", "").run().assertContent("GET");
96  		a2.get("/?method=HEAD").run().assertContent("GET");
97  		a2.get("/?method=OPTIONS").run().assertContent("GET");
98  		a2.request("get","/?method=FOO").run().assertContent("GET");
99  
100 		var a3 = MockRestClient.build(A3.class);
101 		a3.get("/").run().assertContent("GET");
102 		a3.put("/", "").run().assertContent("PUT");
103 		a3.head("/").run().assertContent("");
104 		a3.options("/").run().assertContent("OPTIONS");
105 		a3.get("/?method=PUT").run().assertContent("GET");
106 		a3.put("/?method=GET", "").run().assertContent("GET");
107 		a3.get("/?method=HEAD").run().assertContent("GET");
108 		a3.get("/?method=OPTIONS").run().assertContent("GET");
109 		a3.request("get","/?method=FOO").run().assertContent("GET");
110 
111 		var a4 = MockRestClient.build(A4.class);
112 		a4.get("/").run().assertContent("GET");
113 		a4.put("/", "").run().assertContent("PUT");
114 		a4.head("/").run().assertContent("");
115 		a4.options("/").run().assertContent("OPTIONS");
116 		a4.get("/?method=PUT").run().assertContent("GET");
117 		a4.put("/?method=GET", "").run().assertContent("PUT");
118 		a4.get("/?method=HEAD").run().assertContent("GET");
119 		a4.get("/?method=OPTIONS").run().assertContent("GET");
120 		a4.request("get","/?method=FOO").run().assertContent("FOO");
121 
122 		var a5 = MockRestClient.build(A5.class);
123 		a5.get("/").run().assertContent("GET");
124 		a5.put("/", "").run().assertContent("PUT");
125 		a5.head("/").run().assertContent("");
126 		a5.options("/").run().assertContent("OPTIONS");
127 		a5.get("/?method=PUT").run().assertContent("PUT");
128 		a5.put("/?method=GET", "").run().assertContent("GET");
129 		a5.get("/?method=HEAD").run().assertContent("HEAD");
130 		a5.get("/?method=OPTIONS").run().assertContent("OPTIONS");
131 		a5.request("get","/?method=FOO").run().assertContent("FOO");
132 		a5.get("/?method=Put").run().assertContent("PUT");
133 		a5.get("/?method=Foo").run().assertContent("FOO");
134 
135 		var a6 = MockRestClient.build(A6.class);
136 		a6.get("/").run().assertContent("GET");
137 		a6.put("/", "").run().assertContent("PUT");
138 		a6.head("/").run().assertContent("");
139 		a6.options("/").run().assertContent("OPTIONS");
140 		a6.get("/?method=PUT").run().assertContent("GET");
141 		a6.put("/?method=GET", "").run().assertContent("PUT");
142 		a6.get("/?method=HEAD").run().assertContent("GET");
143 		a6.get("/?method=OPTIONS").run().assertContent("GET");
144 		a6.request("get","/?method=FOO").run().assertContent("GET");
145 
146 		var a7 = MockRestClient.build(A7.class);
147 		a7.get("/").run().assertContent("GET");
148 		a7.put("/", "").run().assertContent("PUT");
149 		a7.head("/").run().assertContent("");
150 		a7.options("/").run().assertContent("OPTIONS");
151 		a7.get("/?method=PUT").run().assertContent("GET");
152 		a7.put("/?method=GET", "").run().assertContent("PUT");
153 		a7.get("/?method=HEAD").run().assertContent("GET");
154 		a7.get("/?method=OPTIONS").run().assertContent("GET");
155 		a7.request("get","/?method=FOO").run().assertContent("GET");
156 
157 		var a8 = MockRestClient.build(A8.class);
158 		a8.get("/").run().assertContent("GET");
159 		a8.put("/", "").run().assertContent("PUT");
160 		a8.head("/").run().assertContent("");
161 		a8.options("/").run().assertContent("OPTIONS");
162 		a8.get("/?method=PUT").run().assertContent("GET");
163 		a8.put("/?method=GET", "").run().assertContent("PUT");
164 		a8.get("/?method=HEAD").run().assertContent("GET");
165 		a8.get("/?method=OPTIONS").run().assertContent("GET");
166 		a8.request("get","/?method=FOO").run().assertContent("GET");
167 	}
168 }