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.httppart.HttpPartCollectionFormat.*;
21  import static org.apache.juneau.uon.ParamFormat.*;
22  
23  import java.io.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.collections.*;
27  import org.apache.juneau.httppart.*;
28  import org.apache.juneau.rest.annotation.*;
29  import org.apache.juneau.rest.httppart.*;
30  import org.apache.juneau.rest.mock.*;
31  import org.apache.juneau.rest.servlet.*;
32  import org.junit.jupiter.api.*;
33  
34  class RestClient_Config_OpenApi_Test extends TestBase {
35  
36  	@Rest
37  	public static class A extends BasicRestObject {
38  		@RestPost
39  		public Reader echoBody(org.apache.juneau.rest.RestRequest req) throws IOException {
40  			return req.getContent().getReader();
41  		}
42  		@RestGet
43  		public String[] checkHeader(org.apache.juneau.rest.RestRequest req) {
44  			return req.getHeaders().getAll(req.getHeaderParam("Check").orElse(null)).stream().map(RequestHeader::getValue).toArray(String[]::new);
45  		}
46  		@RestGet
47  		public Reader checkQuery(org.apache.juneau.rest.RestRequest req) {
48  			return reader(req.getQueryParams().asQueryString());
49  		}
50  		@RestPost
51  		public Reader checkFormData(org.apache.juneau.rest.RestRequest req) {
52  			return reader(req.getFormParams().asQueryString());
53  		}
54  	}
55  
56  	@Test void a01_oapiFormat() throws Exception {
57  		client().oapiFormat(HttpPartFormat.UON).build().get("/checkQuery").queryData("Foo","bar baz").run().assertContent().asString().asUrlDecode().is("Foo='bar baz'");
58  	}
59  
60  	@Test void a02_oapiCollectionFormat() throws Exception {
61  		var a = a("bar","baz");
62  		var x = client().oapiCollectionFormat(PIPES).build();
63  		x.get("/checkQuery").queryData("Foo",a).run().assertContent().asString().asUrlDecode().is("Foo=bar|baz");
64  		x.post("/checkFormData").formData("Foo",a).run().assertContent().asString().asUrlDecode().is("Foo=bar|baz");
65  		x.get("/checkHeader").header("Check","Foo").header("Foo",a).accept("text/json5").run().assertContent("['bar|baz']");
66  	}
67  
68  	@Test void a03_paramFormat() throws Exception {
69  		 var m = JsonMap.of(
70  			"foo","bar",
71  			"baz",new String[]{"qux","true","123"}
72  		);
73  		client().urlEnc().paramFormat(PLAINTEXT).build().post("/echoBody",m).run().assertContent("foo=bar&baz=qux,true,123");
74  		client().urlEnc().paramFormatPlain().build().post("/echoBody",m).run().assertContent("foo=bar&baz=qux,true,123");
75  		client().urlEnc().paramFormat(UON).build().post("/echoBody",m).run().assertContent("foo=bar&baz=@(qux,'true','123')");
76  	}
77  
78  	//------------------------------------------------------------------------------------------------------------------
79  	// Helper methods.
80  	//------------------------------------------------------------------------------------------------------------------
81  
82  	private static RestClient.Builder client() {
83  		return MockRestClient.create(A.class);
84  	}
85  }