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