View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  	// Method tests
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 	// Helper methods.
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 }