This commit is contained in:
parent
4683b077da
commit
b4b3aa9c94
|
|
@ -161,11 +161,11 @@ namespace Microsoft.AspNetCore.Http
|
||||||
|
|
||||||
private static IDictionary<Type, object> KnownListParsers = new Dictionary<Type, object>()
|
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(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 => { IList<StringWithQualityHeaderValue> result; return StringWithQualityHeaderValue.TryParseList(value, out result) ? result : null; }) },
|
{ 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 => { IList<CookieHeaderValue> result; return CookieHeaderValue.TryParseList(value, out result) ? result : null; }) },
|
{ 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 => { IList<EntityTagHeaderValue> result; return EntityTagHeaderValue.TryParseList(value, out result) ? result : null; }) },
|
{ 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 => { IList<SetCookieHeaderValue> result; return SetCookieHeaderValue.TryParseList(value, out result) ? result : null; }) },
|
{ 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)
|
internal static T Get<T>(this IHeaderDictionary headers, string name)
|
||||||
|
|
@ -204,7 +204,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
|
|
||||||
if (StringValues.IsNullOrEmpty(values))
|
if (StringValues.IsNullOrEmpty(values))
|
||||||
{
|
{
|
||||||
return null;
|
return Array.Empty<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (KnownListParsers.TryGetValue(typeof(T), out temp))
|
if (KnownListParsers.TryGetValue(typeof(T), out temp))
|
||||||
|
|
@ -281,7 +281,7 @@ namespace Microsoft.AspNetCore.Http
|
||||||
{
|
{
|
||||||
return (IList<T>)parameters[1];
|
return (IList<T>)parameters[1];
|
||||||
}
|
}
|
||||||
return null;
|
return Array.Empty<T>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,24 +99,24 @@ namespace Microsoft.AspNetCore.Http.Headers
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetListT_KnownTypeWithMissingValue_Null()
|
public void GetListT_KnownTypeWithMissingValue_EmptyList()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext();
|
var context = new DefaultHttpContext();
|
||||||
|
|
||||||
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
|
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
|
||||||
|
|
||||||
Assert.Null(result);
|
Assert.Empty(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetListT_KnownTypeWithInvalidValue_Null()
|
public void GetListT_KnownTypeWithInvalidValue_EmptyList()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext();
|
var context = new DefaultHttpContext();
|
||||||
context.Request.Headers[HeaderNames.Accept] = "invalid";
|
context.Request.Headers[HeaderNames.Accept] = "invalid";
|
||||||
|
|
||||||
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
|
var result = context.Request.GetTypedHeaders().GetList<MediaTypeHeaderValue>(HeaderNames.Accept);
|
||||||
|
|
||||||
Assert.Null(result);
|
Assert.Empty(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -131,22 +131,22 @@ namespace Microsoft.AspNetCore.Http.Headers
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetListT_UnknownTypeWithTryParseListAndInvalidValue_Null()
|
public void GetListT_UnknownTypeWithTryParseListAndInvalidValue_EmptyList()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext();
|
var context = new DefaultHttpContext();
|
||||||
context.Request.Headers["custom"] = "invalid";
|
context.Request.Headers["custom"] = "invalid";
|
||||||
|
|
||||||
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
|
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
|
||||||
Assert.Null(results);
|
Assert.Empty(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetListT_UnknownTypeWithTryParseListAndMissingValue_Null()
|
public void GetListT_UnknownTypeWithTryParseListAndMissingValue_EmptyList()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext();
|
var context = new DefaultHttpContext();
|
||||||
|
|
||||||
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
|
var results = context.Request.GetTypedHeaders().GetList<TestHeaderValue>("custom");
|
||||||
Assert.Null(results);
|
Assert.Empty(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -202,4 +202,4 @@ namespace Microsoft.AspNetCore.Http.Headers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue