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.annotation;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.commons.utils.StringUtils.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.encoders.*;
24  import org.apache.juneau.http.annotation.*;
25  import org.apache.juneau.http.header.*;
26  import org.apache.juneau.rest.mock.*;
27  import org.junit.jupiter.api.*;
28  
29  class Rest_Encoders_Test extends TestBase {
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Setup classes
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	public static class MyEncoder extends GzipEncoder {
36  		@Override /* ConfigEncoder */
37  		public String[] getCodings() {
38  			return a("mycoding");
39  		}
40  	}
41  
42  	//------------------------------------------------------------------------------------------------------------------
43  	// Test with no compression enabled.
44  	//------------------------------------------------------------------------------------------------------------------
45  
46  	@Rest
47  	public static class A {
48  		@RestOp
49  		public String put(@Content String in) {
50  			return in;
51  		}
52  	}
53  
54  	@Test void a01_noCompression() throws Exception {
55  		var a = MockRestClient.buildLax(A.class);
56  		a.put("/", "foo").run().assertContent("foo");
57  		a.put("/", "foo").header(ContentEncoding.of("")).run().assertContent("foo");
58  		a.put("/", "foo").header(ContentEncoding.of("identity")).run().assertContent("foo");
59  		a.put("?noTrace=true", compress("foo")).header(ContentEncoding.of("mycoding")).run()
60  			.assertStatus(415)
61  			.assertContent().isContains(
62  				"Unsupported encoding in request header 'Content-Encoding': 'mycoding'",
63  				"Supported codings: ['identity']"
64  			);
65  	}
66  
67  	//------------------------------------------------------------------------------------------------------------------
68  	// Test with compression enabled.
69  	//------------------------------------------------------------------------------------------------------------------
70  
71  	@Rest(encoders=MyEncoder.class)
72  	public static class B {
73  		@RestOp
74  		public String put(@Content String in) {
75  			return in;
76  		}
77  	}
78  
79  	@Test void b01_withCompression() throws Exception {
80  		var b = MockRestClient.build(B.class);
81  		b.put("/", "foo")
82  			.run()
83  			.assertContent("foo");
84  		b.put("/", "foo")
85  			.header(ContentEncoding.of(""))
86  			.run()
87  			.assertContent("foo");
88  		b.put("/", "foo")
89  			.header(ContentEncoding.of("identity"))
90  			.run()
91  			.assertContent("foo");
92  	}
93  }