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 java.io.*;
25
26 import org.apache.http.*;
27 import org.apache.juneau.*;
28 import org.apache.juneau.collections.*;
29 import org.apache.juneau.http.part.*;
30 import org.apache.juneau.httppart.*;
31 import org.apache.juneau.rest.annotation.*;
32 import org.apache.juneau.rest.mock.*;
33 import org.apache.juneau.rest.servlet.*;
34 import org.apache.juneau.serializer.*;
35 import org.apache.juneau.testutils.pojos.*;
36 import org.apache.juneau.uon.*;
37 import org.apache.juneau.utest.utils.*;
38 import org.junit.jupiter.api.*;
39
40 class RestClient_FormData_Test extends TestBase {
41
42 @Rest
43 public static class A extends BasicRestObject {
44 @RestPost
45 public Reader formData(org.apache.juneau.rest.RestRequest req) {
46 return reader(req.getFormParams().asQueryString());
47 }
48 }
49
50
51
52
53
54 @Test void a01_formData_String_Object() throws Exception {
55 client().formData("foo","bar").formData(part("foo",new StringBuilder("baz"),null)).build().post("/formData").run().assertContent("foo=bar&foo=baz");
56 client().build().post("/formData").formData("foo","bar").formData("foo",new StringBuilder("baz")).run().assertContent("foo=bar&foo=baz");
57 client().build().post("/formData").formData(null,"bar").run().assertContent("");
58 client().build().post("/formData").formData("foo",null).run().assertContent("");
59 client().build().post("/formData").formData(null,(String)null).run().assertContent("");
60 }
61
62 @Test void a03_formData_NameValuePair() throws Exception {
63 client().formData(part("foo","bar")).build().post("/formData").formData(part("foo","baz")).run().assertContent("foo=bar&foo=baz");
64 }
65
66 @Test void a04_formDatas_Objects() throws Exception {
67 client().formData(part("foo","bar")).build().post("/formData").run().assertContent("foo=bar");
68 client().formData(parts("foo","bar","foo","baz").getAll()).build().post("/formData").run().assertContent("foo=bar&foo=baz");
69 client().formData(part("foo","bar"),part("foo","baz")).build().post("/formData").run().assertContent("foo=bar&foo=baz");
70
71 client().build().post("/formData").formData(part("foo","bar")).run().assertContent("foo=bar");
72 client().build().post("/formData").formData(part("foo","bar"),part("foo","baz")).run().assertContent("foo=bar&foo=baz");
73
74 client().build().post("/formData").formDataBean(ABean.get()).run().assertContent("a=1&b=foo");
75
76 client().formData(part("foo","bar"),null).build().post("/formData").run().assertContent("foo=bar");
77 client().formData(part("foo",null)).build().post("/formData").run().assertContent("");
78 client().formData(part(null,null)).build().post("/formData").run().assertContent("");
79
80 client().build().post("/formData").formData(part("foo",null)).run().assertContent("");
81 client().build().post("/formData").formData(part(null,"foo")).run().assertContent("");
82 client().build().post("/formData").formData(part(null,null)).run().assertContent("");
83
84 client().formData(part("foo","bar",null)).build().post("/formData").run().assertContent("foo=bar");
85 client().formData(part("foo",null,null)).build().post("/formData").run().assertContent("");
86 client().formData(part("foo",null,null).skipIfEmpty().schema(HttpPartSchema.create().default_("bar").build())).build().post("/formData").run().assertContent("foo=bar");
87 }
88
89 @Test void a06_formData_String_Object_Schema() throws Exception {
90 var l = l("bar","baz");
91 var l2 = l("qux","quux");
92 client().formData(part("foo",l,T_ARRAY_PIPES)).build().post("/formData").formData(part("foo",l2,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz&foo=qux|quux");
93 }
94
95 @Test void a07_formData_String_Object_Schema_Serializer() throws Exception {
96 var l = l("bar","baz");
97 client().formData(part("foo",l,T_ARRAY_PIPES).serializer(UonSerializer.DEFAULT)).build().post("/formData").run().assertContent().asString().asUrlDecode().is("foo=@(bar,baz)");
98 }
99
100 @Test void a09_formData_String_Supplier() throws Exception {
101 var s = MutableSupplier.of(null);
102
103 var x1 = client().formData(part("foo",s,null)).build();
104 s.set(JsonList.of("foo","bar"));
105 x1.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=foo,bar");
106 s.set(JsonList.of("bar","baz"));
107 x1.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=bar,baz");
108
109 var x2 = client().build();
110 s.set(JsonList.of("foo","bar"));
111 x2.post("/formData").formData("foo",s).run().assertContent().asString().asUrlDecode().is("foo=foo,bar");
112 s.set(JsonList.of("bar","baz"));
113 x2.post("/formData").formData("foo",s).run().assertContent().asString().asUrlDecode().is("foo=bar,baz");
114 }
115
116 @Test void a10_formData_String_Supplier_Schema_Serializer() throws Exception {
117 var s = MutableSupplier.of(JsonList.of("foo","bar"));
118 var x = client().formData(part("foo",s,T_ARRAY_PIPES).serializer(FakeWriterSerializer.X)).build();
119 x.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=xfoo|barx");
120 s.set(JsonList.of("bar","baz"));
121 x.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=xbar|bazx");
122 }
123
124 @Test void a11_formData_String_Supplier_Schema() throws Exception {
125 var l1 = l("foo","bar");
126 var l2 = l("bar","baz");
127 var s = MutableSupplier.of(null);
128
129 var x1 = client().formData(part("foo",s,T_ARRAY_PIPES)).build();
130 s.set(l1);
131 x1.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=foo|bar");
132 s.set(l2);
133 x1.post("/formData").run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
134
135 var x2 = client().build();
136 s.set(l1);
137 x2.post("/formData").formData(part("foo",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=foo|bar");
138 s.set(l2);
139 x2.post("/formData").formData(part("foo",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
140 }
141
142 public static class A12 implements HttpPartSerializer {
143 @Override
144 public HttpPartSerializerSession getPartSession() {
145 return (type, schema, value) -> {
146 throw new SerializeException("bad");
147 };
148 }
149 }
150
151 @Test void a12_badSerialization() {
152 assertThrowsWithMessage(Exception.class, "bad", ()->client().formData(part("Foo","bar",null).serializer(new A12())).build().post("/").run());
153 }
154
155
156
157
158
159 private static NameValuePair part(String name, Object val) {
160 return basicPart(name, val);
161 }
162
163 private static SerializedPart part(String name, Object val, HttpPartSchema schema) {
164 return serializedPart(name, val).schema(schema);
165 }
166
167 private static PartList parts(String...pairs) {
168 return partList(pairs);
169 }
170
171 private static RestClient.Builder client() {
172 return MockRestClient.create(A.class).json5();
173 }
174 }