Ignore empty header values #722

This commit is contained in:
John Luo 2017-05-15 11:16:24 -07:00
parent dfd938e4f7
commit 07470d41b1
2 changed files with 26 additions and 5 deletions

View File

@ -26,9 +26,13 @@ namespace Microsoft.AspNetCore.Http.Internal
{ {
foreach (var segment in new HeaderSegmentCollection(values)) foreach (var segment in new HeaderSegmentCollection(values))
{ {
if (segment.Data.HasValue) if (!StringSegment.IsNullOrEmpty(segment.Data))
{ {
yield return DeQuote(segment.Data.Value); var value = DeQuote(segment.Data.Value);
if (!string.IsNullOrEmpty(value))
{
yield return value;
}
} }
} }
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Xunit; using Xunit;
@ -15,7 +16,9 @@ namespace Microsoft.AspNetCore.Http
new[] { "Value1", "Value2", "Value3", "Value4" }, new[] { "Value1", "Value2", "Value3", "Value4" },
new[] { "Value1", "", "Value3", "Value4" }, new[] { "Value1", "", "Value3", "Value4" },
new[] { "Value1", "", "", "Value4" }, new[] { "Value1", "", "", "Value4" },
new[] { "", "", "", "" } new[] { "Value1", "", null, "Value4" },
new[] { "", "", "", "" },
new[] { "", null, "", null },
}; };
[Fact] [Fact]
@ -37,7 +40,7 @@ namespace Microsoft.AspNetCore.Http
[Theory] [Theory]
[MemberData(nameof(HeaderSegmentData))] [MemberData(nameof(HeaderSegmentData))]
public void EmptyHeaderSegmentsAreParsable(IEnumerable<string> segments) public void EmptyHeaderSegmentsAreIgnored(IEnumerable<string> segments)
{ {
var header = string.Join(",", segments); var header = string.Join(",", segments);
@ -48,8 +51,22 @@ namespace Microsoft.AspNetCore.Http
}); });
var result = headers.GetCommaSeparatedValues("Header1"); var result = headers.GetCommaSeparatedValues("Header1");
var expectedResult = segments.Where(s => !string.IsNullOrEmpty(s));
Assert.Equal(segments, result); Assert.Equal(expectedResult, result);
}
[Fact]
public void EmtpyQuotedHeaderSegmentsAreIgnored()
{
var headers = new HeaderDictionary(
new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
{
{ "Header1", "Value1,\"\",,Value2" },
});
var result = headers.GetCommaSeparatedValues("Header1");
Assert.Equal(new[] { "Value1", "Value2" }, result);
} }
} }
} }