提交 ab8310b5 编写于 作者: B Brian Clozel

Fix HeadersAdapters implementations

This commit harmonizes the `HeadersAdapter` implementations across all
supported servers with regards to the `get(Object key)` contract; some
server implementations are not sticking to a `Map`-like contract and
return empty `List` instead of `null` when a header is not present.

This also fixes the `size()` implementations to reflect the number of
header keys, as some implementations consider multiple values for the
same header as different entries.

Issue: SPR-17396
上级 fdaceeb6
......@@ -114,7 +114,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Nullable
@Override
public List<String> get(Object key) {
if (key instanceof String) {
if (containsKey(key)) {
return this.headers.getValuesList((String) key);
}
return null;
......
......@@ -91,7 +91,7 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public int size() {
return this.headers.size();
return this.headers.names().size();
}
@Override
......@@ -114,7 +114,7 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
@Nullable
public List<String> get(Object key) {
if (key instanceof String) {
if (containsKey(key)) {
return this.headers.getAll((String) key);
}
return null;
......
......@@ -128,7 +128,7 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
@Override
@Nullable
public List<String> get(Object key) {
if (key instanceof String) {
if (containsKey(key)) {
return Collections.list(this.headers.values((String) key));
}
return null;
......
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.server.reactive;
import java.util.Locale;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.undertow.util.HeaderMap;
import org.apache.tomcat.util.http.MimeHeaders;
import org.eclipse.jetty.http.HttpFields;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@code HeadersAdapters} {@code MultiValueMap} implementations.
*
* @author Brian Clozel
*/
@RunWith(Parameterized.class)
public class HeadersAdaptersTests {
@Parameterized.Parameter(0)
public MultiValueMap<String, String> headers;
@Parameterized.Parameters(name = "headers [{0}]")
public static Object[][] arguments() {
return new Object[][] {
{CollectionUtils.toMultiValueMap(
new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH))},
{new NettyHeadersAdapter(new DefaultHttpHeaders())},
{new TomcatHeadersAdapter(new MimeHeaders())},
{new UndertowHeadersAdapter(new HeaderMap())},
{new JettyHeadersAdapter(new HttpFields())}
};
}
@Test
public void getWithUnknownHeaderShouldReturnNull() {
assertNull(this.headers.get("Unknown"));
}
@Test
public void getFirstWithUnknownHeaderShouldReturnNull() {
assertNull(this.headers.getFirst("Unknown"));
}
@Test
public void sizeWithMultipleValuesForHeaderShouldCountHeaders() {
this.headers.add("TestHeader", "first");
this.headers.add("TestHeader", "second");
assertEquals(1, this.headers.size());
}
@Test
public void keySetShouldNotDuplicateHeaderNames() {
this.headers.add("TestHeader", "first");
this.headers.add("OtherHeader", "test");
this.headers.add("TestHeader", "second");
assertEquals(2, this.headers.keySet().size());
}
@Test
public void containsKeyShouldBeCaseInsensitive() {
this.headers.add("TestHeader", "first");
assertTrue(this.headers.containsKey("testheader"));
}
@Test
public void addShouldKeepOrdering() {
this.headers.add("TestHeader", "first");
this.headers.add("TestHeader", "second");
assertEquals("first", this.headers.getFirst("TestHeader"));
assertEquals("first", this.headers.get("TestHeader").get(0));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册