|
11 | 11 |
|
12 | 12 | package io.vertx.tests.http.headers; |
13 | 13 |
|
| 14 | +import io.netty.handler.codec.Headers; |
14 | 15 | import io.netty.handler.codec.http2.DefaultHttp2Headers; |
15 | | -import io.vertx.core.MultiMap; |
16 | 16 | import io.vertx.core.http.HttpMethod; |
17 | 17 | import io.vertx.core.http.impl.headers.HttpHeaders; |
18 | 18 | import io.vertx.core.http.impl.headers.HttpRequestHeaders; |
19 | | -import org.junit.Before; |
20 | 19 | import org.junit.Ignore; |
21 | 20 | import org.junit.Test; |
22 | 21 |
|
23 | | -import java.util.Arrays; |
24 | | -import java.util.Collections; |
25 | | -import java.util.HashSet; |
26 | | -import java.util.List; |
27 | | -import java.util.Map; |
| 22 | +import java.util.*; |
28 | 23 | import java.util.stream.Collectors; |
29 | 24 |
|
30 | 25 | import static org.junit.Assert.*; |
|
34 | 29 | */ |
35 | 30 | public class Http2HeadersAdaptorsTest extends HeadersTest { |
36 | 31 |
|
37 | | - DefaultHttp2Headers headers; |
38 | | - MultiMap map; |
39 | | - |
40 | | - @Before |
41 | | - public void setUp() { |
42 | | - headers = new DefaultHttp2Headers(); |
43 | | - map = new HttpHeaders(headers); |
44 | | - } |
45 | | - |
46 | 32 | @Override |
47 | 33 | protected HttpHeaders newMultiMap() { |
48 | 34 | return new HttpHeaders(new DefaultHttp2Headers()); |
49 | 35 | } |
50 | 36 |
|
51 | 37 | @Test |
52 | 38 | public void testGetConvertUpperCase() { |
53 | | - map.set("foo", "foo_value"); |
54 | | - assertEquals("foo_value", map.get("Foo")); |
55 | | - assertEquals("foo_value", map.get((CharSequence) "Foo")); |
| 39 | + HttpHeaders headers = newMultiMap(); |
| 40 | + headers.set("foo", "foo_value"); |
| 41 | + assertEquals("foo_value", headers.get("Foo")); |
| 42 | + assertEquals("foo_value", headers.get((CharSequence) "Foo")); |
56 | 43 | } |
57 | 44 |
|
58 | 45 | @Test |
59 | 46 | public void testGetAllConvertUpperCase() { |
60 | | - map.set("foo", "foo_value"); |
61 | | - assertEquals(Collections.singletonList("foo_value"), map.getAll("Foo")); |
62 | | - assertEquals(Collections.singletonList("foo_value"), map.getAll((CharSequence) "Foo")); |
| 47 | + HttpHeaders headers = newMultiMap(); |
| 48 | + headers.set("foo", "foo_value"); |
| 49 | + assertEquals(Collections.singletonList("foo_value"), headers.getAll("Foo")); |
| 50 | + assertEquals(Collections.singletonList("foo_value"), headers.getAll((CharSequence) "Foo")); |
63 | 51 | } |
64 | 52 |
|
65 | 53 | @Test |
66 | 54 | public void testContainsConvertUpperCase() { |
67 | | - map.set("foo", "foo_value"); |
68 | | - assertTrue(map.contains("Foo")); |
69 | | - assertTrue(map.contains((CharSequence) "Foo")); |
| 55 | + HttpHeaders headers = newMultiMap(); |
| 56 | + headers.set("foo", "foo_value"); |
| 57 | + assertTrue(headers.contains("Foo")); |
| 58 | + assertTrue(headers.contains((CharSequence) "Foo")); |
70 | 59 | } |
71 | 60 |
|
72 | 61 | @Test |
73 | 62 | public void testSetConvertUpperCase() { |
74 | | - map.set("Foo", "foo_value"); |
75 | | - map.set((CharSequence) "Bar", "bar_value"); |
76 | | - map.set("Juu", (Iterable<String>)Collections.singletonList("juu_value")); |
77 | | - map.set("Daa", Collections.singletonList((CharSequence)"daa_value")); |
78 | | - assertHeaderNames("foo","bar", "juu", "daa"); |
| 63 | + HttpHeaders headers = newMultiMap(); |
| 64 | + headers.set("Foo", "foo_value"); |
| 65 | + headers.set((CharSequence) "Bar", "bar_value"); |
| 66 | + headers.set("Juu", (Iterable<String>)Collections.singletonList("juu_value")); |
| 67 | + headers.set("Daa", Collections.singletonList((CharSequence)"daa_value")); |
| 68 | + assertHeaderNames(headers, "foo","bar", "juu", "daa"); |
79 | 69 | } |
80 | 70 |
|
81 | 71 | @Test |
82 | 72 | public void testAddConvertUpperCase() { |
83 | | - map.add("Foo", "foo_value"); |
84 | | - map.add((CharSequence) "Bar", "bar_value"); |
85 | | - map.add("Juu", (Iterable<String>)Collections.singletonList("juu_value")); |
86 | | - map.add("Daa", Collections.singletonList((CharSequence)"daa_value")); |
87 | | - assertHeaderNames("foo","bar", "juu", "daa"); |
| 73 | + HttpHeaders headers = newMultiMap(); |
| 74 | + headers.add("Foo", "foo_value"); |
| 75 | + headers.add((CharSequence) "Bar", "bar_value"); |
| 76 | + headers.add("Juu", (Iterable<String>)Collections.singletonList("juu_value")); |
| 77 | + headers.add("Daa", Collections.singletonList((CharSequence)"daa_value")); |
| 78 | + assertHeaderNames(headers, "foo","bar", "juu", "daa"); |
88 | 79 | } |
89 | 80 |
|
90 | 81 | @Test |
91 | 82 | public void testRemoveConvertUpperCase() { |
92 | | - map.set("foo", "foo_value"); |
93 | | - map.remove("Foo"); |
94 | | - map.set("bar", "bar_value"); |
95 | | - map.remove((CharSequence) "Bar"); |
96 | | - assertHeaderNames(); |
| 83 | + HttpHeaders headers = newMultiMap(); |
| 84 | + headers.set("foo", "foo_value"); |
| 85 | + headers.remove("Foo"); |
| 86 | + headers.set("bar", "bar_value"); |
| 87 | + headers.remove((CharSequence) "Bar"); |
| 88 | + assertHeaderNames(headers); |
97 | 89 | } |
98 | 90 |
|
99 | 91 | @Ignore |
100 | 92 | @Test |
101 | 93 | public void testEntries() { |
102 | | - map.set("foo", Arrays.<String>asList("foo_value_1", "foo_value_2")); |
103 | | - List<Map.Entry<String, String>> entries = map.entries(); |
104 | | - assertEquals(entries.size(), 1); |
| 94 | + HttpHeaders headers = newMultiMap(); |
| 95 | + headers.set("foo", Arrays.<String>asList("foo_value_1", "foo_value_2")); |
| 96 | + List<Map.Entry<String, String>> entries = headers.entries(); |
| 97 | + assertEquals(1, entries.size()); |
105 | 98 | assertEquals("foo", entries.get(0).getKey()); |
106 | 99 | assertEquals("foo_value_1", entries.get(0).getValue()); |
107 | | - map.set("bar", "bar_value"); |
108 | | - Map<String, String> collected = map.entries().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 100 | + headers.set("bar", "bar_value"); |
| 101 | + Map<String, String> collected = headers.entries().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
109 | 102 | assertEquals("foo_value_1", collected.get("foo")); |
110 | 103 | assertEquals("bar_value", collected.get("bar")); |
111 | 104 | } |
112 | 105 |
|
113 | | - private void assertHeaderNames(String... expected) { |
114 | | - assertEquals(new HashSet<>(Arrays.asList(expected)), headers.names().stream().map(CharSequence::toString).collect(Collectors.toSet())); |
| 106 | + private void assertHeaderNames(HttpHeaders headers, String... expected) { |
| 107 | + assertEquals(new HashSet<>(Arrays.asList(expected)), headers.unwrap().names().stream().map(CharSequence::toString).collect(Collectors.toSet())); |
115 | 108 | } |
116 | 109 |
|
117 | 110 | private HttpHeaders headers(HttpMethod method, String authority, String host) { |
@@ -147,4 +140,18 @@ public void testAuthorityValidation() { |
147 | 140 | assertFalse(headers(HttpMethod.CONNECT, "localhost:a", null).validate()); |
148 | 141 | assertFalse(headers(HttpMethod.CONNECT, null, "localhost:a").validate()); |
149 | 142 | } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testFilterPseudoHeaders() { |
| 146 | + HttpHeaders headers = headers(HttpMethod.PUT, "localhost:8080", "another:4443"); |
| 147 | + List<String> names = new ArrayList<>(); |
| 148 | + headers.forEach(entry -> { |
| 149 | + names.add(entry.getKey()); |
| 150 | + }); |
| 151 | + assertEquals(List.of("host"), names); |
| 152 | + assertEquals(1, headers.size()); |
| 153 | + HttpHeaders other = newMultiMap(); |
| 154 | + other.setAll(headers); |
| 155 | + assertEquals(1, other.unwrap().size()); |
| 156 | + } |
150 | 157 | } |
0 commit comments