Răsfoiți Sursa

Opened the TaskDelegate queue to allow custom request extension operations.

Christian Noon 10 ani în urmă
părinte
comite
2401f6c92b
2 a modificat fișierele cu 68 adăugiri și 3 ștergeri
  1. 11 3
      Source/Request.swift
  2. 57 0
      Tests/RequestTests.swift

+ 11 - 3
Source/Request.swift

@@ -29,7 +29,8 @@ public class Request {
 
     // MARK: - Properties
 
-    let delegate: TaskDelegate
+    /// The delegate for the underlying task.
+    public let delegate: TaskDelegate
 
     /// The underlying task.
     public var task: NSURLSessionTask { return delegate.task }
@@ -218,9 +219,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

@@ -250,6 +250,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