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