diff --git a/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs b/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs index 5a6adf9415..3741ffa110 100644 --- a/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs +++ b/src/Microsoft.Net.Http.Headers/HttpRuleParser.cs @@ -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 diff --git a/test/Microsoft.Net.Http.Headers.Tests/DateParserTest.cs b/test/Microsoft.Net.Http.Headers.Tests/DateParserTest.cs index 0fffc0c134..83fec06c46 100644 --- a/test/Microsoft.Net.Http.Headers.Tests/DateParserTest.cs +++ b/test/Microsoft.Net.Http.Headers.Tests/DateParserTest.cs @@ -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 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 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 } }