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.http;
18  
19  import static org.apache.juneau.MediaRanges.*;
20  import static org.apache.juneau.TestUtils.*;
21  import static org.apache.juneau.commons.utils.CollectionUtils.*;
22  import static org.apache.juneau.junit.bct.BctAssertions.*;
23  import static org.junit.jupiter.api.Assertions.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.MediaType;
27  import org.junit.jupiter.api.*;
28  
29  class MediaRanges_Test extends TestBase {
30  
31  	//-----------------------------------------------------------------------------------------------------------------
32  	// Verifies that media type parameters are distinguished from media range extensions.
33  	//-----------------------------------------------------------------------------------------------------------------
34  	@Test void a01_extensions() {
35  		var x1 = of("text/json");
36  		var x2 = x1.getRange(0);
37  		assertBean(x2, "toString,parameters,qValue,extensions", "text/json,[],1.0,[]");
38  
39  		assertNull(x1.getRange(-1));
40  		assertNull(x1.getRange(1));
41  
42  		x1 = of("foo,bar");
43  		x2 = x1.getRange(0);
44  		assertString("foo", x2);
45  		assertJson("[]", x2.getParameters());
46  		assertString("1.0", x2.getQValue());
47  		assertJson("[]", x2.getExtensions());
48  
49  		x1 = of(" foo , bar ");
50  		x2 = x1.getRange(0);
51  		assertString("foo", x2);
52  		assertJson("[]", x2.getParameters());
53  		assertString("1.0", x2.getQValue());
54  		assertJson("[]", x2.getExtensions());
55  
56  		x1 = of("text/json;a=1;q=0.9;b=2");
57  		x2 = x1.getRange(0);
58  		assertString("text/json;a=1;q=0.9;b=2", x2);
59  		assertJson("[{name:'a',value:'1'}]", x2.getParameters());
60  		assertString("0.9", x2.getQValue());
61  		assertJson("[{name:'b',value:'2'}]", x2.getExtensions());
62  
63  		x1 = of("text/json;a=1;a=2;q=0.9;b=3;b=4");
64  		x2 = x1.getRange(0);
65  		assertString("text/json;a=1;a=2;q=0.9;b=3;b=4", x2);
66  		assertJson("[{name:'a',value:'1'},{name:'a',value:'2'}]", x2.getParameters());
67  		assertString("0.9", x2.getQValue());
68  		assertJson("[{name:'b',value:'3'},{name:'b',value:'4'}]", x2.getExtensions());
69  
70  		x1 = of("text/json;a=1;a=2;q=1.0;b=3;b=4");
71  		x2 = x1.getRange(0);
72  		assertString("text/json;a=1;a=2;q=1.0;b=3;b=4", x2);
73  		assertJson("[{name:'a',value:'1'},{name:'a',value:'2'}]", x2.getParameters());
74  		assertString("1.0", x2.getQValue());
75  		assertJson("[{name:'b',value:'3'},{name:'b',value:'4'}]", x2.getExtensions());
76  
77  		x1 = of("text/json;a=1");
78  		x2 = x1.getRange(0);
79  		assertString("text/json;a=1", x2);
80  		assertJson("[{name:'a',value:'1'}]", x2.getParameters());
81  		assertString("1.0", x2.getQValue());
82  		assertJson("[]", x2.getExtensions());
83  
84  		x1 = of("text/json;a=1;");
85  		x2 = x1.getRange(0);
86  		assertString("text/json;a=1", x2);
87  		assertJson("[{name:'a',value:'1'}]", x2.getParameters());
88  		assertString("1.0", x2.getQValue());
89  		assertJson("[]", x2.getExtensions());
90  
91  		x1 = of("text/json;q=0.9");
92  		x2 = x1.getRange(0);
93  		assertString("text/json;q=0.9", x2);
94  		assertJson("[]", x2.getParameters());
95  		assertString("0.9", x2.getQValue());
96  		assertJson("[]", x2.getExtensions());
97  
98  		x1 = of("text/json;q=0.9;");
99  		x2 = x1.getRange(0);
100 		assertString("text/json;q=0.9", x2);
101 		assertJson("[]", x2.getParameters());
102 		assertString("0.9", x2.getQValue());
103 		assertJson("[]", x2.getExtensions());
104 	}
105 
106 	@Test void a02_hasSubtypePart() {
107 		var mr = of("text/json+x,text/foo+y;q=0.0");
108 		assertTrue(mr.hasSubtypePart("json"));
109 		assertTrue(mr.hasSubtypePart("x"));
110 		assertFalse(mr.hasSubtypePart("foo"));
111 		assertFalse(mr.hasSubtypePart("y"));
112 	}
113 
114 	@Test void a03_ordering() {
115 		assertString("text/json", of("text/json"));
116 		assertString("text/json,text/*", of("text/json,text/*"));
117 		assertString("text/json,text/*", of("text/*,text/json"));
118 		assertString("text/*,text/*", of("text/*,text/*"));
119 		assertString("text/*,*/text", of("*/text,text/*"));
120 		assertString("text/*,*/text", of("text/*,*/text"));
121 		assertString("a;q=0.9,b;q=0.1", of("a;q=0.9,b;q=0.1"));
122 		assertString("b;q=0.9,a;q=0.1", of("b;q=0.9,a;q=0.1"));
123 		assertString("a,b;q=0.9,c;q=0.1,d;q=0.0", of("a,b;q=0.9,c;q=0.1,d;q=0"));
124 		assertString("a,b;q=0.9,c;q=0.1,d;q=0.0", of("d;q=0,c;q=0.1,b;q=0.9,a"));
125 		assertString("a,b;q=0.9,c;q=0.1,d;q=0.0", of("a;q=1,b;q=0.9,c;q=0.1,d;q=0"));
126 		assertString("a,b;q=0.9,c;q=0.1,d;q=0.0", of("d;q=0,c;q=0.1,b;q=0.9,a;q=1"));
127 		assertString("d,c;q=0.9,b;q=0.1,a;q=0.0", of("a;q=0,b;q=0.1,c;q=0.9,d;q=1"));
128 		assertString("*", of("*"));
129 		assertString("", of(""));
130 		assertString("", of(null));
131 		assertString("foo/bar/baz", of("foo/bar/baz"));
132 	}
133 
134 	@Test void a04_match() {
135 		var x1 = of("text/json");
136 		assertEquals(0, x1.match(l(MediaType.of("text/json"))));
137 		assertEquals(-1, x1.match(l(MediaType.of("text/foo"))));
138 		assertEquals(-1, x1.match(l((MediaType)null)));
139 		assertEquals(-1, x1.match(null));
140 
141 		var x2 = of("");
142 		assertEquals(-1, x2.match(l(MediaType.of("text/json"))));
143 	}
144 
145 	@Test void a05_getRanges() {
146 		var x1 = of("text/json");
147 		assertJson("['text/json']", x1.toList());
148 	}
149 }