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.client;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.http.HttpParts.*;
22  import static org.apache.juneau.httppart.HttpPartSchema.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.http.part.*;
26  import org.apache.juneau.httppart.*;
27  import org.apache.juneau.rest.annotation.*;
28  import org.apache.juneau.rest.mock.*;
29  import org.apache.juneau.rest.servlet.*;
30  import org.junit.jupiter.api.*;
31  
32  class RestClient_Paths_Test extends TestBase {
33  
34  	@Rest
35  	public static class A extends BasicRestObject {
36  		@RestGet(path="/echo/*")
37  		public String getEcho(org.apache.juneau.rest.RestRequest req) {
38  			return req.toString();
39  		}
40  	}
41  
42  	//------------------------------------------------------------------------------------------------------------------
43  	// Method tests
44  	//------------------------------------------------------------------------------------------------------------------
45  
46  	public static class A1 {
47  		public int x;
48  		public A1 init() {
49  			x = 1;
50  			return this;
51  		}
52  		@Override
53  		public String toString() {
54  			return "xxx";
55  		}
56  	}
57  
58  	@Test void a01_path_String_Object() throws Exception {
59  		client().build().get("/echo/{x}").pathData("x",new A1().init()).run().assertContent().isContains("GET /echo/x=1 HTTP/1.1");
60  		client().build().get("/echo/*").pathData("/*",new A1().init()).run().assertContent().isContains("GET /echo/x=1 HTTP/1.1");
61  		assertThrowsWithMessage(IllegalStateException.class, "Path variable {y} was not found in path.", ()->client().build().get("/echo/{x}").pathData("y","foo").run());
62  	}
63  
64  	@Test void a02_path_NameValuePair() throws Exception {
65  		client().build().get("/echo/{x}").pathData(part("x","foo")).run().assertContent().isContains("GET /echo/foo HTTP/1.1");
66  	}
67  
68  	@Test void a03_paths_Object() throws Exception {
69  		client().build().get("/echo/{x}").pathData(part("x","foo")).run().assertContent().isContains("GET /echo/foo HTTP/1.1");
70  	}
71  
72  	@Test void a04_pathPairs_Objects() throws Exception {
73  		client().build().get("/echo/{x}").pathDataPairs("x","1").run().assertContent().isContains("GET /echo/1 HTTP/1.1");
74  		assertThrowsWithMessage(IllegalArgumentException.class, "Odd number of parameters passed into pathDataPairs(String...)", ()->client().build().get("/echo/{x}").pathDataPairs("x"));
75  	}
76  
77  	@Test void a05_path_String_Object_Schema() throws Exception {
78  		var a = a("foo","bar");
79  		client().build().get("/echo/{x}").pathData(part("x",a,T_ARRAY_PIPES)).run().assertContent().isContains("GET /echo/foo%7Cbar HTTP/1.1");
80  	}
81  
82  	//------------------------------------------------------------------------------------------------------------------
83  	// Helper methods.
84  	//------------------------------------------------------------------------------------------------------------------
85  
86  	private static BasicPart part(String name, Object val) {
87  		return basicPart(name, val);
88  	}
89  
90  	private static BasicPart part(String name, Object val, HttpPartSchema schema) {
91  		return serializedPart(name, val).schema(schema);
92  	}
93  
94  	private static RestClient.Builder client() {
95  		return MockRestClient.create(A.class).json5();
96  	}
97  }