Return an empty list instead of null for IHeaderDictionary extensions #3486 (#10972)

This commit is contained in:
Kahbazi 2019-06-09 21:15:36 +04:30 committed by Chris Ross
parent 4683b077da
commit b4b3aa9c94
2 changed files with 16 additions and 16 deletions

View File

@ -161,11 +161,11 @@ namespace Microsoft.AspNetCore.Http
private static IDictionary<Type, object> KnownListParsers = new Dictionary<Type, object>()
{
{ typeof(MediaTypeHeaderValue), new Func<IList<string>, IList<MediaTypeHeaderValue>>(value => { IList<MediaTypeHeaderValue> result; return MediaTypeHeaderValue.TryParseList(value, out result) ? result : null; }) },
{ typeof(StringWithQualityHeaderValue), new Func<IList<string>, IList<StringWithQualityHeaderValue>>(value => { IList<StringWithQualityHeaderValue> result; return StringWithQualityHeaderValue.TryParseList(value, out result) ? result : null; }) },
{ typeof(CookieHeaderValue), new Func<IList<string>, IList<CookieHeaderValue>>(value => { IList<CookieHeaderValue> result; return CookieHeaderValue.TryParseList(value, out result) ? result : null; }) },
{ typeof(EntityTagHeaderValue), new Func<IList<string>, IList<EntityTagHeaderValue>>(value => { IList<EntityTagHeaderValue> result; return EntityTagHeaderValue.TryParseList(value, out result) ? result : null; }) },
{ typeof(SetCookieHeaderValue), new Func<IList<string>, IList<SetCookieHeaderValue>>(value => { IList<SetCookieHeaderValue> result; return SetCookieHeaderValue.TryParseList(value, out result) ? result : null; }) },
{ typeof(MediaTypeHeaderValue), new Func<IList<string>, IList<MediaTypeHeaderValue>>(value => { return MediaTypeHeaderValue.TryParseList(value, out IList<MediaTypeHeaderValue> result) ? result : Array.Empty<MediaTypeHeaderValue>(); }) },
{ typeof(StringWithQualityHeaderValue), new Func<IList<string>, IList<StringWithQualityHeaderValue>>(value => { return StringWithQualityHeaderValue.TryParseList(value, out IList<StringWithQualityHeaderValue> result) ? result : Array.Empty<StringWithQualityHeaderValue>(); }) },
{ typeof(CookieHeaderValue), new Func<IList<string>, IList<CookieHeaderValue>>(value => { return CookieHeaderValue.TryParseList(value, out IList<CookieHeaderValue> result) ? result : Array.Empty<CookieHeaderValue>(); }) },
{ typeof(EntityTagHeaderValue), new Func<IList<string>, IList<EntityTagHeaderValue>>(value => { return EntityTagHeaderValue.TryParseList(value, out IList<EntityTagHeaderValue> result) ? result : Array.Empty<EntityTagHeaderValue>(); }) },
{ typeof(SetCookieHeaderValue), new Func<IList<string>, IList<SetCookieHeaderValue>>(value => { return SetCookieHeaderValue.TryParseList(value, out IList<SetCookieHeaderValue> result) ? result : Array.Empty<SetCookieHeaderValue>(); }) },
};
internal static T Get<T>(this IHeaderDictionary headers, string name)
@ -204,7 +204,7 @@ namespace Microsoft.AspNetCore.Http
if (StringValues.IsNullOrEmpty(values))
{
return null;
return Array.Empty<T>();
}
if (KnownListParsers.TryGetValue(typeof(T), out temp))
@ -281,7 +281,7 @@ namespace Microsoft.AspNetCore.Http
{
return (IList<T>)parameters[1];
}
return null;
return Array.Empty<T>();
}
}
}

View File

@ -99,24 +99,24 @@ namespace Microsoft.AspNetCore.Http.Headers
}
[Fact]
public void GetListT_KnownTypeWithMissingValue_Null()
public void GetListT_KnownTypeWithMissingValue_EmptyList()
{
var context = new DefaultHttpContext();
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
Assert.Null(result);
Assert.Empty(result);
}
[Fact]
public void GetListT_KnownTypeWithInvalidValue_Null()
public void GetListT_KnownTypeWithInvalidValue_EmptyList()
{
var context = new DefaultHttpContext();
context.Request.Headers[HeaderNames.Accept] = "invalid";
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
Assert.Null(result);
Assert.Empty(result);
}
[Fact]
@ -131,22 +131,22 @@ namespace Microsoft.AspNetCore.Http.Headers
}
[Fact]
public void GetListT_UnknownTypeWithTryParseListAndInvalidValue_Null()
public void GetListT_UnknownTypeWithTryParseListAndInvalidValue_EmptyList()
{
var context = new DefaultHttpContext();
context.Request.Headers["custom"] = "invalid";
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
Assert.Null(results);
Assert.Empty(results);
}
[Fact]
public void GetListT_UnknownTypeWithTryParseListAndMissingValue_Null()
public void GetListT_UnknownTypeWithTryParseListAndMissingValue_EmptyList()
{
var context = new DefaultHttpContext();
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
Assert.Null(results);
Assert.Empty(results);
}
[Fact]
@ -202,4 +202,4 @@ namespace Microsoft.AspNetCore.Http.Headers
}
}
}
}
}