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.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
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
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 }