1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }