Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Ignore empty header values #837

Merged
merged 1 commit into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ private static IEnumerable<string> GetHeaderSplitImplementation(StringValues val
{
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;
}
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions test/Microsoft.AspNetCore.Http.Tests/HeaderDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Primitives;
using Xunit;

Expand All @@ -15,7 +16,9 @@ public class HeaderDictionaryTests
new[] { "Value1", "Value2", "Value3", "Value4" },
new[] { "Value1", "", "Value3", "Value4" },
new[] { "Value1", "", "", "Value4" },
new[] { "", "", "", "" }
new[] { "Value1", "", null, "Value4" },
new[] { "", "", "", "" },
new[] { "", null, "", null },
};

[Fact]
Expand All @@ -37,7 +40,7 @@ public void PropertiesAreAccessible()

[Theory]
[MemberData(nameof(HeaderSegmentData))]
public void EmptyHeaderSegmentsAreParsable(IEnumerable<string> segments)
public void EmptyHeaderSegmentsAreIgnored(IEnumerable<string> segments)
{
var header = string.Join(",", segments);

Expand All @@ -48,8 +51,22 @@ public void EmptyHeaderSegmentsAreParsable(IEnumerable<string> segments)
});

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);
}
}
}