test/jdk/java/net/httpclient/HttpHeadersOf.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 50681 4254bed3c09d
child 56795 03ece2518428
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     4
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     8
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    13
 * accompanied this code).
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    14
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    18
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    21
 * questions.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    22
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    23
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    24
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    25
 * @test
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    26
 * @summary Tests for HttpHeaders.of factory method
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    27
 * @run testng HttpHeadersOf
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    28
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    29
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    30
import java.net.http.HttpHeaders;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    31
import java.util.ArrayList;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    32
import java.util.LinkedList;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    33
import java.util.List;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    34
import java.util.Map;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    35
import java.util.function.BiPredicate;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    36
import org.testng.annotations.DataProvider;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    37
import org.testng.annotations.Test;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    38
import static org.testng.Assert.assertFalse;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    39
import static org.testng.Assert.assertEquals;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    40
import static org.testng.Assert.assertThrows;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    41
import static org.testng.Assert.assertTrue;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    42
import static org.testng.Assert.fail;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    43
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    44
public class HttpHeadersOf {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    45
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    46
    static final Class<NullPointerException> NPE = NullPointerException.class;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    47
    static final Class<NumberFormatException> NFE = NumberFormatException.class;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    48
    static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    49
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    50
    static final BiPredicate<String,String> ACCEPT_ALL =
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    51
        new BiPredicate<>() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    52
            @Override public boolean test(String name, String value) { return true; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    53
            @Override public String toString() { return "ACCEPT_ALL"; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    54
        };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    55
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    56
    static final BiPredicate<String,String> REJECT_ALL =
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    57
        new BiPredicate<>() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    58
            @Override public boolean test(String name, String value) { return false; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    59
            @Override public String toString() { return "REJECT_ALL"; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    60
        };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    61
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    62
    @DataProvider(name = "predicates")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    63
    public Object[][] predicates() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    64
        return new Object[][] { { ACCEPT_ALL }, { REJECT_ALL } };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    65
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    66
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    67
    @Test(dataProvider = "predicates")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    68
    public void testNull(BiPredicate<String,String> filter) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    69
        assertThrows(NPE, () -> HttpHeaders.of(null, null));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    70
        assertThrows(NPE, () -> HttpHeaders.of(null, filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    71
        assertThrows(NPE, () -> HttpHeaders.of(Map.of(), null));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    72
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of("value")), null));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    73
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    74
        // nulls in the Map
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    75
        assertThrows(NPE, () -> HttpHeaders.of(Map.of(null, List.of("value)")), filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    76
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", null), filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    77
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of(null)), filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    78
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of("aValue", null)), filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    79
        assertThrows(NPE, () -> HttpHeaders.of(Map.of("name", List.of(null, "aValue")), filter));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    80
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    81
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    82
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    83
    @DataProvider(name = "filterMaps")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    84
    public Object[][] filterMaps() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    85
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    86
                Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    87
                Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    88
                Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    89
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    90
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    91
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    92
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    93
    @Test(dataProvider = "filterMaps")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    94
    public void testFilter(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    95
        HttpHeaders headers = HttpHeaders.of(map, REJECT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    96
        assertEquals(headers.map().size(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    97
        assertFalse(headers.firstValue("A").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    98
        assertEquals(headers.allValues("A").size(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    99
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   100
        headers = HttpHeaders.of(map, (name, value) -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   101
            if (name.equals("A")) return true; else return false; });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   102
        assertEquals(headers.map().size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   103
        assertTrue(headers.firstValue("A").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   104
        assertEquals(headers.allValues("A"), map.get("A"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   105
        assertEquals(headers.allValues("A").size(), map.get("A").size());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   106
        assertFalse(headers.firstValue("X").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   107
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   108
        headers = HttpHeaders.of(map, (name, value) -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   109
            if (name.equals("X")) return true; else return false; });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   110
        assertEquals(headers.map().size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   111
        assertTrue(headers.firstValue("X").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   112
        assertEquals(headers.allValues("X"), map.get("X"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   113
        assertEquals(headers.allValues("X").size(), map.get("X").size());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   114
        assertFalse(headers.firstValue("A").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   115
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   116
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   117
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   118
    @DataProvider(name = "mapValues")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   119
    public Object[][] mapValues() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   120
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   121
            Map.of("A", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   122
            Map.of("A", List.of("B", "C")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   123
            Map.of("A", List.of("B", "C", "D")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   124
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   125
            Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   126
            Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   127
            Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   128
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   129
            Map.of("A", List.of("B"),           "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   130
            Map.of("A", List.of("B", "C"),      "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   131
            Map.of("A", List.of("B", "C", "D"), "X", List.of("Y", "Z")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   132
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   133
            Map.of("X", List.of("Y", "Z"), "A", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   134
            Map.of("X", List.of("Y", "Z"), "A", List.of("B", "C")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   135
            Map.of("X", List.of("Y", "Z"), "A", List.of("B", "C", "D"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   136
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   137
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   138
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   139
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   140
    @Test(dataProvider = "mapValues")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   141
    public void testMapValues(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   142
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   143
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   144
        assertEquals(headers.map().size(), map.size());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   145
        assertTrue(headers.firstValue("A").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   146
        assertTrue(headers.firstValue("a").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   147
        assertEquals(headers.firstValue("A").get(), "B");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   148
        assertEquals(headers.firstValue("a").get(), "B");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   149
        assertEquals(headers.allValues("A"), map.get("A"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   150
        assertEquals(headers.allValues("a"), map.get("A"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   151
        assertEquals(headers.allValues("F").size(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   152
        assertTrue(headers.map().get("A").contains("B"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   153
        assertFalse(headers.map().get("A").contains("F"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   154
        assertThrows(NFE, () -> headers.firstValueAsLong("A"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   155
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   156
        // a non-exhaustive list of mutators
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   157
        assertThrows(UOE, () -> headers.map().put("Z", List.of("Z")));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   158
        assertThrows(UOE, () -> headers.map().remove("A"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   159
        assertThrows(UOE, () -> headers.map().remove("A", "B"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   160
        assertThrows(UOE, () -> headers.map().clear());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   161
        assertThrows(UOE, () -> headers.allValues("A").remove("B"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   162
        assertThrows(UOE, () -> headers.allValues("A").remove(1));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   163
        assertThrows(UOE, () -> headers.allValues("A").clear());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   164
        assertThrows(UOE, () -> headers.allValues("A").add("Z"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   165
        assertThrows(UOE, () -> headers.allValues("A").addAll(List.of("Z")));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   166
        assertThrows(UOE, () -> headers.allValues("A").add(1, "Z"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   167
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   168
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   169
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   170
    @DataProvider(name = "caseInsensitivity")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   171
    public Object[][] caseInsensitivity() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   172
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   173
             Map.of("Accept-Encoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   174
             Map.of("accept-encoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   175
             Map.of("AccePT-ENCoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   176
             Map.of("ACCept-EncodING", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   177
             Map.of("ACCEPT-ENCODING", List.of("gzip, deflate"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   178
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   179
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   180
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   181
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   182
    @Test(dataProvider = "caseInsensitivity")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   183
    public void testCaseInsensitivity(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   184
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   185
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   186
        for (String name : List.of("Accept-Encoding", "accept-encoding",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   187
                                   "aCCept-EnCODing", "accepT-encodinG")) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   188
            assertTrue(headers.firstValue(name).isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   189
            assertTrue(headers.allValues(name).contains("gzip, deflate"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   190
            assertEquals(headers.firstValue(name).get(), "gzip, deflate");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   191
            assertEquals(headers.allValues(name).size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   192
            assertEquals(headers.map().size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   193
            assertEquals(headers.map().get(name).size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   194
            assertEquals(headers.map().get(name).get(0), "gzip, deflate");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   195
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   196
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   197
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   198
    @Test
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   199
    public void testEqualsAndHashCode() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   200
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   201
                Map.of("Accept-Encoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   202
                Map.of("accept-encoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   203
                Map.of("AccePT-ENCoding", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   204
                Map.of("ACCept-EncodING", List.of("gzip, deflate")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   205
                Map.of("ACCEPT-ENCODING", List.of("gzip, deflate"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   206
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   207
        int mapDiffer = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   208
        int mapHashDiffer = 0;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   209
        for (Map<String, List<String>> m1 : maps) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   210
            HttpHeaders h1 = HttpHeaders.of(m1, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   211
            for (Map<String, List<String>> m2 : maps) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   212
                HttpHeaders h2 = HttpHeaders.of(m2, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   213
                if (!m1.equals(m2)) mapDiffer++;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   214
                if (m1.hashCode() != m2.hashCode()) mapHashDiffer++;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   215
                assertEquals(h1, h2, "HttpHeaders differ");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   216
                assertEquals(h1.hashCode(), h2.hashCode(),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   217
                        "hashCode differ for " + List.of(m1,m2));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   218
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   219
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   220
        assertTrue(mapDiffer > 0, "all maps were equal!");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   221
        assertTrue(mapHashDiffer > 0, "all maps had same hashCode!");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   222
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   223
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   224
    @DataProvider(name = "valueAsLong")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   225
    public Object[][] valueAsLong() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   226
        return new Object[][] {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   227
            new Object[] { Map.of("Content-Length", List.of("10")), 10l },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   228
            new Object[] { Map.of("Content-Length", List.of("101")), 101l },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   229
            new Object[] { Map.of("Content-Length", List.of("56789")), 56789l },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   230
            new Object[] { Map.of("Content-Length", List.of(Long.toString(Long.MAX_VALUE))), Long.MAX_VALUE },
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   231
            new Object[] { Map.of("Content-Length", List.of(Long.toString(Long.MIN_VALUE))), Long.MIN_VALUE }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   232
        };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   233
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   234
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   235
    @Test(dataProvider = "valueAsLong")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   236
    public void testValueAsLong(Map<String,List<String>> map, long expected) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   237
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   238
        assertEquals(headers.firstValueAsLong("Content-Length").getAsLong(), expected);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   239
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   240
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   241
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   242
    @DataProvider(name = "duplicateNames")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   243
    public Object[][] duplicateNames() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   244
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   245
                Map.of("X-name", List.of(),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   246
                       "x-name", List.of()),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   247
                Map.of("X-name", List.of(""),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   248
                       "x-name", List.of("")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   249
                Map.of("X-name", List.of("C"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   250
                       "x-name", List.of("D")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   251
                Map.of("X-name", List.of("E"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   252
                       "Y-name", List.of("F"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   253
                       "X-Name", List.of("G")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   254
                Map.of("X-chegar", List.of("H"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   255
                       "y-dfuchs", List.of("I"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   256
                       "Y-dfuchs", List.of("J")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   257
                Map.of("X-name ", List.of("K"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   258
                       "X-Name", List.of("L")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   259
                Map.of("X-name", List.of("M"),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   260
                       "\rX-Name", List.of("N"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   261
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   262
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   263
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   264
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   265
    @Test(dataProvider = "duplicateNames")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   266
    public void testDuplicates(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   267
        HttpHeaders headers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   268
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   269
            headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   270
            fail("UNEXPECTED: " + headers);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   271
        } catch (IllegalArgumentException iae) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   272
            System.out.println("caught EXPECTED IAE:" + iae);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   273
            assertTrue(iae.getMessage().contains("duplicate"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   274
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   275
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   276
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   277
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   278
    @DataProvider(name = "noSplittingJoining")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   279
    public Object[][] noSplittingJoining() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   280
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   281
                Map.of("A", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   282
                Map.of("A", List.of("B", "C")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   283
                Map.of("A", List.of("B", "C", "D")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   284
                Map.of("A", List.of("B", "C", "D", "E")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   285
                Map.of("A", List.of("B", "C", "D", "E", "F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   286
                Map.of("A", List.of("B, C")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   287
                Map.of("A", List.of("B, C, D")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   288
                Map.of("A", List.of("B, C, D, E")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   289
                Map.of("A", List.of("B, C, D, E, F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   290
                Map.of("A", List.of("B, C", "D", "E", "F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   291
                Map.of("A", List.of("B", "C, D", "E", "F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   292
                Map.of("A", List.of("B", "C, D", "E, F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   293
                Map.of("A", List.of("B", "C, D, E", "F")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   294
                Map.of("A", List.of("B", "C, D, E, F"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   295
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   296
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   297
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   298
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   299
    @Test(dataProvider = "noSplittingJoining")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   300
    public void testNoSplittingJoining(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   301
        HttpHeaders headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   302
        Map<String,List<String>> headersMap = headers.map();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   303
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   304
        assertEquals(headers.map().size(), map.size());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   305
        for (Map.Entry<String,List<String>> entry : map.entrySet()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   306
            String headerName = entry.getKey();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   307
            List<String> headerValues = entry.getValue();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   308
            assertEquals(headerValues, headersMap.get(headerName));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   309
            assertEquals(headerValues, headers.allValues(headerName));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   310
            assertEquals(headerValues.get(0), headers.firstValue(headerName).get());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   311
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   312
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   313
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   314
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   315
    @DataProvider(name = "trimming")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   316
    public Object[][] trimming() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   317
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   318
                Map.of("A", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   319
                Map.of(" A", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   320
                Map.of("A ", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   321
                Map.of("A", List.of(" B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   322
                Map.of("A", List.of("B ")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   323
                Map.of("\tA", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   324
                Map.of("A\t", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   325
                Map.of("A", List.of("\tB")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   326
                Map.of("A", List.of("B\t")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   327
                Map.of("A\r", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   328
                Map.of("A\n", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   329
                Map.of("A\r\n", List.of("B"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   330
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   331
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   332
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   333
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   334
    @Test(dataProvider = "trimming")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   335
    public void testTrimming(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   336
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   337
            assertEquals(name, "A");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   338
            assertEquals(value, "B");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   339
            return true;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   340
        });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   341
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   342
        assertEquals(headers.map().size(), 1);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   343
        assertEquals(headers.firstValue("A").get(), "B");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   344
        assertEquals(headers.allValues("A"), List.of("B"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   345
        assertTrue(headers.map().get("A").equals(List.of("B")));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   346
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   347
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   348
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   349
    @DataProvider(name = "emptyKey")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   350
    public Object[][] emptyKey() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   351
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   352
                Map.of("", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   353
                Map.of(" ", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   354
                Map.of("  ", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   355
                Map.of("\t", List.of("B")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   356
                Map.of("\t\t", List.of("B"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   357
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   358
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   359
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   360
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   361
    @Test(dataProvider = "emptyKey")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   362
    public void testEmptyKey(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   363
        HttpHeaders headers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   364
        try {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   365
            headers = HttpHeaders.of(map, ACCEPT_ALL);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   366
            fail("UNEXPECTED: " + headers);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   367
        } catch (IllegalArgumentException iae) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   368
            System.out.println("caught EXPECTED IAE:" + iae);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   369
            assertTrue(iae.getMessage().contains("empty"));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   370
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   371
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   372
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   373
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   374
    @DataProvider(name = "emptyValue")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   375
    public Object[][] emptyValue() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   376
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   377
                Map.of("A", List.of("")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   378
                Map.of("A", List.of("", "")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   379
                Map.of("A", List.of("", "", " ")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   380
                Map.of("A", List.of("\t")),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   381
                Map.of("A", List.of("\t\t"))
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   382
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   383
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   384
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   385
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   386
    @Test(dataProvider = "emptyValue")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   387
    public void testEmptyValue(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   388
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   389
            assertEquals(value, "");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   390
            return true;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   391
        });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   392
        assertEquals(headers.map().size(), map.size());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   393
        assertEquals(headers.map().get("A").get(0), "");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   394
        headers.allValues("A").forEach(v -> assertEquals(v, ""));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   395
        assertEquals(headers.firstValue("A").get(), "");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   396
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   397
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   398
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   399
    @DataProvider(name = "noValues")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   400
    public Object[][] noValues() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   401
        List<Map<String, List<String>>> maps = List.of(
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   402
                Map.of("A", List.of()),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   403
                Map.of("A", List.of(), "B", List.of()),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   404
                Map.of("A", List.of(), "B", List.of(), "C", List.of()),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   405
                Map.of("A", new ArrayList()),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   406
                Map.of("A", new LinkedList())
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   407
        );
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   408
        return maps.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   409
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   410
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   411
    @Test(dataProvider = "noValues")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   412
    public void testNoValues(Map<String,List<String>> map) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   413
        HttpHeaders headers = HttpHeaders.of(map, (name, value) -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   414
            fail("UNEXPECTED call to filter");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   415
            return true;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   416
        });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   417
        assertEquals(headers.map().size(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   418
        assertEquals(headers.map().get("A"), null);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   419
        assertEquals(headers.allValues("A").size(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   420
        assertFalse(headers.firstValue("A").isPresent());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   421
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   422
}