Adds new date string in HttpRuleParser

This commit is contained in:
Justin Kotalik 2017-07-12 17:34:55 -07:00
parent 68ba94218a
commit b41d8656d5
2 changed files with 31 additions and 30 deletions

View File

@ -24,8 +24,9 @@ namespace Microsoft.Net.Http.Headers
"d MMM yy H:m:s 'GMT'", // RFC 1123, no day-of-week, short year
"d MMM yy H:m:s", // RFC 1123, no day-of-week, short year, no zone
"dddd, d'-'MMM'-'yy H:m:s 'GMT'", // RFC 850
"dddd, d'-'MMM'-'yy H:m:s 'GMT'", // RFC 850, short year
"dddd, d'-'MMM'-'yy H:m:s", // RFC 850 no zone
"ddd, d'-'MMM'-'yyyy H:m:s 'GMT'", // RFC 850, long year
"ddd MMM d H:m:s yyyy", // ANSI C's asctime() format
"ddd, d MMM yyyy H:m:s zzz", // RFC 5322

View File

@ -2,29 +2,47 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Xunit;
namespace Microsoft.Net.Http.Headers
{
public class DateParserTest
{
[Fact]
public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
[Theory]
[MemberData(nameof(ValidStringData))]
public void TryParse_SetOfValidValueStrings_ParsedCorrectly(string input, DateTimeOffset expected)
{
// We don't need to validate all possible date values, since they're already tested in HttpRuleParserTest.
// Just make sure the parser calls HttpRuleParser methods correctly.
CheckValidParsedValue("Tue, 15 Nov 1994 08:12:31 GMT", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero));
CheckValidParsedValue(" Sunday, 06-Nov-94 08:49:37 GMT ", new DateTimeOffset(1994, 11, 6, 8, 49, 37, TimeSpan.Zero));
CheckValidParsedValue(" Tue,\r\n 15 Nov\r\n 1994 08:12:31 GMT ", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero));
DateTimeOffset result;
Assert.True(HeaderUtilities.TryParseDate(input, out result));
Assert.Equal(expected, result);
}
[Fact]
public void TryParse_SetOfInvalidValueStrings_ReturnsFalse()
private static IEnumerable<object[]> ValidStringData()
{
CheckInvalidParsedValue(null);
CheckInvalidParsedValue(string.Empty);
CheckInvalidParsedValue(" ");
CheckInvalidParsedValue("!!Sunday, 06-Nov-94 08:49:37 GMT");
yield return new object[] { "Tue, 15 Nov 1994 08:12:31 GMT", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero) };
yield return new object[] { " Sunday, 06-Nov-94 08:49:37 GMT ", new DateTimeOffset(1994, 11, 6, 8, 49, 37, TimeSpan.Zero) };
yield return new object[] { " Tue,\r\n 15 Nov\r\n 1994 08:12:31 GMT ", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero) };
yield return new object[] { "Sat, 09-Dec-2017 07:07:03 GMT ", new DateTimeOffset(2017, 12, 09, 7, 7, 3, TimeSpan.Zero) };
}
[Theory]
[MemberData(nameof(InvalidStringData))]
public void TryParse_SetOfInvalidValueStrings_ReturnsFalse(string input)
{
DateTimeOffset result;
Assert.False(HeaderUtilities.TryParseDate(input, out result));
Assert.Equal(new DateTimeOffset(), result);
}
private static IEnumerable<object[]> InvalidStringData()
{
yield return new object[] { null };
yield return new object[] { string.Empty };
yield return new object[] { " " };
yield return new object[] { "!!Sunday, 06-Nov-94 08:49:37 GMT" };
}
[Fact]
@ -36,23 +54,5 @@ namespace Microsoft.Net.Http.Headers
Assert.Equal("Fri, 01 Jan 2010 01:01:01 GMT",
HeaderUtilities.FormatDate(new DateTimeOffset(2010, 1, 1, 1, 1, 1, TimeSpan.Zero)));
}
#region Helper methods
private void CheckValidParsedValue(string input, DateTimeOffset expectedResult)
{
DateTimeOffset result;
Assert.True(HeaderUtilities.TryParseDate(input, out result));
Assert.Equal(expectedResult, result);
}
private void CheckInvalidParsedValue(string input)
{
DateTimeOffset result;
Assert.False(HeaderUtilities.TryParseDate(input, out result));
Assert.Equal(new DateTimeOffset(), result);
}
#endregion
}
}