瀏覽代碼

Merge pull request #590 from Alamofire/feature/opening_up_request

Christian Noon 10 年之前
父節點
當前提交
cc878b87ce
共有 3 個文件被更改,包括 69 次插入25 次删除
  1. 0 21
      Source/Manager.swift
  2. 12 4
      Source/Request.swift
  3. 57 0
      Tests/RequestTests.swift

+ 0 - 21
Source/Manager.swift

@@ -411,26 +411,5 @@ public class Manager {
                 delegate.URLSession(session, downloadTask: downloadTask, didResumeAtOffset: fileOffset, expectedTotalBytes: expectedTotalBytes)
             }
         }
-
-        // MARK: - NSObject
-
-        public override func respondsToSelector(selector: Selector) -> Bool {
-            switch selector {
-            case "URLSession:didBecomeInvalidWithError:":
-                return (self.sessionDidBecomeInvalidWithError != nil)
-            case "URLSession:didReceiveChallenge:completionHandler:":
-                return (self.sessionDidReceiveChallenge != nil)
-            case "URLSessionDidFinishEventsForBackgroundURLSession:":
-                return (self.sessionDidFinishEventsForBackgroundURLSession != nil)
-            case "URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:":
-                return (self.taskWillPerformHTTPRedirection != nil)
-            case "URLSession:dataTask:didReceiveResponse:completionHandler:":
-                return (self.dataTaskDidReceiveResponse != nil)
-            case "URLSession:dataTask:willCacheResponse:completionHandler:":
-                return (self.dataTaskWillCacheResponse != nil)
-            default:
-                return self.dynamicType.instancesRespondToSelector(selector)
-            }
-        }
     }
 }

+ 12 - 4
Source/Request.swift

@@ -29,6 +29,9 @@ public class Request {
 
     // MARK: - Properties
 
+    /// The delegate for the underlying task.
+    public let delegate: TaskDelegate
+
     /// The underlying task.
     public var task: NSURLSessionTask { return delegate.task }
 
@@ -44,8 +47,6 @@ public class Request {
     /// The progress of the request lifecycle.
     public var progress: NSProgress { return delegate.progress }
 
-    let delegate: TaskDelegate
-
     // MARK: - Lifecycle
 
     init(session: NSURLSession, task: NSURLSessionTask) {
@@ -168,9 +169,16 @@ public class Request {
 
     // MARK: - TaskDelegate
 
-    class TaskDelegate: NSObject, NSURLSessionTaskDelegate {
+    /**
+        The task delegate is responsible for handling all delegate callbacks for the underlying task as well as 
+        executing all operations attached to the serial operation queue upon task completion.
+    */
+    public class TaskDelegate: NSObject {
+
+        /// The serial operation queue used to execute all operations after the task completes.
+        public let queue: NSOperationQueue
+
         let task: NSURLSessionTask
-        let queue: NSOperationQueue
         let progress: NSProgress
 
         var data: NSData? { return nil }

+ 57 - 0
Tests/RequestTests.swift

@@ -252,6 +252,63 @@ class RequestResponseTestCase: BaseTestCase {
 
 // MARK: -
 
+extension Request {
+    private func preValidate(operation: Void -> Void) -> Self {
+        self.delegate.queue.addOperationWithBlock {
+            operation()
+        }
+
+        return self
+    }
+
+    private func postValidate(operation: Void -> Void) -> Self {
+        self.delegate.queue.addOperationWithBlock {
+            operation()
+        }
+
+        return self
+    }
+}
+
+// MARK: -
+
+class RequestExtensionTestCase: BaseTestCase {
+    func testThatRequestExtensionHasAccessToTaskDelegateQueue() {
+        // Given
+        let URLString = "http://httpbin.org/get"
+        let expectation = expectationWithDescription("GET request should succeed: \(URLString)")
+
+        var responses: [String] = []
+
+        // When
+        Alamofire.request(.GET, URLString)
+            .preValidate {
+                responses.append("preValidate")
+            }
+            .validate()
+            .postValidate {
+                responses.append("postValidate")
+            }
+            .response { _, _, _, _ in
+                responses.append("response")
+                expectation.fulfill()
+        }
+
+        waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
+
+        // Then
+        if responses.count == 3 {
+            XCTAssertEqual(responses[0], "preValidate", "response at index 0 should be preValidate")
+            XCTAssertEqual(responses[1], "postValidate", "response at index 1 should be postValidate")
+            XCTAssertEqual(responses[2], "response", "response at index 2 should be response")
+        } else {
+            XCTFail("responses count should be equal to 3")
+        }
+    }
+}
+
+// MARK: -
+
 class RequestDescriptionTestCase: BaseTestCase {
     func testRequestDescription() {
         // Given