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  
22  import java.util.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.collections.*;
26  import org.apache.juneau.rest.RestRequest;
27  import org.apache.juneau.rest.annotation.*;
28  import org.apache.juneau.rest.mock.*;
29  import org.junit.jupiter.api.*;
30  
31  class RestClient_CallbackStrings_Test extends TestBase {
32  
33  	//-----------------------------------------------------------------------------------------------------------------
34  	// Basic tests
35  	//-----------------------------------------------------------------------------------------------------------------
36  
37  	@Rest
38  	public static class A {
39  		@RestGet(path="/*")
40  		public JsonMap get(RestRequest req) throws Exception {
41  			return JsonMap.of("method","GET","headers",getFooHeaders(req),"content",req.getContent().asString());
42  		}
43  		@RestPut(path="/*")
44  		public JsonMap put(RestRequest req) throws Exception {
45  			return JsonMap.of("method","PUT","headers",getFooHeaders(req),"content",req.getContent().asString());
46  		}
47  		private static Map<String,Object> getFooHeaders(RestRequest req) {
48  			var m = new TreeMap<String,Object>();
49  			req.getHeaders().stream().filter(x -> x.getName().startsWith("Foo-")).forEach(x -> m.put(x.getName(), x.getValue()));
50  			return m;
51  		}
52  	}
53  
54  	@Test void a01_callback() throws Exception {
55  		var x = MockRestClient.build(A.class);
56  		x.callback("GET /testCallback").run().assertContent("{method:'GET',headers:{},content:''}");
57  		x.callback("GET /testCallback some sample content").run().assertContent("{method:'GET',headers:{},content:'some sample content'}");
58  		x.callback("GET {Foo-X:123,Foo-Y:'abc'} /testCallback").run().assertContent("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:''}");
59  		x.callback("GET  { Foo-X : 123,Foo-Y : 'abc' } /testCallback").run().assertContent("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:''}");
60  		x.callback("GET {Foo-X:123,Foo-Y:'abc'} /testCallback   some sample content  ").run().assertContent("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:'some sample content'}");
61  		x.callback("PUT {Foo-X:123,Foo-Y:'abc'} /testCallback   some sample content  ").run().assertContent("{method:'PUT',headers:{'Foo-X':'123','Foo-Y':'abc'},content:'some sample content'}");
62  	}
63  
64  	@Test void a02_callback_invalidStrings() {
65  		var x = MockRestClient.build(A.class);
66  		for (var s : l("","GET","GET ","GET {","GET {xxx} /foo",null)) {
67  			assertThrowsWithMessage(Exception.class, "Invalid format for call string", ()->x.callback(s).run().getContent().asString());
68  		}
69  	}
70  }