From 02a490ed1422229749814f40472fd7ef72a49490 Mon Sep 17 00:00:00 2001 From: Will Stanton Date: Tue, 27 Sep 2016 01:51:59 +0000 Subject: [PATCH] Fix Any? -> Any build warnings New TZ test, HTTPCookie test revision Fixes whitespace around changes --- Foundation/NSHTTPCookie.swift | 115 +++++++++++++++----------- Foundation/TimeZone.swift | 2 +- Foundation/URLComponents.swift | 4 +- Foundation/URLRequest.swift | 24 +++--- TestFoundation/TestNSHTTPCookie.swift | 38 +++++---- TestFoundation/TestNSTimeZone.swift | 22 ++++- 6 files changed, 121 insertions(+), 84 deletions(-) diff --git a/Foundation/NSHTTPCookie.swift b/Foundation/NSHTTPCookie.swift index 4472dc1a5e..7da3b6ed06 100644 --- a/Foundation/NSHTTPCookie.swift +++ b/Foundation/NSHTTPCookie.swift @@ -37,39 +37,42 @@ extension HTTPCookiePropertyKey { /// Key for cookie value public static let value = HTTPCookiePropertyKey(rawValue: "Value") - + /// Key for cookie origin URL public static let originURL = HTTPCookiePropertyKey(rawValue: "OriginURL") /// Key for cookie version public static let version = HTTPCookiePropertyKey(rawValue: "Version") - + /// Key for cookie domain public static let domain = HTTPCookiePropertyKey(rawValue: "Domain") - + /// Key for cookie path public static let path = HTTPCookiePropertyKey(rawValue: "Path") - + /// Key for cookie secure flag public static let secure = HTTPCookiePropertyKey(rawValue: "Secure") - + /// Key for cookie expiration date public static let expires = HTTPCookiePropertyKey(rawValue: "Expires") /// Key for cookie comment text public static let comment = HTTPCookiePropertyKey(rawValue: "Comment") - + /// Key for cookie comment URL public static let commentURL = HTTPCookiePropertyKey(rawValue: "CommentURL") - + /// Key for cookie discard (session-only) flag public static let discard = HTTPCookiePropertyKey(rawValue: "Discard") - + /// Key for cookie maximum age (an alternate way of specifying the expiration) public static let maximumAge = HTTPCookiePropertyKey(rawValue: "Max-Age") /// Key for cookie ports public static let port = HTTPCookiePropertyKey(rawValue: "Port") + + // For Cocoa compatibility + internal static let created = HTTPCookiePropertyKey(rawValue: "Created") } /// `NSHTTPCookie` represents an http cookie. @@ -94,10 +97,10 @@ open class HTTPCookie : NSObject { let _version: Int var _properties: [HTTPCookiePropertyKey : Any] - static let _attributes: [HTTPCookiePropertyKey] = [.name, .value, .originURL, .version, - .domain, .path, .secure, .expires, - .comment, .commentURL, .discard, .maximumAge, - .port] + static let _attributes: [HTTPCookiePropertyKey] + = [.name, .value, .originURL, .version, .domain, + .path, .secure, .expires, .comment, .commentURL, + .discard, .maximumAge, .port] /// Initialize a NSHTTPCookie object with a dictionary of parameters /// @@ -268,7 +271,7 @@ open class HTTPCookie : NSObject { version = 0 } _version = version - + if let portString = properties[.port] as? String, _version == 1 { _portList = portString.characters .split(separator: ",") @@ -300,8 +303,7 @@ open class HTTPCookie : NSObject { } else { _expiresDate = nil } - - + if let discardString = properties[.discard] as? String { _sessionOnly = discardString == "TRUE" } else { @@ -321,23 +323,38 @@ open class HTTPCookie : NSObject { } } _HTTPOnly = false - _properties = [.comment : properties[.comment], - .commentURL : properties[.commentURL], - HTTPCookiePropertyKey(rawValue: "Created") : Date().timeIntervalSinceReferenceDate, // Cocoa Compatibility - .discard : _sessionOnly, - .domain : _domain, - .expires : _expiresDate, - .maximumAge : properties[.maximumAge], - .name : _name, - .originURL : properties[.originURL], - .path : _path, - .port : _portList, - .secure : _secure, - .value : _value, - .version : _version + + + _properties = [ + .created : Date().timeIntervalSinceReferenceDate, // Cocoa Compatibility + .discard : _sessionOnly, + .domain : _domain, + .name : _name, + .path : _path, + .secure : _secure, + .value : _value, + .version : _version ] + if let comment = properties[.comment] { + _properties[.comment] = comment + } + if let commentURL = properties[.commentURL] { + _properties[.commentURL] = commentURL + } + if let expires = properties[.expires] { + _properties[.expires] = expires + } + if let maximumAge = properties[.maximumAge] { + _properties[.maximumAge] = maximumAge + } + if let originURL = properties[.originURL] { + _properties[.originURL] = originURL + } + if let _portList = _portList { + _properties[.port] = _portList + } } - + /// Return a dictionary of header fields that can be used to add the /// specified cookies to the request. /// @@ -355,7 +372,7 @@ open class HTTPCookie : NSObject { } return ["Cookie": cookieString] } - + /// Return an array of cookies parsed from the specified response header fields and URL. /// /// This method will ignore irrelevant header fields so @@ -371,7 +388,7 @@ open class HTTPCookie : NSObject { // names and values. This implementation takes care of multiple cookies in the same field, however it doesn't //support commas and semicolons in names and values(except for dates) - guard let cookies: String = headerFields["Set-Cookie"] else { return [] } + guard let cookies: String = headerFields["Set-Cookie"] else { return [] } let nameValuePairs = cookies.components(separatedBy: ";") //split the name/value and attribute/value pairs .map({$0.trim()}) //trim whitespaces @@ -383,7 +400,7 @@ open class HTTPCookie : NSObject { //mark cookie boundaries in the name-value array var cookieIndices = (0..) -> HTTPCookie? { @@ -403,20 +420,20 @@ open class HTTPCookie : NSObject { let name = pair.components(separatedBy: "=")[0] var value = pair.components(separatedBy: "\(name)=")[1] //a value can have an "=" if canonicalize(name) == .expires { - value = value.insertComma(at: 3) //re-insert the comma + value = value.insertComma(at: 3) //re-insert the comma } properties[canonicalize(name)] = value } - + //if domain wasn't provided use the URL if properties[.domain] == nil { properties[.domain] = url.absoluteString } - + //the default Path is "/" if properties[.path] == nil { properties[.path] = "/" - } + } return HTTPCookie(properties: properties) } @@ -470,7 +487,7 @@ open class HTTPCookie : NSObject { open var properties: [HTTPCookiePropertyKey : Any]? { return _properties } - + /// The version of the receiver. /// /// Version 0 maps to "old-style" Netscape cookies. @@ -478,17 +495,17 @@ open class HTTPCookie : NSObject { open var version: Int { return _version } - + /// The name of the receiver. open var name: String { return _name } - + /// The value of the receiver. open var value: String { return _value } - + /// Returns The expires date of the receiver. /// /// The expires date is the date when the cookie should be @@ -497,7 +514,7 @@ open class HTTPCookie : NSObject { /*@NSCopying*/ open var expiresDate: Date? { return _expiresDate } - + /// Whether the receiver is session-only. /// /// `true` if this receiver should be discarded at the end of the @@ -506,7 +523,7 @@ open class HTTPCookie : NSObject { open var isSessionOnly: Bool { return _sessionOnly } - + /// The domain of the receiver. /// /// This value specifies URL domain to which the cookie @@ -535,7 +552,7 @@ open class HTTPCookie : NSObject { open var isSecure: Bool { return _secure } - + /// Whether the receiver should only be sent to HTTP servers per RFC 2965 /// /// Cookies may be marked as HTTPOnly by a server (or by a javascript). @@ -546,7 +563,7 @@ open class HTTPCookie : NSObject { open var isHTTPOnly: Bool { return _HTTPOnly } - + /// The comment of the receiver. /// /// This value specifies a string which is suitable for @@ -555,7 +572,7 @@ open class HTTPCookie : NSObject { open var comment: String? { return _comment } - + /// The comment URL of the receiver. /// /// This value specifies a URL which is suitable for @@ -564,7 +581,7 @@ open class HTTPCookie : NSObject { /*@NSCopying*/ open var commentURL: URL? { return _commentURL } - + /// The list ports to which the receiver should be sent. /// /// This value specifies an NSArray of NSNumbers diff --git a/Foundation/TimeZone.swift b/Foundation/TimeZone.swift index fd998df7b1..ecbbbc1aca 100644 --- a/Foundation/TimeZone.swift +++ b/Foundation/TimeZone.swift @@ -236,7 +236,7 @@ extension TimeZone : CustomStringConvertible, CustomDebugStringConvertible, Cust var c: [(label: String?, value: Any)] = [] c.append((label: "identifier", value: identifier)) c.append((label: "kind", value: _kindDescription)) - c.append((label: "abbreviation", value: abbreviation())) + c.append((label: "abbreviation", value: abbreviation() as Any)) c.append((label: "secondsFromGMT", value: secondsFromGMT())) c.append((label: "isDaylightSavingTime", value: isDaylightSavingTime())) return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct) diff --git a/Foundation/URLComponents.swift b/Foundation/URLComponents.swift index f828912cc2..c79e17b0c3 100644 --- a/Foundation/URLComponents.swift +++ b/Foundation/URLComponents.swift @@ -368,11 +368,11 @@ extension URLQueryItem : CustomStringConvertible, CustomDebugStringConvertible, public var debugDescription: String { return self.description } - + public var customMirror: Mirror { var c: [(label: String?, value: Any)] = [] c.append((label: "name", value: name)) - c.append((label: "value", value: value)) + c.append((label: "value", value: value as Any)) return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct) } } diff --git a/Foundation/URLRequest.swift b/Foundation/URLRequest.swift index 4f82aa77c7..f0c30c917d 100644 --- a/Foundation/URLRequest.swift +++ b/Foundation/URLRequest.swift @@ -238,23 +238,23 @@ extension URLRequest : CustomStringConvertible, CustomDebugStringConvertible, Cu return "url: nil" } } - + public var debugDescription: String { return self.description } - + public var customMirror: Mirror { var c: [(label: String?, value: Any)] = [] - c.append((label: "url", value: url)) + c.append((label: "url", value: url as Any)) c.append((label: "cachePolicy", value: cachePolicy.rawValue)) c.append((label: "timeoutInterval", value: timeoutInterval)) - c.append((label: "mainDocumentURL", value: mainDocumentURL)) + c.append((label: "mainDocumentURL", value: mainDocumentURL as Any)) c.append((label: "networkServiceType", value: networkServiceType)) c.append((label: "allowsCellularAccess", value: allowsCellularAccess)) - c.append((label: "httpMethod", value: httpMethod)) - c.append((label: "allHTTPHeaderFields", value: allHTTPHeaderFields)) - c.append((label: "httpBody", value: httpBody)) - c.append((label: "httpBodyStream", value: httpBodyStream)) + c.append((label: "httpMethod", value: httpMethod as Any)) + c.append((label: "allHTTPHeaderFields", value: allHTTPHeaderFields as Any)) + c.append((label: "httpBody", value: httpBody as Any)) + c.append((label: "httpBodyStream", value: httpBodyStream as Any)) c.append((label: "httpShouldHandleCookies", value: httpShouldHandleCookies)) c.append((label: "httpShouldUsePipelining", value: httpShouldUsePipelining)) return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct) @@ -265,21 +265,21 @@ extension URLRequest : _ObjectTypeBridgeable { public static func _getObjectiveCType() -> Any.Type { return NSURLRequest.self } - + @_semantics("convertToObjectiveC") public func _bridgeToObjectiveC() -> NSURLRequest { return _handle._copiedReference() } - + public static func _forceBridgeFromObjectiveC(_ input: NSURLRequest, result: inout URLRequest?) { result = URLRequest(_bridged: input) } - + public static func _conditionallyBridgeFromObjectiveC(_ input: NSURLRequest, result: inout URLRequest?) -> Bool { result = URLRequest(_bridged: input) return true } - + public static func _unconditionallyBridgeFromObjectiveC(_ source: NSURLRequest?) -> URLRequest { var result: URLRequest? = nil _forceBridgeFromObjectiveC(source!, result: &result) diff --git a/TestFoundation/TestNSHTTPCookie.swift b/TestFoundation/TestNSHTTPCookie.swift index 8ec0ba651a..6fd0e270f8 100644 --- a/TestFoundation/TestNSHTTPCookie.swift +++ b/TestFoundation/TestNSHTTPCookie.swift @@ -21,10 +21,10 @@ class TestNSHTTPCookie: XCTestCase { return [ ("test_BasicConstruction", test_BasicConstruction), ("test_RequestHeaderFields", test_RequestHeaderFields), - ("test_cookiesWithResponseHeader1cookie", test_cookiesWithResponseHeader1cookie), - ("test_cookiesWithResponseHeader0cookies", test_cookiesWithResponseHeader0cookies), - ("test_cookiesWithResponseHeader2cookies", test_cookiesWithResponseHeader2cookies), - ("test_cookiesWithResponseHeaderNoDomain", test_cookiesWithResponseHeaderNoDomain), + ("test_cookiesWithResponseHeader1cookie", test_cookiesWithResponseHeader1cookie), + ("test_cookiesWithResponseHeader0cookies", test_cookiesWithResponseHeader0cookies), + ("test_cookiesWithResponseHeader2cookies", test_cookiesWithResponseHeader2cookies), + ("test_cookiesWithResponseHeaderNoDomain", test_cookiesWithResponseHeaderNoDomain), ("test_cookiesWithResponseHeaderNoPathNoDomain", test_cookiesWithResponseHeaderNoPathNoDomain) ] } @@ -96,11 +96,11 @@ class TestNSHTTPCookie: XCTestCase { XCTAssert(versionZeroCookieWithInvalidVersionOneProps?.isSecure == true) XCTAssert(versionZeroCookieWithInvalidVersionOneProps?.version == 0) } - + func test_RequestHeaderFields() { let noCookies: [HTTPCookie] = [] XCTAssertEqual(HTTPCookie.requestHeaderFields(with: noCookies)["Cookie"], "") - + let basicCookies: [HTTPCookie] = [ HTTPCookie(properties: [ .name: "TestCookie1", @@ -115,15 +115,15 @@ class TestNSHTTPCookie: XCTestCase { .originURL: URL(string: "https://apple.com")! ])!, ] - + let basicCookieString = HTTPCookie.requestHeaderFields(with: basicCookies)["Cookie"] XCTAssertEqual(basicCookieString, "TestCookie1=testValue1; TestCookie2=testValue2") } func test_cookiesWithResponseHeader1cookie() { - let header = ["header1":"value1", - "Set-Cookie": "fr=anjd&232; Max-Age=7776000; path=/; domain=.example.com; secure; httponly", - "header2":"value2", + let header = ["header1":"value1", + "Set-Cookie": "fr=anjd&232; Max-Age=7776000; path=/; domain=.example.com; secure; httponly", + "header2":"value2", "header3":"value3"] let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, forURL: URL(string: "http://example.com")!) XCTAssertEqual(cookies.count, 1) @@ -138,9 +138,9 @@ class TestNSHTTPCookie: XCTestCase { } func test_cookiesWithResponseHeader2cookies() { - let header = ["header1":"value1", + let header = ["header1":"value1", "Set-Cookie": "fr=a&2@#; Max-Age=1186000; path=/; domain=.example.com; secure, xd=plm!@#;path=/;domain=.example2.com", - "header2":"value2", + "header2":"value2", "header3":"value3"] let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, forURL: URL(string: "http://example.com")!) XCTAssertEqual(cookies.count, 2) @@ -149,12 +149,14 @@ class TestNSHTTPCookie: XCTestCase { } func test_cookiesWithResponseHeaderNoDomain() { - let header = ["header1":"value1", - "Set-Cookie": "fr=anjd&232; expires=Wed, 21 Sep 2016 05:33:00 GMT; Max-Age=7776000; path=/; secure; httponly", - "header2":"value2", + let header = ["header1":"value1", + "Set-Cookie": "fr=anjd&232; expires=Wed, 21 Sep 2016 05:33:00 GMT; Max-Age=7776000; path=/; secure; httponly", + "header2":"value2", "header3":"value3"] let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, forURL: URL(string: "http://example.com")!) XCTAssertEqual(cookies[0].domain, "http://example.com") + XCTAssertNotNil(cookies[0].expiresDate) + let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss O" @@ -167,12 +169,12 @@ class TestNSHTTPCookie: XCTestCase { } func test_cookiesWithResponseHeaderNoPathNoDomain() { - let header = ["header1":"value1", + let header = ["header1":"value1", "Set-Cookie": "fr=tx; expires=Wed, 21-Sep-2016 05:33:00 GMT; Max-Age=7776000; secure; httponly", - "header2":"value2", + "header2":"value2", "header3":"value3"] let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, forURL: URL(string: "http://example.com")!) XCTAssertEqual(cookies[0].domain, "http://example.com") - XCTAssertEqual(cookies[0].path, "/") + XCTAssertEqual(cookies[0].path, "/") } } diff --git a/TestFoundation/TestNSTimeZone.swift b/TestFoundation/TestNSTimeZone.swift index 04037cbdbb..41ac367fee 100644 --- a/TestFoundation/TestNSTimeZone.swift +++ b/TestFoundation/TestNSTimeZone.swift @@ -38,6 +38,8 @@ class TestNSTimeZone: XCTestCase { ("test_localizedName", test_localizedName), // Also disabled due to https://bugs.swift.org/browse/SR-300 // ("test_systemTimeZoneUsesSystemTime", test_systemTimeZoneUsesSystemTime), + + ("test_customMirror", test_tz_customMirror), ] } @@ -152,7 +154,7 @@ class TestNSTimeZone: XCTestCase { let tz3 = TimeZone(identifier: "GMT-9999") XCTAssertNil(tz3) } - + func test_initializingTimeZoneWithAbbreviation() { // Test invalid timezone abbreviation var tz = TimeZone(abbreviation: "XXX") @@ -162,7 +164,7 @@ class TestNSTimeZone: XCTestCase { let expectedName = "America/Halifax" XCTAssertEqual(tz?.identifier, expectedName, "expected name \"\(expectedName)\" is not equal to \"\(tz?.identifier)\"") } - + func test_systemTimeZoneUsesSystemTime() { tzset() var t = time(nil) @@ -172,4 +174,20 @@ class TestNSTimeZone: XCTestCase { let expectedName = String(cString: lt.tm_zone, encoding: String.Encoding.ascii) ?? "Invalid Zone" XCTAssertEqual(zoneName, expectedName, "expected name \"\(expectedName)\" is not equal to \"\(zoneName)\"") } + + func test_tz_customMirror() { + let tz = TimeZone.current + let mirror = Mirror(reflecting: tz as TimeZone) + var children = [String : Any](minimumCapacity: Int(mirror.children.count)) + mirror.children.forEach { + if let label = $0.label { + children[label] = $0.value + } + } + + XCTAssertNotNil(children["identifier"]) + XCTAssertNotNil(children["kind"]) + XCTAssertNotNil(children["secondsFromGMT"]) + XCTAssertNotNil(children["isDaylightSavingTime"]) + } }