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