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;
18  
19  import org.apache.juneau.*;
20  import org.apache.juneau.http.annotation.*;
21  import org.apache.juneau.plaintext.*;
22  import org.apache.juneau.rest.annotation.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.apache.juneau.utest.utils.*;
25  import org.junit.jupiter.api.*;
26  
27  class Header_AcceptCharset_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Test that Q-values are being resolved correctly.
31  	//------------------------------------------------------------------------------------------------------------------
32  
33  	@Rest(defaultCharset="utf-8",serializers=PlainTextSerializer.class)
34  	public static class A {
35  		@RestGet
36  		public String a() {
37  			return "foo";
38  		}
39  	}
40  
41  	@Test void a01_qValues() throws Exception {
42  		var a = MockRestClient.build(A.class);
43  		a.get("/a").accept("text/plain").acceptCharset("utf-8").run().assertCharset().is("utf-8");
44  		a.get("/a").accept("text/plain").acceptCharset("iso-8859-1").run().assertCharset().is("ISO-8859-1");
45  		a.get("/a").accept("text/plain").acceptCharset("bad,utf-8").run().assertCharset().is("utf-8");
46  		a.get("/a").accept("text/plain").acceptCharset("utf-8,bad").run().assertCharset().is("utf-8");
47  		a.get("/a").accept("text/plain").acceptCharset("bad;q=0.9,utf-8;q=0.1").run().assertCharset().is("utf-8");
48  		a.get("/a").accept("text/plain").acceptCharset("bad;q=0.1,utf-8;q=0.9").run().assertCharset().is("utf-8");
49  		a.get("/a").accept("text/plain").acceptCharset("utf-8;q=0.9,iso-8859-1;q=0.1").run().assertCharset().is("utf-8");
50  		a.get("/a").accept("text/plain").acceptCharset("utf-8;q=0.1,iso-8859-1;q=0.9").run().assertCharset().is("ISO-8859-1");
51  		a.get("/a").accept("text/plain").acceptCharset("*").run().assertCharset().is("utf-8");
52  		a.get("/a").accept("text/plain").acceptCharset("bad,iso-8859-1;q=0.5,*;q=0.1").run().assertCharset().is("ISO-8859-1");
53  		a.get("/a").accept("text/plain").acceptCharset("bad,iso-8859-1;q=0.1,*;q=0.5").run().assertCharset().is("utf-8");
54  	}
55  
56  	//------------------------------------------------------------------------------------------------------------------
57  	// Validate various Accept-Charset variations.
58  	//------------------------------------------------------------------------------------------------------------------
59  
60  	@Rest(defaultCharset="utf-8")
61  	public static class B {
62  
63  		@RestPut(parsers=TestParser.class, serializers=TestSerializer.class)
64  		public String a(@Content String in) {
65  			return in;
66  		}
67  
68  		public static class TestParser extends FakeReaderParser {
69  			public TestParser(FakeReaderParser.Builder builder) {
70  				super(builder.consumes("text/plain").function((session,in,type) -> session.getStreamCharset().toString()));
71  			}
72  		}
73  
74  		public static class TestSerializer extends FakeWriterSerializer {
75  			public TestSerializer(FakeWriterSerializer.Builder builder) {
76  				super(builder.produces("text/plain").function((session,o) -> (o.toString() + "/" + session.getStreamCharset())));
77  			}
78  		}
79  	}
80  
81  	@Test void b01_charsetOnResponse() throws Exception {
82  		var b = MockRestClient.buildLax(B.class);
83  		b.put("/a", null).plainText().run().assertContent("UTF-8/UTF-8");
84  		b.put("/a", null).plainText().acceptCharset("Shift_JIS").run().assertContent("UTF-8/Shift_JIS");
85  		b.put("/a?noTrace=true", null).plainText().acceptCharset("BAD").run().assertStatus(406).assertContent().isContains("No supported charsets in header 'Accept-Charset': 'BAD'");
86  		b.put("/a", null).plainText().acceptCharset("UTF-8").run().assertContent("UTF-8/UTF-8");
87  		b.put("/a", null).plainText().acceptCharset("bad,iso-8859-1").run().assertContent("UTF-8/ISO-8859-1");
88  		b.put("/a", null).plainText().acceptCharset("bad;q=0.9,iso-8859-1;q=0.1").run().assertContent("UTF-8/ISO-8859-1");
89  		b.put("/a", null).plainText().acceptCharset("bad;q=0.1,iso-8859-1;q=0.9").run().assertContent("UTF-8/ISO-8859-1");
90  	}
91  }