Skip to content

Commit

Permalink
Clean up test files
Browse files Browse the repository at this point in the history
The Linux test files are now autogenerated, instead of being maintained manually.
The test input is now broken into multiple lines to comply with the 100-character column limit.
Various minor code style errors have also been corrected.
  • Loading branch information
Saklad5 committed Apr 20, 2020
1 parent 53656b2 commit 3ff1139
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 70 deletions.
199 changes: 135 additions & 64 deletions Tests/HTTPResponseDateTests/HTTPResponseDateTests.swift
Original file line number Diff line number Diff line change
@@ -1,77 +1,155 @@
import HTTPResponseDate
import Foundation
import HTTPResponseDate
import XCTest

final class HTTPResponseDateTests: XCTestCase {
/// A set of timestamps relative to 1970 (taken from `DateFormatter`'s test cases), along with an `HTTPURLResponse` for each supported format of `date` header.
static private let testResponses: [Date: (rfc1123: HTTPURLResponse, rfc850: HTTPURLResponse, asctime: HTTPURLResponse)] = [

This comment has been minimized.

Copy link
@MAGNA-RX7

MAGNA-RX7 Feb 17, 2023

Hack whatsapp mobile

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"),
-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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
-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"),
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"),
-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"),
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"),
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"),
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"),
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"),
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"),
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"),
]
.reduce(into: [:]) { testResponses, entry in
let mockResponse = { (timestamp: String) in
HTTPURLResponse(
url: URL(string: "example.com")!,
statusCode: 200,
httpVersion: "HTTP/2",
headerFields: ["date": timestamp]
)!
}
testResponses[Date(timeIntervalSinceReferenceDate: entry.key)] = (
mockResponse(entry.value.rfc1123),
mockResponse(entry.value.rfc850),
mockResponse(entry.value.asctime)
)
}
/// A set of dates along with an `HTTPURLResponse` for each supported `date` header representation of them.
///
/// These dates were taken from `DateFormatter`'s test cases.
static private let testResponses: [
Date: (rfc1123: HTTPURLResponse, rfc850: HTTPURLResponse, asctime: HTTPURLResponse)
] = [
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"
),
-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"
),
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"
),
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"
),
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"
),
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"
),
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"
),
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"
),
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"
),
-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"
),
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"
),
-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"
),
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"
),
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"
),
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"
),
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"
),
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"
),
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"
),
]
.reduce(into: [:]) { testResponses, entry in
let mockResponse = { (timestamp: String) in
HTTPURLResponse(
url: URL(string: "example.com")!,
statusCode: 200,
httpVersion: "HTTP/2",
headerFields: ["date": timestamp]
)!
}
testResponses[Date(timeIntervalSinceReferenceDate: entry.key)] = (
mockResponse(entry.value.rfc1123),
mockResponse(entry.value.rfc850),
mockResponse(entry.value.asctime)
)
}

func testRFC1123TimestampParsing()
{
Self.testResponses.forEach {
XCTAssertEqual($0.key, $0.value.rfc1123.date)
}
func testRFC1123TimestampParsing() {
Self.testResponses.forEach { XCTAssertEqual($0.key, $0.value.rfc1123.date) }
}

// Since the year is truncated to two-digits, it is impossible to consistently parse the entire date from this format.
func testRFC850TimestampParsing() throws
{
func testRFC850TimestampParsing() throws {
try Self.testResponses.forEach {
let parsedDate = try XCTUnwrap($0.value.rfc850.date)

guard $0.key != parsedDate else {
return XCTAssert(true)
}
guard $0.key != parsedDate else { return XCTAssert(true) }

let actualDateComponents = Calendar.current.dateComponents(in: TimeZone(abbreviation: "GMT")!, from: $0.key)
let parsedDateComponents = Calendar.current.dateComponents(in: TimeZone(abbreviation: "GMT")!, from: try XCTUnwrap($0.value.rfc850.date))
let actualDateComponents
= Calendar.current.dateComponents(in: TimeZone(abbreviation: "GMT")!, from: $0.key)
let parsedDateComponents = Calendar.current.dateComponents(
in: TimeZone(abbreviation: "GMT")!,
from: try XCTUnwrap($0.value.rfc850.date)
)

XCTAssertEqual(try XCTUnwrap(actualDateComponents.year) % 100, try XCTUnwrap(parsedDateComponents.year) % 100)
for component in [Calendar.Component.weekday, .day, .month, .hour, .minute, .second, .timeZone] {
XCTAssertEqual(try XCTUnwrap(actualDateComponents.value(for: component)), try XCTUnwrap(parsedDateComponents.value(for: component)))
XCTAssertEqual(
try XCTUnwrap(actualDateComponents.year) % 100,
try XCTUnwrap(parsedDateComponents.year) % 100
)
for component in
[Calendar.Component.weekday, .day, .month, .hour, .minute, .second, .timeZone] {
XCTAssertEqual(
try XCTUnwrap(actualDateComponents.value(for: component)),
try XCTUnwrap(parsedDateComponents.value(for: component))
)
}
}
}

func testAsctimeTimestampParsing()
{
Self.testResponses.forEach {
XCTAssertEqual($0.key, $0.value.asctime.date)
}
func testAsctimeTimestampParsing() {
Self.testResponses.forEach { XCTAssertEqual($0.key, $0.value.asctime.date) }
}

func testMalformedTimestampParsing() {
Expand All @@ -86,11 +164,4 @@ final class HTTPResponseDateTests: XCTestCase {
.date
)
}

static var allTests = [
("RFC 1123 Timestamp Parsing", testRFC1123TimestampParsing),
("RFC 850 Timestamp Parsing", testRFC850TimestampParsing),
("asctime Timestamp Parsing", testAsctimeTimestampParsing),
("Malformed Timestamp Parsing", testMalformedTimestampParsing)
]
}
22 changes: 17 additions & 5 deletions Tests/HTTPResponseDateTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#if !canImport(ObjectiveC)
import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(HTTPResponseDateTests.allTests),
]
extension HTTPResponseDateTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__HTTPResponseDateTests = [
("testAsctimeTimestampParsing", testAsctimeTimestampParsing),
("testMalformedTimestampParsing", testMalformedTimestampParsing),
("testRFC1123TimestampParsing", testRFC1123TimestampParsing),
("testRFC850TimestampParsing", testRFC850TimestampParsing),
]
}

public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(HTTPResponseDateTests.__allTests__HTTPResponseDateTests),
]
}
#endif
5 changes: 4 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import XCTest

import HTTPResponseDateTests

var tests = [XCTestCaseEntry]()
tests += HTTPResponseDateTests.allTests()
tests += HTTPResponseDateTests.__allTests()

XCTMain(tests)

0 comments on commit 3ff1139

Please sign in to comment.