1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.http;
18
19 import static org.apache.juneau.common.utils.Utils.*;
20 import static org.apache.juneau.http.HttpHeaders.*;
21 import static org.junit.jupiter.api.Assertions.*;
22
23 import java.util.*;
24
25 import org.apache.juneau.*;
26 import org.apache.juneau.json.*;
27 import org.junit.jupiter.params.*;
28 import org.junit.jupiter.params.provider.*;
29
30
31
32
33 class MediaRanges_FindMatch_Test extends TestBase {
34
35 private static final Input[] INPUT = {
36
37
38
39
40 input("SimpleMatch-1", "text/json", "['text/json']", 0, 0),
41 input("SimpleMatch-2", "text/json", "['text/json','text/foo']", 0, 0),
42 input("SimpleMatch-3", "text/json", "['text/foo','text/json']", 1, 1),
43
44
45 input("SimpleNoMatch-1", "text/jsonx", "['text/json']", -1, -1),
46 input("SimpleNoMatch-2", "text/jso", "['text/json']", -1, -1),
47 input("SimpleNoMatch-3", "text/json", "['application/json']", -1, -1),
48 input("SimpleNoMatch-4", "text/json", "[]", -1, -1),
49
50
51 input("MetaMatch-1", "text/*", "['text/a','text/b+c','text/b+d+e']", 0, 2),
52 input("MetaMatch-2", "text/b+*", "['text/a','text/b+c','text/b+d+e']", 1, 2),
53 input("MetaMatch-3", "text/c+*", "['text/a','text/b+c','text/b+d+e']", 1, 1),
54 input("MetaMatch-4", "text/b+d+e", "['text/a','text/b+c','text/b+d']", -1, -1),
55 input("MetaMatch-5", "text/b+*", "['text/a','text/b+c','text/b+d+e']", 1, 2),
56 input("MetaMatch-6", "text/d+e+*", "['text/a','text/b+c','text/b+d+e']", 2, 2),
57
58 input("MetaMatch-7", "*/a", "['text/a','application/b']", 0, 0),
59 input("MetaMatch-8", "*/*", "['text/a','text/b+c']", 0, 1),
60 input("MetaMatch-9", "*/*", "['text/b+c','text/a']", 0, 1),
61
62
63 input("RevMetaMatch-1", "text/a", "['text/*']", 0, 0),
64 input("RevMetaMatch-2", "text/a", "['*/a']", 0, 0),
65 input("RevMetaMatch-3", "text/a", "['*/*']", 0, 0),
66
67
68 input("MixedMetaMatch-1", "text/*", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 0, 0),
69 input("MixedMetaMatch-2", "*/a", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 1, 1),
70 input("MixedMetaMatch-3", "*/*", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 0, 0),
71 input("MixedMetaMatch-4", "text/a+*", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 1, 2),
72 input("MixedMetaMatch-5", "text/c+*", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 3, 3),
73 input("MixedMetaMatch-6", "text/d+*", "['text/*','text/a','text/a+b','text/b+c','text/d+*']", 4, 4),
74
75
76 input("Fuzzy-1", "text/1+2", "['text/1+2']", 0, 0),
77
78 input("Fuzzy-2", "text/2+1", "['text/1+2']", 0, 0),
79
80
81
82 input("Fuzzy-3", "text/json+foo", "['text/json+*']", 0, 0),
83
84
85
86 input("Fuzzy-4", "text/json", "['text/json+lax']", -1, -1),
87
88 input("Fuzzy-5", "text/1+2", "['text/1','text/1+3']", -1, -1),
89
90
91 input("Fuzzy-6", "text/1+2", "['text/1','text/1+2','text/1+2+3']", 1, 1),
92
93 input("Fuzzy-7", "text/1+2", "['text/1+2+3','text/1','text/1+2']", 2, 2),
94
95 input("Fuzzy-8", "text/1+2", "['text/3+2+1','text/1','text/2+1']", 2, 2),
96 input("Fuzzy-9", "text/1+2+3+4", "['text/1+2','text/1+2+3']", -1, -1),
97 input("Fuzzy-10", "text/1+2+3+4", "['text/1+2+3','text/1+2']", -1, -1),
98 input("Fuzzy-11", "text/4+2+3+1", "['text/1+2+3','text/1+2']", -1, -1),
99 input("Fuzzy-12", "text/4+2+3+1", "['text/1+2','text/1+2+3']", -1, -1),
100
101
102 input("Q-1", "text/A;q=0.9,text/B;q=0.1", "['text/A','text/B']", 0, 0),
103 input("Q-2", "text/A;q=0.9,text/B;q=0.1", "['text/B','text/A']", 1, 1),
104 input("Q-3", "text/A+1;q=0.9,text/B;q=0.1", "['text/A','text/B']", 1, 1),
105 input("Q-4", "text/A;q=0.9,text/B+1;q=0.1", "['text/A','text/B+1']", 0, 0),
106 input("Q-5", "text/A;q=0.9,text/A+1;q=0.1", "['text/A+1','text/A']", 1, 1),
107
108
109 input("Q0-1", "text/A;q=0,text/B;q=0.1", "['text/A','text/B']", 1, 1),
110 input("Q0-2", "text/A;q=0,text/B;q=0.1", "['text/A','text/A+1']", -1, -1),
111
112
113 input("Parms-1", "text/A", "['text/A;foo=bar','text/B']", 0, 0),
114 input("Parms-2", "text/A;foo=bar", "['text/A','text/B']", 0, 0),
115
116
117 input("Json-1a", "text/json", "['text/json','text/json+*','text/*','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
118 input("Json-1b", "text/json", "['text/json+*','text/*','text/json+lax','text/json+lax+*','text/foo','text/json']", 5, 5),
119 input("Json-1c", "text/json", "['text/json+*','text/*','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
120 input("Json-1d", "text/json", "['text/*','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
121 input("Json-1e", "text/json", "['text/json+lax','text/json+lax+*','text/foo']", -1, -1),
122
123 input("Json-2a", "text/json+lax", "['text/json+lax','text/json+lax+*','text/json+*','text/lax+*','text/*','text/json','text/lax']", 0, 0),
124 input("Json-2b", "text/json+lax", "['text/json+lax+*','text/json+*','text/lax+*','text/*','text/json','text/lax']", 0, 0),
125 input("Json-2c", "text/json+lax", "['text/json+*','text/lax+foo+*','text/*','text/json','text/lax']", 0, 0),
126 input("Json-2d", "text/json+lax", "['text/lax+*','text/*','text/json','text/lax']", 0, 0),
127 input("Json-2e", "text/json+lax", "['text/*','text/json','text/lax']", 0, 0),
128 input("Json-2f", "text/json+lax", "['text/json','text/lax']", -1, -1),
129
130 input("Json-3a", "text/json+activity", "['text/json+activity','text/activity+json','text/json+activity+*','text/json+*','text/*','text/json','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
131 input("Json-3b", "text/json+activity", "['text/activity+json','text/json+activity+*','text/json+*','text/*','text/json','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
132 input("Json-3c", "text/json+activity", "['text/json+activity+*','text/json+*','text/*','text/json','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
133 input("Json-3d", "text/json+activity", "['text/json+*','text/*','text/json','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
134 input("Json-3e", "text/json+activity", "['text/*','text/json','text/json+lax','text/json+lax+*','text/foo']", 0, 0),
135 input("Json-3f", "text/json+activity", "['text/json','text/json+lax','text/json+lax+*','text/foo']", -1, -1),
136
137
138 input("Xml-1a", "text/xml", "['text/xml','text/xml+*','text/xml+rdf','text/foo']", 0, 0),
139 input("Xml-1b", "text/xml", "['text/xml+*','text/xml+rdf','text/foo']", 0, 0),
140 input("Xml-1c", "text/xml", "['text/xml+rdf','text/foo']", -1, -1),
141 input("Xml-1d", "text/xml", "['text/foo']", -1, -1),
142
143 input("Xml-2a", "text/xml+id", "['text/xml+*','text/xml','text/xml+rdf']", 0, 0),
144 input("Xml-2b", "text/xml+id", "['text/xml','text/xml+rdf']", -1, -1),
145 input("Xml-2c", "text/xml+id", "['text/xml+rdf']", -1, -1),
146
147
148 input("Rdf-1a", "text/xml+rdf", "['text/xml+rdf','text/xml+*','text/xml']", 0, 0),
149 input("Rdf-1b", "text/xml+rdf", "['text/xml+*','text/xml']", 0, 0),
150 input("Rdf-1c", "text/xml+rdf", "['text/xml']", -1, -1)
151 };
152
153 private static Input input(String label, String accept, String mediaTypes, int expected, int expectedReverse) {
154 return new Input(label, accept, mediaTypes, expected, expectedReverse);
155 }
156
157 private static class Input {
158 private String label, accept, mediaTypes;
159 private int expected, expectedReverse;
160
161 public Input(String label, String accept, String mediaTypes, int expected, int expectedReverse) {
162 this.label = label;
163 this.accept = accept;
164 this.mediaTypes = mediaTypes;
165 this.expected = expected;
166 this.expectedReverse = expectedReverse;
167 }
168 }
169
170 static Input[] input() {
171 return INPUT;
172 }
173
174 @ParameterizedTest
175 @MethodSource("input")
176 void a01_basic(Input input) throws Exception {
177 var a = accept(input.accept);
178 var mt = JsonParser.DEFAULT.parse(input.mediaTypes, MediaType[].class);
179 var r = a.match(alist(mt));
180 assertEquals(input.expected, r, fms("{0} failed", input.label));
181 }
182
183 @ParameterizedTest
184 @MethodSource("input")
185 void a02_reversed(Input input) throws Exception {
186 var a = accept(input.accept);
187 var mt = JsonParser.DEFAULT.parse(input.mediaTypes, MediaType[].class);
188 Collections.reverse(Arrays.asList(mt));
189 var r = a.match(alist(mt));
190 var expected2 = input.expectedReverse == -1 ? -1 : mt.length-input.expectedReverse-1;
191 assertEquals(expected2, r);
192 }
193 }