Browse Source

Initial 2.3 migration for Source, Tests, and Example App

Kevin Harwood 9 years ago
parent
commit
c65e8450de

+ 14 - 0
Alamofire.xcodeproj/project.pbxproj

@@ -1242,6 +1242,7 @@
 				PRODUCT_NAME = Alamofire;
 				SDKROOT = appletvos;
 				SKIP_INSTALL = YES;
+				SWIFT_VERSION = 2.3;
 				TARGETED_DEVICE_FAMILY = 3;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 			};
@@ -1269,6 +1270,7 @@
 				PRODUCT_NAME = Alamofire;
 				SDKROOT = appletvos;
 				SKIP_INSTALL = YES;
+				SWIFT_VERSION = 2.3;
 				TARGETED_DEVICE_FAMILY = 3;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 			};
@@ -1286,6 +1288,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.Alamofire-tvOSTests";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = appletvos;
+				SWIFT_VERSION = 2.3;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 			};
 			name = Debug;
@@ -1303,6 +1306,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.Alamofire-tvOSTests";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = appletvos;
+				SWIFT_VERSION = 2.3;
 				TVOS_DEPLOYMENT_TARGET = 9.0;
 			};
 			name = Release;
@@ -1332,6 +1336,7 @@
 				PRODUCT_NAME = Alamofire;
 				SDKROOT = macosx;
 				SKIP_INSTALL = YES;
+				SWIFT_VERSION = 2.3;
 			};
 			name = Debug;
 		};
@@ -1358,6 +1363,7 @@
 				PRODUCT_NAME = Alamofire;
 				SDKROOT = macosx;
 				SKIP_INSTALL = YES;
+				SWIFT_VERSION = 2.3;
 			};
 			name = Release;
 		};
@@ -1385,6 +1391,7 @@
 				SDKROOT = watchos;
 				SKIP_INSTALL = YES;
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SWIFT_VERSION = 2.3;
 				TARGETED_DEVICE_FAMILY = 4;
 			};
 			name = Debug;
@@ -1412,6 +1419,7 @@
 				PRODUCT_NAME = Alamofire;
 				SDKROOT = watchos;
 				SKIP_INSTALL = YES;
+				SWIFT_VERSION = 2.3;
 				TARGETED_DEVICE_FAMILY = 4;
 			};
 			name = Release;
@@ -1529,6 +1537,7 @@
 				SKIP_INSTALL = YES;
 				SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Debug;
 		};
@@ -1553,6 +1562,7 @@
 				PRODUCT_NAME = Alamofire;
 				SKIP_INSTALL = YES;
 				SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Release;
 		};
@@ -1571,6 +1581,7 @@
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "$(TARGET_NAME)";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Debug;
 		};
@@ -1585,6 +1596,7 @@
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "$(TARGET_NAME)";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Release;
 		};
@@ -1608,6 +1620,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = macosx;
+				SWIFT_VERSION = 2.3;
 			};
 			name = Debug;
 		};
@@ -1628,6 +1641,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = macosx;
+				SWIFT_VERSION = 2.3;
 			};
 			name = Release;
 		};

+ 2 - 0
Example/iOS Example.xcodeproj/project.pbxproj

@@ -427,6 +427,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "iOS Example";
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Debug;
 		};
@@ -441,6 +442,7 @@
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
 				PRODUCT_BUNDLE_IDENTIFIER = "com.alamofire.$(PRODUCT_NAME:rfc1034identifier)";
 				PRODUCT_NAME = "iOS Example";
+				SWIFT_VERSION = 2.3;
 			};
 			name = Release;
 		};

+ 7 - 1
Source/Alamofire.swift

@@ -48,7 +48,13 @@ extension String: URLStringConvertible {
 }
 
 extension NSURL: URLStringConvertible {
-    public var URLString: String { return absoluteString }
+    public var URLString: String {
+        #if swift(>=2.3)
+        return absoluteString!
+        #else
+        return absoluteString
+        #endif
+    }
 }
 
 extension NSURLComponents: URLStringConvertible {

+ 4 - 0
Source/Download.swift

@@ -154,7 +154,11 @@ extension Request {
             let directoryURLs = NSFileManager.defaultManager().URLsForDirectory(directory, inDomains: domain)
 
             if !directoryURLs.isEmpty {
+                #if swift(>=2.3)
+                return directoryURLs[0].URLByAppendingPathComponent(response.suggestedFilename!)!
+                #else
                 return directoryURLs[0].URLByAppendingPathComponent(response.suggestedFilename!)
+                #endif
             }
 
             return temporaryURL

+ 4 - 0
Source/Request.swift

@@ -556,7 +556,11 @@ extension Request: CustomDebugStringConvertible {
             components.append("-d \"\(escapedBody)\"")
         }
 
+    #if swift(>=2.3)
+        components.append("\"\(URL.absoluteString!)\"")
+    #else
         components.append("\"\(URL.absoluteString)\"")
+    #endif
 
         return components.joinWithSeparator(" \\\n\t")
     }

+ 24 - 2
Source/ServerTrustPolicy.swift

@@ -181,13 +181,21 @@ public enum ServerTrustPolicy {
         switch self {
         case let .PerformDefaultEvaluation(validateHost):
             let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
-            SecTrustSetPolicies(serverTrust, [policy])
+            #if swift(>=2.3)
+                SecTrustSetPolicies(serverTrust, [policy!])
+            #else
+                SecTrustSetPolicies(serverTrust, [policy])
+            #endif
 
             serverTrustIsValid = trustIsValid(serverTrust)
         case let .PinCertificates(pinnedCertificates, validateCertificateChain, validateHost):
             if validateCertificateChain {
                 let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
+            #if swift(>=2.3)
+                SecTrustSetPolicies(serverTrust, [policy!])
+            #else
                 SecTrustSetPolicies(serverTrust, [policy])
+            #endif
 
                 SecTrustSetAnchorCertificates(serverTrust, pinnedCertificates)
                 SecTrustSetAnchorCertificatesOnly(serverTrust, true)
@@ -211,7 +219,11 @@ public enum ServerTrustPolicy {
 
             if validateCertificateChain {
                 let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
+            #if swift(>=2.3)
+                SecTrustSetPolicies(serverTrust, [policy!])
+            #else
                 SecTrustSetPolicies(serverTrust, [policy])
+            #endif
 
                 certificateChainEvaluationPassed = trustIsValid(serverTrust)
             }
@@ -239,13 +251,23 @@ public enum ServerTrustPolicy {
 
     private func trustIsValid(trust: SecTrust) -> Bool {
         var isValid = false
-
+    #if swift(>=2.3)
+        var result = SecTrustResultType(rawValue: SecTrustResultType.Invalid.rawValue)
+        let status = SecTrustEvaluate(trust, &result!)
+    #else
         var result = SecTrustResultType(kSecTrustResultInvalid)
         let status = SecTrustEvaluate(trust, &result)
+    #endif
 
         if status == errSecSuccess {
+        #if swift(>=2.3)
+            let unspecified = SecTrustResultType(rawValue: SecTrustResultType.Unspecified.rawValue)
+            let proceed = SecTrustResultType(rawValue: SecTrustResultType.Proceed.rawValue)
+        #else
             let unspecified = SecTrustResultType(kSecTrustResultUnspecified)
             let proceed = SecTrustResultType(kSecTrustResultProceed)
+        #endif
+
 
             isValid = result == unspecified || result == proceed
         }

+ 7 - 1
Source/Upload.swift

@@ -310,14 +310,20 @@ extension Manager {
             } else {
                 let fileManager = NSFileManager.defaultManager()
                 let tempDirectoryURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
-                let directoryURL = tempDirectoryURL.URLByAppendingPathComponent("com.alamofire.manager/multipart.form.data")
                 let fileName = NSUUID().UUIDString
+            #if swift(>=2.3)
+                let directoryURL = tempDirectoryURL.URLByAppendingPathComponent("com.alamofire.manager/multipart.form.data")!
+                let fileURL = directoryURL.URLByAppendingPathComponent(fileName)!
+            #else
+                let directoryURL = tempDirectoryURL.URLByAppendingPathComponent("com.alamofire.manager/multipart.form.data")
                 let fileURL = directoryURL.URLByAppendingPathComponent(fileName)
+            #endif
 
                 do {
                     try fileManager.createDirectoryAtURL(directoryURL, withIntermediateDirectories: true, attributes: nil)
                     try formData.writeEncodedDataToDisk(fileURL)
 
+
                     dispatch_async(dispatch_get_main_queue()) {
                         let encodingResult = MultipartFormDataEncodingResult.Success(
                             request: self.upload(URLRequestWithContentType, file: fileURL),

+ 8 - 0
Tests/DownloadTests.swift

@@ -79,7 +79,11 @@ class DownloadResponseTestCase: BaseTestCase {
     }()
 
     var randomCachesFileURL: NSURL {
+    #if swift(>=2.3)
+        return cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json")!
+    #else
         return cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json")
+    #endif
     }
 
     func testDownloadRequest() {
@@ -169,7 +173,11 @@ class DownloadResponseTestCase: BaseTestCase {
         let fileManager = NSFileManager.defaultManager()
         let directory = fileManager.URLsForDirectory(searchPathDirectory, inDomains: self.searchPathDomain)[0]
         let filename = "test_download_data"
+    #if swift(>=2.3)
+        let fileURL = directory.URLByAppendingPathComponent(filename)!
+    #else
         let fileURL = directory.URLByAppendingPathComponent(filename)
+    #endif
 
         let expectation = expectationWithDescription("Bytes download progress should be reported: \(URLString)")
 

+ 6 - 1
Tests/ManagerTests.swift

@@ -262,10 +262,15 @@ class ManagerConfigurationHeadersTestCase: BaseTestCase {
         // Given, When, Then
         executeAuthorizationHeaderTestForConfigurationType(.Ephemeral)
     }
-
     func testThatBackgroundConfigurationHeadersAreSentWithRequest() {
         // Given, When, Then
+    #if swift(>=2.3)
+        // ⚠️⚠️ rdar://26870455
+        // Will be enabled once rdar is resolved
+        print("⚠️⚠️ Skipping \(#function) due to rdar://26870455 ⚠️⚠️")
+    #else
         executeAuthorizationHeaderTestForConfigurationType(.Background)
+    #endif
     }
 
     private func executeAuthorizationHeaderTestForConfigurationType(type: ConfigurationType) {

+ 8 - 0
Tests/MultipartFormDataTests.swift

@@ -60,7 +60,11 @@ struct BoundaryGenerator {
 
 private func temporaryFileURL() -> NSURL {
     let tempDirectoryURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
+#if swift(>=2.3)
+    let directoryURL = tempDirectoryURL.URLByAppendingPathComponent("com.alamofire.test/multipart.form.data")!
+#else
     let directoryURL = tempDirectoryURL.URLByAppendingPathComponent("com.alamofire.test/multipart.form.data")
+#endif
 
     let fileManager = NSFileManager.defaultManager()
     do {
@@ -70,7 +74,11 @@ private func temporaryFileURL() -> NSURL {
     }
 
     let fileName = NSUUID().UUIDString
+#if swift(>=2.3)
+    let fileURL = directoryURL.URLByAppendingPathComponent(fileName)!
+#else
     let fileURL = directoryURL.URLByAppendingPathComponent(fileName)
+#endif
 
     return fileURL
 }

+ 66 - 1
Tests/ServerTrustPolicyTests.swift

@@ -204,13 +204,22 @@ class ServerTrustPolicyTestCase: BaseTestCase {
 
     func trustIsValid(trust: SecTrust) -> Bool {
         var isValid = false
-
+    #if swift (>=2.3)
+        var result = SecTrustResultType(rawValue: SecTrustResultType.Invalid.rawValue)
+        let status = SecTrustEvaluate(trust, &result!)
+    #else
         var result = SecTrustResultType(kSecTrustResultInvalid)
         let status = SecTrustEvaluate(trust, &result)
+    #endif
 
         if status == errSecSuccess {
+        #if swift (>=2.3)
+            let unspecified = SecTrustResultType(rawValue: SecTrustResultType.Unspecified.rawValue)
+            let proceed = SecTrustResultType(rawValue: SecTrustResultType.Proceed.rawValue)
+        #else
             let unspecified = SecTrustResultType(kSecTrustResultUnspecified)
             let proceed = SecTrustResultType(kSecTrustResultProceed)
+        #endif
 
             isValid = result == unspecified || result == proceed
         }
@@ -233,7 +242,11 @@ class ServerTrustPolicyExplorationBasicX509PolicyValidationTestCase: ServerTrust
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift (>=2.3)
+        let policies = [SecPolicyCreateBasicX509()!]
+    #else
         let policies = [SecPolicyCreateBasicX509()]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -246,7 +259,11 @@ class ServerTrustPolicyExplorationBasicX509PolicyValidationTestCase: ServerTrust
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift (>=2.3)
+        let policies = [SecPolicyCreateBasicX509()!]
+    #else
         let policies = [SecPolicyCreateBasicX509()]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -259,7 +276,11 @@ class ServerTrustPolicyExplorationBasicX509PolicyValidationTestCase: ServerTrust
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift (>=2.3)
+        let policies = [SecPolicyCreateBasicX509()!]
+    #else
         let policies = [SecPolicyCreateBasicX509()]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -272,7 +293,11 @@ class ServerTrustPolicyExplorationBasicX509PolicyValidationTestCase: ServerTrust
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift (>=2.3)
+        let policies = [SecPolicyCreateBasicX509()!]
+    #else
         let policies = [SecPolicyCreateBasicX509()]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -294,7 +319,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -307,7 +336,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -320,7 +353,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -333,7 +370,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -346,7 +387,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -359,7 +404,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -372,11 +421,19 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [
+            SecPolicyCreateSSL(true, "test.alamofire.org")!,
+            SecPolicyCreateSSL(true, "blog.alamofire.org")!,
+            SecPolicyCreateSSL(true, "www.alamofire.org")!
+        ]
+    #else
         let policies = [
             SecPolicyCreateSSL(true, "test.alamofire.org"),
             SecPolicyCreateSSL(true, "blog.alamofire.org"),
             SecPolicyCreateSSL(true, "www.alamofire.org")
         ]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -389,7 +446,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, nil)!]
+    #else
         let policies = [SecPolicyCreateSSL(true, nil)]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then
@@ -402,7 +463,11 @@ class ServerTrustPolicyExplorationSSLPolicyValidationTestCase: ServerTrustPolicy
         setRootCertificateAsLoneAnchorCertificateForTrust(trust)
 
         // When
+    #if swift(>=2.3)
+        let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")!]
+    #else
         let policies = [SecPolicyCreateSSL(true, "test.alamofire.org")]
+    #endif
         SecTrustSetPolicies(trust, policies)
 
         // Then

+ 3 - 3
Tests/SessionDelegateTests.swift

@@ -131,7 +131,7 @@ class SessionDelegateTestCase: BaseTestCase {
 
     func testThatRequestWillPerformHTTPRedirectionByDefault() {
         // Given
-        let redirectURLString = "https://www.apple.com"
+        let redirectURLString = "https://www.apple.com/"
         let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
 
         let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
@@ -201,7 +201,7 @@ class SessionDelegateTestCase: BaseTestCase {
 
     func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
         // Given
-        let redirectURLString = "https://www.apple.com"
+        let redirectURLString = "https://www.apple.com/"
         let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
 
         let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
@@ -243,7 +243,7 @@ class SessionDelegateTestCase: BaseTestCase {
 
     func testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection() {
         // Given
-        let redirectURLString = "https://www.apple.com"
+        let redirectURLString = "https://www.apple.com/"
         let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
 
         let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")

+ 6 - 0
Tests/UploadTests.swift

@@ -586,6 +586,11 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
     }
 
     func testThatUploadingMultipartFormDataOnBackgroundSessionWritesDataToFileToAvoidCrash() {
+    #if swift(>=2.3)
+        // ⚠️⚠️ rdar://26870455
+        // Will be enabled once rdar is resolved
+        print("⚠️⚠️ Skipping \(#function) due to rdar://26870455 ⚠️⚠️")
+    #else
         // Given
         let manager: Manager = {
             let identifier = "com.alamofire.uploadtests.\(NSUUID().UUIDString)"
@@ -646,6 +651,7 @@ class UploadMultipartFormDataTestCase: BaseTestCase {
         } else {
             XCTFail("streaming from disk should not be nil")
         }
+    #endif
     }
 
     // MARK: Combined Test Execution