22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Collections . Generic ;
56using Xunit ;
67
78namespace 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