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