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

Commit b41d865

Browse files
committed
Adds new date string in HttpRuleParser
1 parent 68ba942 commit b41d865

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

src/Microsoft.Net.Http.Headers/HttpRuleParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ internal static class HttpRuleParser
2424
"d MMM yy H:m:s 'GMT'", // RFC 1123, no day-of-week, short year
2525
"d MMM yy H:m:s", // RFC 1123, no day-of-week, short year, no zone
2626

27-
"dddd, d'-'MMM'-'yy H:m:s 'GMT'", // RFC 850
27+
"dddd, d'-'MMM'-'yy H:m:s 'GMT'", // RFC 850, short year
2828
"dddd, d'-'MMM'-'yy H:m:s", // RFC 850 no zone
29+
"ddd, d'-'MMM'-'yyyy H:m:s 'GMT'", // RFC 850, long year
2930
"ddd MMM d H:m:s yyyy", // ANSI C's asctime() format
3031

3132
"ddd, d MMM yyyy H:m:s zzz", // RFC 5322

test/Microsoft.Net.Http.Headers.Tests/DateParserTest.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,47 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using Xunit;
67

78
namespace Microsoft.Net.Http.Headers
89
{
910
public class DateParserTest
1011
{
11-
[Fact]
12-
public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
12+
[Theory]
13+
[MemberData(nameof(ValidStringData))]
14+
public void TryParse_SetOfValidValueStrings_ParsedCorrectly(string input, DateTimeOffset expected)
1315
{
1416
// We don't need to validate all possible date values, since they're already tested in HttpRuleParserTest.
1517
// Just make sure the parser calls HttpRuleParser methods correctly.
16-
CheckValidParsedValue("Tue, 15 Nov 1994 08:12:31 GMT", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero));
17-
CheckValidParsedValue(" Sunday, 06-Nov-94 08:49:37 GMT ", new DateTimeOffset(1994, 11, 6, 8, 49, 37, TimeSpan.Zero));
18-
CheckValidParsedValue(" Tue,\r\n 15 Nov\r\n 1994 08:12:31 GMT ", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero));
18+
DateTimeOffset result;
19+
Assert.True(HeaderUtilities.TryParseDate(input, out result));
20+
Assert.Equal(expected, result);
1921
}
2022

21-
[Fact]
22-
public void TryParse_SetOfInvalidValueStrings_ReturnsFalse()
23+
private static IEnumerable<object[]> ValidStringData()
24+
{
25+
yield return new object[] { "Tue, 15 Nov 1994 08:12:31 GMT", new DateTimeOffset(1994, 11, 15, 8, 12, 31, TimeSpan.Zero) };
26+
yield return new object[] { " Sunday, 06-Nov-94 08:49:37 GMT ", new DateTimeOffset(1994, 11, 6, 8, 49, 37, TimeSpan.Zero) };
27+
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) };
28+
yield return new object[] { "Sat, 09-Dec-2017 07:07:03 GMT ", new DateTimeOffset(2017, 12, 09, 7, 7, 3, TimeSpan.Zero) };
29+
}
30+
31+
[Theory]
32+
[MemberData(nameof(InvalidStringData))]
33+
public void TryParse_SetOfInvalidValueStrings_ReturnsFalse(string input)
34+
{
35+
DateTimeOffset result;
36+
Assert.False(HeaderUtilities.TryParseDate(input, out result));
37+
Assert.Equal(new DateTimeOffset(), result);
38+
}
39+
40+
private static IEnumerable<object[]> InvalidStringData()
2341
{
24-
CheckInvalidParsedValue(null);
25-
CheckInvalidParsedValue(string.Empty);
26-
CheckInvalidParsedValue(" ");
27-
CheckInvalidParsedValue("!!Sunday, 06-Nov-94 08:49:37 GMT");
42+
yield return new object[] { null };
43+
yield return new object[] { string.Empty };
44+
yield return new object[] { " " };
45+
yield return new object[] { "!!Sunday, 06-Nov-94 08:49:37 GMT" };
2846
}
2947

3048
[Fact]
@@ -36,23 +54,5 @@ public void ToString_UseDifferentValues_MatchExpectation()
3654
Assert.Equal("Fri, 01 Jan 2010 01:01:01 GMT",
3755
HeaderUtilities.FormatDate(new DateTimeOffset(2010, 1, 1, 1, 1, 1, TimeSpan.Zero)));
3856
}
39-
40-
#region Helper methods
41-
42-
private void CheckValidParsedValue(string input, DateTimeOffset expectedResult)
43-
{
44-
DateTimeOffset result;
45-
Assert.True(HeaderUtilities.TryParseDate(input, out result));
46-
Assert.Equal(expectedResult, result);
47-
}
48-
49-
private void CheckInvalidParsedValue(string input)
50-
{
51-
DateTimeOffset result;
52-
Assert.False(HeaderUtilities.TryParseDate(input, out result));
53-
Assert.Equal(new DateTimeOffset(), result);
54-
}
55-
56-
#endregion
5757
}
5858
}

0 commit comments

Comments
 (0)