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 java.io.*;
24
25 import org.apache.juneau.*;
26 import org.apache.juneau.http.part.*;
27 import org.apache.juneau.httppart.*;
28 import org.apache.juneau.rest.annotation.*;
29 import org.apache.juneau.rest.mock.*;
30 import org.apache.juneau.rest.servlet.*;
31 import org.apache.juneau.uon.*;
32 import org.apache.juneau.utest.utils.*;
33 import org.junit.jupiter.api.*;
34
35 class RestClient_Query_Test extends TestBase {
36
37 @Rest
38 public static class A extends BasicRestObject {
39 @RestGet
40 public Reader query(org.apache.juneau.rest.RestRequest req) {
41 return reader(req.getQueryParams().asQueryString());
42 }
43 }
44
45
46
47
48
49 @Test void a01_query_String_Object() throws Exception {
50 client().queryData("foo","bar").queryData(part("foo",new StringBuilder("baz"),null)).build().get("/query").run().assertContent("foo=bar&foo=baz");
51 client().build().get("/query").queryData("foo","bar").run().assertContent().isContains("foo=bar");
52 }
53
54 @Test void a02_query_String_Object_Schema() throws Exception {
55 var l = list("bar","baz");
56 client().build().get("/query").queryData(part("foo",l,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
57 client().queryData(part("foo",l,T_ARRAY_PIPES)).build().get("/query").run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
58 }
59
60 @Test void a03_query_String_Object_Schema_Serializer() throws Exception {
61 var l = list("bar","baz");
62 client().queryData(part("foo",l,T_ARRAY_PIPES).serializer(UonSerializer.DEFAULT)).build().get("/query").run().assertContent().asString().asUrlDecode().is("foo=@(bar,baz)");
63 }
64
65 @Test void a06_query_String_Supplier() throws Exception {
66 var l1 = list("foo","bar");
67 var l2 = list("bar","baz");
68 var s = MutableSupplier.of(l1);
69 var x = client().queryData(part("foo",s,null)).build();
70 x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=foo,bar");
71 s.set(l2);
72 x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=bar,baz");
73 }
74
75 @Test void a07_query_String_Supplier_Schema() throws Exception {
76 var l1 = a("foo","bar");
77 var l2 = a("bar","baz");
78 var s = MutableSupplier.of(l1);
79 var x = client().queryData(part("foo",s,T_ARRAY_PIPES)).build();
80 x.get("/query").queryData(part("bar",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=foo|bar&bar=foo|bar");
81 s.set(l2);
82 x.get("/query").queryData(part("bar",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz&bar=bar|baz");
83 }
84
85 @Test void a08_query_String_Supplier_Schema_Serializer() throws Exception {
86 var l1 = list("foo","bar");
87 var l2 = list("bar","baz");
88 var s = MutableSupplier.of(l1);
89 var x = client().queryData(part("foo",s,T_ARRAY_PIPES).serializer(FakeWriterSerializer.X)).build();
90 x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=xfoo|barx");
91 s.set(l2);
92 x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=xbar|bazx");
93 }
94
95 @Test void a09_query_NameValuePair() throws Exception {
96 client().queryData(part("foo","bar")).build().get("/query").queryData(part("foo","baz")).run().assertContent().isContains("foo=bar&foo=baz");
97 }
98
99 public static class A10 {
100 public String foo="bar";
101 }
102
103 @Test void a10_queries_Objects() throws Exception {
104 client().build().get("/query").queryData(part("foo","bar")).run().assertContent("foo=bar");
105 }
106
107 public static class A12 extends Reader {
108 @Override
109 public int read(char[] cbuf,int off,int len) throws IOException {
110 throw new IOException("foo");
111 }
112 @Override
113 public void close() throws IOException {}
114 }
115
116 @Test void a12_queryCustom_Object() throws Exception {
117 client().build().get("/query").queryCustom("foo=bar").run().assertContent().isContains("foo=bar");
118 assertThrowsWithMessage(Exception.class, "foo", ()->client().build().get("").queryCustom(new A12()));
119 }
120
121
122
123
124
125 private static BasicPart part(String name, Object val) {
126 return basicPart(name, val);
127 }
128
129 private static SerializedPart part(String name, Object val, HttpPartSchema schema) {
130 return serializedPart(name, val).schema(schema);
131 }
132
133 private static RestClient.Builder client() {
134 return MockRestClient.create(A.class).json5();
135 }
136 }