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