File: HttpHeadersOf.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (422 lines) | stat: -rw-r--r-- 18,329 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @summary Tests for HttpHeaders.of factory method
 * @run testng HttpHeadersOf
 */

import java.net.http.HttpHeaders;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class HttpHeadersOf {

    static final Class<NullPointerException> NPE = NullPointerException.class;
    static final Class<NumberFormatException> NFE = NumberFormatException.class;
    static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;

    static final BiPredicate<String,String> ACCEPT_ALL =
        new BiPredicate<>() {
            @Override public boolean test(String name, String value) { return true; }
            @Override public String toString() { return "ACCEPT_ALL"; }
        };

    static final BiPredicate<String,String> REJECT_ALL =
        new BiPredicate<>() {
            @Override public boolean test(String name, String value) { return false; }
            @Override public String toString() { return "REJECT_ALL"; }
        };

    @DataProvider(name = "predicates")
    public Object[][] predicates() {
        return new Object[][] { { ACCEPT_ALL }, { REJECT_ALL } };
    }

    @Test(dataProvider = "predicates")
    public void testNull(BiPredicate<String,String> filter) {
        assertThrows(NPE, () -> HttpHeaders.of(null, null));
        assertThrows(NPE, () -> HttpHeaders.of(null, filter));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of(), null));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of("value")), null));

        // nulls in the Map
        assertThrows(NPE, () -> HttpHeaders.of(Map.of(null, List.of("value)")), filter));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", null), filter));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of(null)), filter));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of("aValue", null)), filter));
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of(null, "aValue")), filter));
    }


    @DataProvider(name = "filterMaps")
    public Object[][] filterMaps() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
                Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
                Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "filterMaps")
    public void testFilter(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, REJECT_ALL);
        assertEquals(headers.map().size(), 0);
        assertFalse(headers.firstValue("A").isPresent());
        assertEquals(headers.allValues("A").size(), 0);

        headers = HttpHeaders.of(map, (name, value) -> {
            if (name.equals("A")) return true; else return false; });
        assertEquals(headers.map().size(), 1);
        assertTrue(headers.firstValue("A").isPresent());
        assertEquals(headers.allValues("A"), map.get("A"));
        assertEquals(headers.allValues("A").size(), map.get("A").size());
        assertFalse(headers.firstValue("X").isPresent());

        headers = HttpHeaders.of(map, (name, value) -> {
            if (name.equals("X")) return true; else return false; });
        assertEquals(headers.map().size(), 1);
        assertTrue(headers.firstValue("X").isPresent());
        assertEquals(headers.allValues("X"), map.get("X"));
        assertEquals(headers.allValues("X").size(), map.get("X").size());
        assertFalse(headers.firstValue("A").isPresent());
    }


    @DataProvider(name = "mapValues")
    public Object[][] mapValues() {
        List<Map<String, List<String>>> maps = List.of(
            Map.of("A", List.of("B")),
            Map.of("A", List.of("B", "C")),
            Map.of("A", List.of("B", "C", "D")),

            Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
            Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
            Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z")),

            Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
            Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
            Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z")),

            Map.of("X", List.of("Y", "Z"), "A", List.of("B")),
            Map.of("X", List.of("Y", "Z"), "A", List.of("B", "C")),
            Map.of("X", List.of("Y", "Z"), "A", List.of("B", "C", "D"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "mapValues")
    public void testMapValues(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);

        assertEquals(headers.map().size(), map.size());
        assertTrue(headers.firstValue("A").isPresent());
        assertTrue(headers.firstValue("a").isPresent());
        assertEquals(headers.firstValue("A").get(), "B");
        assertEquals(headers.firstValue("a").get(), "B");
        assertEquals(headers.allValues("A"), map.get("A"));
        assertEquals(headers.allValues("a"), map.get("A"));
        assertEquals(headers.allValues("F").size(), 0);
        assertTrue(headers.map().get("A").contains("B"));
        assertFalse(headers.map().get("A").contains("F"));
        assertThrows(NFE, () -> headers.firstValueAsLong("A"));

        // a non-exhaustive list of mutators
        assertThrows(UOE, () -> headers.map().put("Z", List.of("Z")));
        assertThrows(UOE, () -> headers.map().remove("A"));
        assertThrows(UOE, () -> headers.map().remove("A", "B"));
        assertThrows(UOE, () -> headers.map().clear());
        assertThrows(UOE, () -> headers.allValues("A").remove("B"));
        assertThrows(UOE, () -> headers.allValues("A").remove(1));
        assertThrows(UOE, () -> headers.allValues("A").clear());
        assertThrows(UOE, () -> headers.allValues("A").add("Z"));
        assertThrows(UOE, () -> headers.allValues("A").addAll(List.of("Z")));
        assertThrows(UOE, () -> headers.allValues("A").add(1, "Z"));
    }


    @DataProvider(name = "caseInsensitivity")
    public Object[][] caseInsensitivity() {
        List<Map<String, List<String>>> maps = List.of(
             Map.of("Accept-Encoding", List.of("gzip, deflate")),
             Map.of("accept-encoding", List.of("gzip, deflate")),
             Map.of("AccePT-ENCoding", List.of("gzip, deflate")),
             Map.of("ACCept-EncodING", List.of("gzip, deflate")),
             Map.of("ACCEPT-ENCODING", List.of("gzip, deflate"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "caseInsensitivity")
    public void testCaseInsensitivity(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);

        for (String name : List.of("Accept-Encoding", "accept-encoding",
                                   "aCCept-EnCODing", "accepT-encodinG")) {
            assertTrue(headers.firstValue(name).isPresent());
            assertTrue(headers.allValues(name).contains("gzip, deflate"));
            assertEquals(headers.firstValue(name).get(), "gzip, deflate");
            assertEquals(headers.allValues(name).size(), 1);
            assertEquals(headers.map().size(), 1);
            assertEquals(headers.map().get(name).size(), 1);
            assertEquals(headers.map().get(name).get(0), "gzip, deflate");
        }
    }

    @Test
    public void testEqualsAndHashCode() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("Accept-Encoding", List.of("gzip, deflate")),
                Map.of("accept-encoding", List.of("gzip, deflate")),
                Map.of("AccePT-ENCoding", List.of("gzip, deflate")),
                Map.of("ACCept-EncodING", List.of("gzip, deflate")),
                Map.of("ACCEPT-ENCODING", List.of("gzip, deflate"))
        );
        int mapDiffer = 0;
        int mapHashDiffer = 0;
        for (Map<String, List<String>> m1 : maps) {
            HttpHeaders h1 = HttpHeaders.of(m1, ACCEPT_ALL);
            for (Map<String, List<String>> m2 : maps) {
                HttpHeaders h2 = HttpHeaders.of(m2, ACCEPT_ALL);
                if (!m1.equals(m2)) mapDiffer++;
                if (m1.hashCode() != m2.hashCode()) mapHashDiffer++;
                assertEquals(h1, h2, "HttpHeaders differ");
                assertEquals(h1.hashCode(), h2.hashCode(),
                        "hashCode differ for " + List.of(m1,m2));
            }
        }
        assertTrue(mapDiffer > 0, "all maps were equal!");
        assertTrue(mapHashDiffer > 0, "all maps had same hashCode!");
    }

    @DataProvider(name = "valueAsLong")
    public Object[][] valueAsLong() {
        return new Object[][] {
            new Object[] { Map.of("Content-Length", List.of("10")), 10l },
            new Object[] { Map.of("Content-Length", List.of("101")), 101l },
            new Object[] { Map.of("Content-Length", List.of("56789")), 56789l },
            new Object[] { Map.of("Content-Length", List.of(Long.toString(Long.MAX_VALUE))), Long.MAX_VALUE },
            new Object[] { Map.of("Content-Length", List.of(Long.toString(Long.MIN_VALUE))), Long.MIN_VALUE }
        };
    }

    @Test(dataProvider = "valueAsLong")
    public void testValueAsLong(Map<String,List<String>> map, long expected) {
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
        assertEquals(headers.firstValueAsLong("Content-Length").getAsLong(), expected);
    }


    @DataProvider(name = "duplicateNames")
    public Object[][] duplicateNames() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("X-name", List.of(),
                       "x-name", List.of()),
                Map.of("X-name", List.of(""),
                       "x-name", List.of("")),
                Map.of("X-name", List.of("C"),
                       "x-name", List.of("D")),
                Map.of("X-name", List.of("E"),
                       "Y-name", List.of("F"),
                       "X-Name", List.of("G")),
                Map.of("X-chegar", List.of("H"),
                       "y-dfuchs", List.of("I"),
                       "Y-dfuchs", List.of("J")),
                Map.of("X-name ", List.of("K"),
                       "X-Name", List.of("L")),
                Map.of("X-name", List.of("M"),
                       "\rX-Name", List.of("N"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "duplicateNames")
    public void testDuplicates(Map<String,List<String>> map) {
        HttpHeaders headers;
        try {
            headers = HttpHeaders.of(map, ACCEPT_ALL);
            fail("UNEXPECTED: " + headers);
        } catch (IllegalArgumentException iae) {
            System.out.println("caught EXPECTED IAE:" + iae);
            assertTrue(iae.getMessage().contains("duplicate"));
        }
    }


    @DataProvider(name = "noSplittingJoining")
    public Object[][] noSplittingJoining() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("A", List.of("B")),
                Map.of("A", List.of("B", "C")),
                Map.of("A", List.of("B", "C", "D")),
                Map.of("A", List.of("B", "C", "D", "E")),
                Map.of("A", List.of("B", "C", "D", "E", "F")),
                Map.of("A", List.of("B, C")),
                Map.of("A", List.of("B, C, D")),
                Map.of("A", List.of("B, C, D, E")),
                Map.of("A", List.of("B, C, D, E, F")),
                Map.of("A", List.of("B, C", "D", "E", "F")),
                Map.of("A", List.of("B", "C, D", "E", "F")),
                Map.of("A", List.of("B", "C, D", "E, F")),
                Map.of("A", List.of("B", "C, D, E", "F")),
                Map.of("A", List.of("B", "C, D, E, F"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "noSplittingJoining")
    public void testNoSplittingJoining(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
        Map<String,List<String>> headersMap = headers.map();

        assertEquals(headers.map().size(), map.size());
        for (Map.Entry<String,List<String>> entry : map.entrySet()) {
            String headerName = entry.getKey();
            List<String> headerValues = entry.getValue();
            assertEquals(headerValues, headersMap.get(headerName));
            assertEquals(headerValues, headers.allValues(headerName));
            assertEquals(headerValues.get(0), headers.firstValue(headerName).get());
        }
    }


    @DataProvider(name = "trimming")
    public Object[][] trimming() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("A", List.of("B")),
                Map.of(" A", List.of("B")),
                Map.of("A ", List.of("B")),
                Map.of("A", List.of(" B")),
                Map.of("A", List.of("B ")),
                Map.of("\tA", List.of("B")),
                Map.of("A\t", List.of("B")),
                Map.of("A", List.of("\tB")),
                Map.of("A", List.of("B\t")),
                Map.of("A\r", List.of("B")),
                Map.of("A\n", List.of("B")),
                Map.of("A\r\n", List.of("B"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "trimming")
    public void testTrimming(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
            assertEquals(name, "A");
            assertEquals(value, "B");
            return true;
        });

        assertEquals(headers.map().size(), 1);
        assertEquals(headers.firstValue("A").get(), "B");
        assertEquals(headers.allValues("A"), List.of("B"));
        assertTrue(headers.map().get("A").equals(List.of("B")));
    }


    @DataProvider(name = "emptyKey")
    public Object[][] emptyKey() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("", List.of("B")),
                Map.of(" ", List.of("B")),
                Map.of("  ", List.of("B")),
                Map.of("\t", List.of("B")),
                Map.of("\t\t", List.of("B"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "emptyKey")
    public void testEmptyKey(Map<String,List<String>> map) {
        HttpHeaders headers;
        try {
            headers = HttpHeaders.of(map, ACCEPT_ALL);
            fail("UNEXPECTED: " + headers);
        } catch (IllegalArgumentException iae) {
            System.out.println("caught EXPECTED IAE:" + iae);
            assertTrue(iae.getMessage().contains("empty"));
        }
    }


    @DataProvider(name = "emptyValue")
    public Object[][] emptyValue() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("A", List.of("")),
                Map.of("A", List.of("", "")),
                Map.of("A", List.of("", "", " ")),
                Map.of("A", List.of("\t")),
                Map.of("A", List.of("\t\t"))
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "emptyValue")
    public void testEmptyValue(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
            assertEquals(value, "");
            return true;
        });
        assertEquals(headers.map().size(), map.size());
        assertEquals(headers.map().get("A").get(0), "");
        headers.allValues("A").forEach(v -> assertEquals(v, ""));
        assertEquals(headers.firstValue("A").get(), "");
    }


    @DataProvider(name = "noValues")
    public Object[][] noValues() {
        List<Map<String, List<String>>> maps = List.of(
                Map.of("A", List.of()),
                Map.of("A", List.of(), "B", List.of()),
                Map.of("A", List.of(), "B", List.of(), "C", List.of()),
                Map.of("A", new ArrayList()),
                Map.of("A", new LinkedList())
        );
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
    }

    @Test(dataProvider = "noValues")
    public void testNoValues(Map<String,List<String>> map) {
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
            fail("UNEXPECTED call to filter");
            return true;
        });
        assertEquals(headers.map().size(), 0);
        assertEquals(headers.map().get("A"), null);
        assertEquals(headers.allValues("A").size(), 0);
        assertFalse(headers.firstValue("A").isPresent());
    }
}