1- import HTTPResponseDate
21import Foundation
2+ import HTTPResponseDate
33import XCTest
44
55final class HTTPResponseDateTests : XCTestCase {
6- /// A set of timestamps relative to 1970 (taken from `DateFormatter`'s test cases), along with an `HTTPURLResponse` for each supported format of `date` header.
7- static private let testResponses : [ Date : ( rfc1123: HTTPURLResponse , rfc850: HTTPURLResponse , asctime: HTTPURLResponse ) ] = [
8- 504334638.0 : ( rfc1123: " Sun, 25 Dec 2016 04:57:18 GMT " , rfc850: " Sunday, 25-Dec-16 04:57:18 GMT " , asctime: " Sun Dec 25 04:57:18 2016 " ) ,
9- - 946771200 : ( rfc1123: " Fri, 01 Jan 1971 00:00:00 GMT " , rfc850: " Friday, 01-Jan-71 00:00:00 GMT " , asctime: " Fri Jan 1 00:00:00 1971 " ) ,
10- 499755438 : ( rfc1123: " Wed, 02 Nov 2016 04:57:18 GMT " , rfc850: " Wednesday, 02-Nov-16 04:57:18 GMT " , asctime: " Wed Nov 2 04:57:18 2016 " ) ,
11- 492411438 : ( rfc1123: " Tue, 09 Aug 2016 04:57:18 GMT " , rfc850: " Tuesday, 09-Aug-16 04:57:18 GMT " , asctime: " Tue Aug 9 04:57:18 2016 " ) ,
12- 487227438 : ( rfc1123: " Fri, 10 Jun 2016 04:57:18 GMT " , rfc850: " Friday, 10-Jun-16 04:57:18 GMT " , asctime: " Fri Jun 10 04:57:18 2016 " ) ,
13- 498977838 : ( rfc1123: " Mon, 24 Oct 2016 04:57:18 GMT " , rfc850: " Monday, 24-Oct-16 04:57:18 GMT " , asctime: " Mon Oct 24 04:57:18 2016 " ) ,
14- 1167609600 : ( rfc1123: " Fri, 01 Jan 2038 00:00:00 GMT " , rfc850: " Friday, 01-Jan-38 00:00:00 GMT " , asctime: " Fri Jan 1 00:00:00 2038 " ) ,
15- 480315438 : ( rfc1123: " Tue, 22 Mar 2016 04:57:18 GMT " , rfc850: " Tuesday, 22-Mar-16 04:57:18 GMT " , asctime: " Tue Mar 22 04:57:18 2016 " ) ,
16- 474267438 : ( rfc1123: " Tue, 12 Jan 2016 04:57:18 GMT " , rfc850: " Tuesday, 12-Jan-16 04:57:18 GMT " , asctime: " Tue Jan 12 04:57:18 2016 " ) ,
17- - 978307200 : ( rfc1123: " Thu, 01 Jan 1970 00:00:00 GMT " , rfc850: " Thursday, 01-Jan-70 00:00:00 GMT " , asctime: " Thu Jan 1 00:00:00 1970 " ) ,
18- 495608238 : ( rfc1123: " Thu, 15 Sep 2016 04:57:18 GMT " , rfc850: " Thursday, 15-Sep-16 04:57:18 GMT " , asctime: " Thu Sep 15 04:57:18 2016 " ) ,
19- - 1009843200 : ( rfc1123: " Wed, 01 Jan 1969 00:00:00 GMT " , rfc850: " Wednesday, 01-Jan-69 00:00:00 GMT " , asctime: " Wed Jan 1 00:00:00 1969 " ) ,
20- 477377838 : ( rfc1123: " Wed, 17 Feb 2016 04:57:18 GMT " , rfc850: " Wednesday, 17-Feb-16 04:57:18 GMT " , asctime: " Wed Feb 17 04:57:18 2016 " ) ,
21- 484289838 : ( rfc1123: " Sat, 07 May 2016 04:57:18 GMT " , rfc850: " Saturday, 07-May-16 04:57:18 GMT " , asctime: " Sat May 7 04:57:18 2016 " ) ,
22- 491547438 : ( rfc1123: " Sat, 30 Jul 2016 04:57:18 GMT " , rfc850: " Saturday, 30-Jul-16 04:57:18 GMT " , asctime: " Sat Jul 30 04:57:18 2016 " ) ,
23- 477964800 : ( rfc1123: " Wed, 24 Feb 2016 00:00:00 GMT " , rfc850: " Wednesday, 24-Feb-16 00:00:00 GMT " , asctime: " Wed Feb 24 00:00:00 2016 " ) ,
24- 481438638 : ( rfc1123: " Mon, 04 Apr 2016 04:57:18 GMT " , rfc850: " Monday, 04-Apr-16 04:57:18 GMT " , asctime: " Mon Apr 4 04:57:18 2016 " ) ,
25- 478051199 : ( rfc1123: " Wed, 24 Feb 2016 23:59:59 GMT " , rfc850: " Wednesday, 24-Feb-16 23:59:59 GMT " , asctime: " Wed Feb 24 23:59:59 2016 " ) ,
26- ]
27- . reduce ( into: [ : ] ) { testResponses, entry in
28- let mockResponse = { ( timestamp: String ) in
29- HTTPURLResponse (
30- url: URL ( string: " example.com " ) !,
31- statusCode: 200 ,
32- httpVersion: " HTTP/2 " ,
33- headerFields: [ " date " : timestamp]
34- ) !
35- }
36- testResponses [ Date ( timeIntervalSinceReferenceDate: entry. key) ] = (
37- mockResponse ( entry. value. rfc1123) ,
38- mockResponse ( entry. value. rfc850) ,
39- mockResponse ( entry. value. asctime)
40- )
41- }
6+ /// A set of dates along with an `HTTPURLResponse` for each supported `date` header representation of them.
7+ ///
8+ /// These dates were taken from `DateFormatter`'s test cases.
9+ static private let testResponses : [
10+ Date : ( rfc1123: HTTPURLResponse , rfc850: HTTPURLResponse , asctime: HTTPURLResponse )
11+ ] = [
12+ 504334638.0 : (
13+ rfc1123: " Sun, 25 Dec 2016 04:57:18 GMT " ,
14+ rfc850: " Sunday, 25-Dec-16 04:57:18 GMT " ,
15+ asctime: " Sun Dec 25 04:57:18 2016 "
16+ ) ,
17+ - 946771200 : (
18+ rfc1123: " Fri, 01 Jan 1971 00:00:00 GMT " ,
19+ rfc850: " Friday, 01-Jan-71 00:00:00 GMT " ,
20+ asctime: " Fri Jan 1 00:00:00 1971 "
21+ ) ,
22+ 499755438 : (
23+ rfc1123: " Wed, 02 Nov 2016 04:57:18 GMT " ,
24+ rfc850: " Wednesday, 02-Nov-16 04:57:18 GMT " ,
25+ asctime: " Wed Nov 2 04:57:18 2016 "
26+ ) ,
27+ 492411438 : (
28+ rfc1123: " Tue, 09 Aug 2016 04:57:18 GMT " ,
29+ rfc850: " Tuesday, 09-Aug-16 04:57:18 GMT " ,
30+ asctime: " Tue Aug 9 04:57:18 2016 "
31+ ) ,
32+ 487227438 : (
33+ rfc1123: " Fri, 10 Jun 2016 04:57:18 GMT " ,
34+ rfc850: " Friday, 10-Jun-16 04:57:18 GMT " ,
35+ asctime: " Fri Jun 10 04:57:18 2016 "
36+ ) ,
37+ 498977838 : (
38+ rfc1123: " Mon, 24 Oct 2016 04:57:18 GMT " ,
39+ rfc850: " Monday, 24-Oct-16 04:57:18 GMT " ,
40+ asctime: " Mon Oct 24 04:57:18 2016 "
41+ ) ,
42+ 1167609600 : (
43+ rfc1123: " Fri, 01 Jan 2038 00:00:00 GMT " ,
44+ rfc850: " Friday, 01-Jan-38 00:00:00 GMT " ,
45+ asctime: " Fri Jan 1 00:00:00 2038 "
46+ ) ,
47+ 480315438 : (
48+ rfc1123: " Tue, 22 Mar 2016 04:57:18 GMT " ,
49+ rfc850: " Tuesday, 22-Mar-16 04:57:18 GMT " ,
50+ asctime: " Tue Mar 22 04:57:18 2016 "
51+ ) ,
52+ 474267438 : (
53+ rfc1123: " Tue, 12 Jan 2016 04:57:18 GMT " ,
54+ rfc850: " Tuesday, 12-Jan-16 04:57:18 GMT " ,
55+ asctime: " Tue Jan 12 04:57:18 2016 "
56+ ) ,
57+ - 978307200 : (
58+ rfc1123: " Thu, 01 Jan 1970 00:00:00 GMT " ,
59+ rfc850: " Thursday, 01-Jan-70 00:00:00 GMT " ,
60+ asctime: " Thu Jan 1 00:00:00 1970 "
61+ ) ,
62+ 495608238 : (
63+ rfc1123: " Thu, 15 Sep 2016 04:57:18 GMT " ,
64+ rfc850: " Thursday, 15-Sep-16 04:57:18 GMT " ,
65+ asctime: " Thu Sep 15 04:57:18 2016 "
66+ ) ,
67+ - 1009843200 : (
68+ rfc1123: " Wed, 01 Jan 1969 00:00:00 GMT " ,
69+ rfc850: " Wednesday, 01-Jan-69 00:00:00 GMT " ,
70+ asctime: " Wed Jan 1 00:00:00 1969 "
71+ ) ,
72+ 477377838 : (
73+ rfc1123: " Wed, 17 Feb 2016 04:57:18 GMT " ,
74+ rfc850: " Wednesday, 17-Feb-16 04:57:18 GMT " ,
75+ asctime: " Wed Feb 17 04:57:18 2016 "
76+ ) ,
77+ 484289838 : (
78+ rfc1123: " Sat, 07 May 2016 04:57:18 GMT " ,
79+ rfc850: " Saturday, 07-May-16 04:57:18 GMT " ,
80+ asctime: " Sat May 7 04:57:18 2016 "
81+ ) ,
82+ 491547438 : (
83+ rfc1123: " Sat, 30 Jul 2016 04:57:18 GMT " ,
84+ rfc850: " Saturday, 30-Jul-16 04:57:18 GMT " ,
85+ asctime: " Sat Jul 30 04:57:18 2016 "
86+ ) ,
87+ 477964800 : (
88+ rfc1123: " Wed, 24 Feb 2016 00:00:00 GMT " ,
89+ rfc850: " Wednesday, 24-Feb-16 00:00:00 GMT " ,
90+ asctime: " Wed Feb 24 00:00:00 2016 "
91+ ) ,
92+ 481438638 : (
93+ rfc1123: " Mon, 04 Apr 2016 04:57:18 GMT " ,
94+ rfc850: " Monday, 04-Apr-16 04:57:18 GMT " ,
95+ asctime: " Mon Apr 4 04:57:18 2016 "
96+ ) ,
97+ 478051199 : (
98+ rfc1123: " Wed, 24 Feb 2016 23:59:59 GMT " ,
99+ rfc850: " Wednesday, 24-Feb-16 23:59:59 GMT " ,
100+ asctime: " Wed Feb 24 23:59:59 2016 "
101+ ) ,
102+ ]
103+ . reduce ( into: [ : ] ) { testResponses, entry in
104+ let mockResponse = { ( timestamp: String ) in
105+ HTTPURLResponse (
106+ url: URL ( string: " example.com " ) !,
107+ statusCode: 200 ,
108+ httpVersion: " HTTP/2 " ,
109+ headerFields: [ " date " : timestamp]
110+ ) !
111+ }
112+ testResponses [ Date ( timeIntervalSinceReferenceDate: entry. key) ] = (
113+ mockResponse ( entry. value. rfc1123) ,
114+ mockResponse ( entry. value. rfc850) ,
115+ mockResponse ( entry. value. asctime)
116+ )
117+ }
42118
43- func testRFC1123TimestampParsing( )
44- {
45- Self . testResponses. forEach {
46- XCTAssertEqual ( $0. key, $0. value. rfc1123. date)
47- }
119+ func testRFC1123TimestampParsing( ) {
120+ Self . testResponses. forEach { XCTAssertEqual ( $0. key, $0. value. rfc1123. date) }
48121 }
49122
50123 // Since the year is truncated to two-digits, it is impossible to consistently parse the entire date from this format.
51- func testRFC850TimestampParsing( ) throws
52- {
124+ func testRFC850TimestampParsing( ) throws {
53125 try Self . testResponses. forEach {
54126 let parsedDate = try XCTUnwrap ( $0. value. rfc850. date)
55127
56- guard $0. key != parsedDate else {
57- return XCTAssert ( true )
58- }
128+ guard $0. key != parsedDate else { return XCTAssert ( true ) }
59129
60- let actualDateComponents = Calendar . current. dateComponents ( in: TimeZone ( abbreviation: " GMT " ) !, from: $0. key)
61- let parsedDateComponents = Calendar . current. dateComponents ( in: TimeZone ( abbreviation: " GMT " ) !, from: try XCTUnwrap ( $0. value. rfc850. date) )
130+ let actualDateComponents
131+ = Calendar . current. dateComponents ( in: TimeZone ( abbreviation: " GMT " ) !, from: $0. key)
132+ let parsedDateComponents = Calendar . current. dateComponents (
133+ in: TimeZone ( abbreviation: " GMT " ) !,
134+ from: try XCTUnwrap ( $0. value. rfc850. date)
135+ )
62136
63- XCTAssertEqual ( try XCTUnwrap ( actualDateComponents. year) % 100 , try XCTUnwrap ( parsedDateComponents. year) % 100 )
64- for component in [ Calendar . Component. weekday, . day, . month, . hour, . minute, . second, . timeZone] {
65- XCTAssertEqual ( try XCTUnwrap ( actualDateComponents. value ( for: component) ) , try XCTUnwrap ( parsedDateComponents. value ( for: component) ) )
137+ XCTAssertEqual (
138+ try XCTUnwrap ( actualDateComponents. year) % 100 ,
139+ try XCTUnwrap ( parsedDateComponents. year) % 100
140+ )
141+ for component in
142+ [ Calendar . Component. weekday, . day, . month, . hour, . minute, . second, . timeZone] {
143+ XCTAssertEqual (
144+ try XCTUnwrap ( actualDateComponents. value ( for: component) ) ,
145+ try XCTUnwrap ( parsedDateComponents. value ( for: component) )
146+ )
66147 }
67148 }
68149 }
69150
70- func testAsctimeTimestampParsing( )
71- {
72- Self . testResponses. forEach {
73- XCTAssertEqual ( $0. key, $0. value. asctime. date)
74- }
151+ func testAsctimeTimestampParsing( ) {
152+ Self . testResponses. forEach { XCTAssertEqual ( $0. key, $0. value. asctime. date) }
75153 }
76154
77155 func testMalformedTimestampParsing( ) {
@@ -86,11 +164,4 @@ final class HTTPResponseDateTests: XCTestCase {
86164 . date
87165 )
88166 }
89-
90- static var allTests = [
91- ( " RFC 1123 Timestamp Parsing " , testRFC1123TimestampParsing) ,
92- ( " RFC 850 Timestamp Parsing " , testRFC850TimestampParsing) ,
93- ( " asctime Timestamp Parsing " , testAsctimeTimestampParsing) ,
94- ( " Malformed Timestamp Parsing " , testMalformedTimestampParsing)
95- ]
96167}
0 commit comments