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.mock;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  
21  import java.util.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.assertions.*;
25  import org.junit.jupiter.api.*;
26  
27  class PathResolverTest extends TestBase {
28  
29  	public static class PathResolver2 extends MockPathResolver {
30  
31  		public PathResolver2(String target, String contextPath, String servletPath, String pathToResolve, Map<String,Object> pathVars) {
32  			super(target, contextPath, servletPath, pathToResolve, pathVars);
33  		}
34  
35  		FluentStringAssertion<PathResolver2> assertUri() {
36  			return new FluentStringAssertion<>(getURI(), this);
37  		}
38  
39  		FluentStringAssertion<PathResolver2> assertContextPath() {
40  			return new FluentStringAssertion<>(getContextPath(), this);
41  		}
42  
43  		FluentStringAssertion<PathResolver2> assertServletPath() {
44  			return new FluentStringAssertion<>(getServletPath(), this);
45  		}
46  
47  		FluentStringAssertion<PathResolver2> assertPathInfo() {
48  			return new FluentStringAssertion<>(getRemainder(), this);
49  		}
50  
51  		FluentStringAssertion<PathResolver2> assertTarget() {
52  			return new FluentStringAssertion<>(getTarget(), this);
53  		}
54  
55  		FluentStringAssertion<PathResolver2> assertError() {
56  			return new FluentStringAssertion<>(getError(), this);
57  		}
58  	}
59  
60  	private static PathResolver2 create(String target, String contextPath, String servletPath, String pathToResolve, Map<String,Object> pathVars) {
61  		return new PathResolver2(target, contextPath, servletPath, pathToResolve, pathVars);
62  	}
63  
64  	@Test void basicDefaultTarget() {
65  		create(null, null, null, "/foo", null)
66  			.assertUri().is("http://localhost/foo")
67  			.assertTarget().is("http://localhost")
68  			.assertContextPath().is("")
69  			.assertServletPath().is("")
70  			.assertPathInfo().is("/foo");
71  
72  		create(null, null, "/sp", "/foo", null)
73  			.assertUri().is("http://localhost/sp/foo")
74  			.assertTarget().is("http://localhost")
75  			.assertContextPath().is("")
76  			.assertServletPath().is("/sp")
77  			.assertPathInfo().is("/foo");
78  
79  		create(null, "/cp", null, "/foo", null)
80  			.assertUri().is("http://localhost/cp/foo")
81  			.assertTarget().is("http://localhost")
82  			.assertContextPath().is("/cp")
83  			.assertServletPath().is("")
84  			.assertPathInfo().is("/foo");
85  
86  		create(null, "/cp", "/sp", "/foo", null)
87  			.assertUri().is("http://localhost/cp/sp/foo")
88  			.assertTarget().is("http://localhost")
89  			.assertContextPath().is("/cp")
90  			.assertServletPath().is("/sp")
91  			.assertPathInfo().is("/foo");
92  
93  		create(null, "/", "/", "/foo", null)
94  			.assertUri().is("http://localhost/foo")
95  			.assertTarget().is("http://localhost")
96  			.assertContextPath().is("")
97  			.assertServletPath().is("")
98  			.assertPathInfo().is("/foo");
99  
100 		create(null, "", "", "/foo", null)
101 			.assertUri().is("http://localhost/foo")
102 			.assertTarget().is("http://localhost")
103 			.assertContextPath().is("")
104 			.assertServletPath().is("")
105 			.assertPathInfo().is("/foo");
106 
107 		create(null, "", "", "/", null)
108 			.assertUri().is("http://localhost")
109 			.assertTarget().is("http://localhost")
110 			.assertContextPath().is("")
111 			.assertServletPath().is("")
112 			.assertPathInfo().is("");
113 
114 		create(null, "", "", "", null)
115 			.assertUri().is("http://localhost")
116 			.assertTarget().is("http://localhost")
117 			.assertContextPath().is("")
118 			.assertServletPath().is("")
119 			.assertPathInfo().is("");
120 
121 		create(null, "", "", null, null)
122 			.assertUri().is("http://localhost")
123 			.assertTarget().is("http://localhost")
124 			.assertContextPath().is("")
125 			.assertServletPath().is("")
126 			.assertPathInfo().is("");
127 
128 		create(null, "/cp/cp2", "/sp/sp2", "/foo/foo2", null)
129 			.assertUri().is("http://localhost/cp/cp2/sp/sp2/foo/foo2")
130 			.assertTarget().is("http://localhost")
131 			.assertContextPath().is("/cp/cp2")
132 			.assertServletPath().is("/sp/sp2")
133 			.assertPathInfo().is("/foo/foo2");
134 
135 		create(null, "cp/cp2/", "sp/sp2/", "foo/foo2/", null)
136 			.assertUri().is("http://localhost/cp/cp2/sp/sp2/foo/foo2")
137 			.assertTarget().is("http://localhost")
138 			.assertContextPath().is("/cp/cp2")
139 			.assertServletPath().is("/sp/sp2")
140 			.assertPathInfo().is("/foo/foo2");
141 	}
142 
143 	@Test void basicWithTarget() {
144 		create("http://foobar", null, null, "/foo", null)
145 			.assertUri().is("http://foobar/foo")
146 			.assertTarget().is("http://foobar")
147 			.assertContextPath().is("")
148 			.assertServletPath().is("")
149 			.assertPathInfo().is("/foo");
150 
151 		create("http://foobar", null, "/sp", "/foo", null)
152 			.assertUri().is("http://foobar/sp/foo")
153 			.assertTarget().is("http://foobar")
154 			.assertContextPath().is("")
155 			.assertServletPath().is("/sp")
156 			.assertPathInfo().is("/foo");
157 
158 		create("http://foobar", "/cp", null, "/foo", null)
159 			.assertUri().is("http://foobar/cp/foo")
160 			.assertTarget().is("http://foobar")
161 			.assertContextPath().is("/cp")
162 			.assertServletPath().is("")
163 			.assertPathInfo().is("/foo");
164 
165 		create("http://foobar", "/cp", "/sp", "/foo", null)
166 			.assertUri().is("http://foobar/cp/sp/foo")
167 			.assertTarget().is("http://foobar")
168 			.assertContextPath().is("/cp")
169 			.assertServletPath().is("/sp")
170 			.assertPathInfo().is("/foo");
171 
172 		create("http://foobar", "/", "/", "/foo", null)
173 			.assertUri().is("http://foobar/foo")
174 			.assertTarget().is("http://foobar")
175 			.assertContextPath().is("")
176 			.assertServletPath().is("")
177 			.assertPathInfo().is("/foo");
178 
179 		create("http://foobar", "", "", "/foo", null)
180 			.assertUri().is("http://foobar/foo")
181 			.assertTarget().is("http://foobar")
182 			.assertContextPath().is("")
183 			.assertServletPath().is("")
184 			.assertPathInfo().is("/foo");
185 
186 		create("http://foobar", "", "", "/", null)
187 			.assertUri().is("http://foobar")
188 			.assertTarget().is("http://foobar")
189 			.assertContextPath().is("")
190 			.assertServletPath().is("")
191 			.assertPathInfo().is("");
192 
193 		create("http://foobar", "", "", "", null)
194 			.assertUri().is("http://foobar")
195 			.assertTarget().is("http://foobar")
196 			.assertContextPath().is("")
197 			.assertServletPath().is("")
198 			.assertPathInfo().is("");
199 
200 		create("http://foobar", "", "", null, null)
201 			.assertUri().is("http://foobar")
202 			.assertTarget().is("http://foobar")
203 			.assertContextPath().is("")
204 			.assertServletPath().is("")
205 			.assertPathInfo().is("");
206 	}
207 
208 	@Test void basicWithPathVars() {
209 		Map<String,Object> vars = map("foo","123");
210 
211 		create(null, null, null, "/foo", vars)
212 			.assertUri().is("http://localhost/foo")
213 			.assertTarget().is("http://localhost")
214 			.assertContextPath().is("")
215 			.assertServletPath().is("")
216 			.assertPathInfo().is("/foo");
217 
218 		create(null, "/cp/{foo}", "/sp/{foo}", "/foo/{foo}", vars)
219 			.assertUri().is("http://localhost/cp/123/sp/123/foo/{foo}")
220 			.assertTarget().is("http://localhost")
221 			.assertContextPath().is("/cp/123")
222 			.assertServletPath().is("/sp/123")
223 			.assertPathInfo().is("/foo/{foo}");
224 	}
225 
226 	@Test void fullPaths() {
227 		Map<String,Object> vars = map("foo","123");
228 
229 		create(null, null, null, "http://foobar/foo", vars)
230 			.assertUri().is("http://foobar/foo")
231 			.assertTarget().is("http://foobar")
232 			.assertContextPath().is("")
233 			.assertServletPath().is("")
234 			.assertPathInfo().is("/foo");
235 
236 		create(null, null, null, "http://foobar/foo/bar", vars)
237 			.assertUri().is("http://foobar/foo/bar")
238 			.assertTarget().is("http://foobar")
239 			.assertContextPath().is("")
240 			.assertServletPath().is("")
241 			.assertPathInfo().is("/foo/bar");
242 
243 		create(null, "/foo", null, "http://foobar/foo/bar", vars)
244 			.assertError().isNull()
245 			.assertUri().is("http://foobar/foo/bar")
246 			.assertTarget().is("http://foobar")
247 			.assertContextPath().is("/foo")
248 			.assertServletPath().is("")
249 			.assertPathInfo().is("/bar");
250 
251 		create(null, "/", "/foo", "http://foobar/foo/bar", vars)
252 			.assertError().isNull()
253 			.assertUri().is("http://foobar/foo/bar")
254 			.assertTarget().is("http://foobar")
255 			.assertContextPath().is("")
256 			.assertServletPath().is("/foo")
257 			.assertPathInfo().is("/bar");
258 
259 		create(null, "/foo", "/bar", "http://foobar/foo/bar", vars)
260 			.assertError().isNull()
261 			.assertUri().is("http://foobar/foo/bar")
262 			.assertTarget().is("http://foobar")
263 			.assertContextPath().is("/foo")
264 			.assertServletPath().is("/bar")
265 			.assertPathInfo().is("");
266 
267 		create(null, "/foo", "/bar", "http://foobar/foo/bar/baz", vars)
268 			.assertError().isNull()
269 			.assertUri().is("http://foobar/foo/bar/baz")
270 			.assertTarget().is("http://foobar")
271 			.assertContextPath().is("/foo")
272 			.assertServletPath().is("/bar")
273 			.assertPathInfo().is("/baz");
274 
275 		create(null, "/foo/bar", "/baz", "http://foobar/foo/bar/baz", vars)
276 			.assertError().isNull()
277 			.assertUri().is("http://foobar/foo/bar/baz")
278 			.assertTarget().is("http://foobar")
279 			.assertContextPath().is("/foo/bar")
280 			.assertServletPath().is("/baz")
281 			.assertPathInfo().is("");
282 
283 		create(null, "/foo", "/bar/baz", "http://foobar/foo/bar/baz", vars)
284 			.assertError().isNull()
285 			.assertUri().is("http://foobar/foo/bar/baz")
286 			.assertTarget().is("http://foobar")
287 			.assertContextPath().is("/foo")
288 			.assertServletPath().is("/bar/baz")
289 			.assertPathInfo().is("");
290 
291 		create(null, "/foo", "/bar/baz", "http://foobar/foo/bar/baz/qux", vars)
292 			.assertError().isNull()
293 			.assertUri().is("http://foobar/foo/bar/baz/qux")
294 			.assertTarget().is("http://foobar")
295 			.assertContextPath().is("/foo")
296 			.assertServletPath().is("/bar/baz")
297 			.assertPathInfo().is("/qux");
298 
299 		create(null, "/foo", "/bar/baz", "http://foobar/foo/bar/baz/qux/quux", vars)
300 			.assertError().isNull()
301 			.assertUri().is("http://foobar/foo/bar/baz/qux/quux")
302 			.assertTarget().is("http://foobar")
303 			.assertContextPath().is("/foo")
304 			.assertServletPath().is("/bar/baz")
305 			.assertPathInfo().is("/qux/quux");
306 
307 		create(null, "/foo", "/bar/{x}", "http://foobar/foo/bar/baz/qux/quux", vars)
308 			.assertError().isNull()
309 			.assertUri().is("http://foobar/foo/bar/baz/qux/quux")
310 			.assertTarget().is("http://foobar")
311 			.assertContextPath().is("/foo")
312 			.assertServletPath().is("/bar/baz")
313 			.assertPathInfo().is("/qux/quux");
314 		create(null, "/{x}", "/{x}/{x}", "http://foobar/foo/bar/baz/qux/quux", vars)
315 			.assertError().isNull()
316 			.assertUri().is("http://foobar/foo/bar/baz/qux/quux")
317 			.assertTarget().is("http://foobar")
318 			.assertContextPath().is("/foo")
319 			.assertServletPath().is("/bar/baz")
320 			.assertPathInfo().is("/qux/quux");
321 		create(null, "/{bar}", "/{bar}/{bar}", "http://foobar/foo/bar/baz/qux/quux", vars)
322 			.assertError().isNull()
323 			.assertUri().is("http://foobar/foo/bar/baz/qux/quux")
324 			.assertTarget().is("http://foobar")
325 			.assertContextPath().is("/foo")
326 			.assertServletPath().is("/bar/baz")
327 			.assertPathInfo().is("/qux/quux");
328 
329 		create(null, null, null, "http://foobar", vars)
330 			.assertUri().is("http://foobar")
331 			.assertTarget().is("http://foobar")
332 			.assertContextPath().is("")
333 			.assertServletPath().is("")
334 			.assertPathInfo().is("");
335 
336 		create(null, null, null, "http://foobar/", vars)
337 			.assertUri().is("http://foobar/")
338 			.assertTarget().is("http://foobar")
339 			.assertContextPath().is("")
340 			.assertServletPath().is("")
341 			.assertPathInfo().is("/");
342 
343 		create(null, "/foo", null, "http://foobar/foo", vars)
344 			.assertUri().is("http://foobar/foo")
345 			.assertTarget().is("http://foobar")
346 			.assertContextPath().is("/foo")
347 			.assertServletPath().is("")
348 			.assertPathInfo().is("");
349 	}
350 
351 	@Test void errors() {
352 		create(null, null, null, "http://", null)
353 			.assertError().is("Invalid URI pattern encountered:  http://");
354 		create(null, null, null, "http:///", null)
355 			.assertError().is("Invalid URI pattern encountered:  http:///");
356 		create(null, "/foo", null, "http://localhost/bar/baz", null)
357 			.assertError().is("Context path [/foo] not found in URI:  http://localhost/bar/baz");
358 		create(null, "/foo", "/bar", "http://localhost/foo/baz/qux", null)
359 			.assertError().is("Servlet path [/bar] not found in URI:  http://localhost/foo/baz/qux");
360 	}
361 }