Browse Source

Merge pull request #424 from jhersh/redirect-tests

[Tests] Added redirect sessiondelegate tests
Mattt Thompson 10 years ago
parent
commit
81f81fb9d0
1 changed files with 59 additions and 0 deletions
  1. 59 0
      Tests/ResponseTests.swift

+ 59 - 0
Tests/ResponseTests.swift

@@ -67,3 +67,62 @@ class AlamofireJSONResponseTestCase: XCTestCase {
         }
     }
 }
+
+class AlamofireRedirectResponseTestCase: XCTestCase {
+    func testGETRequestRedirectResponse() {
+        let URL = "http://google.com"
+        let expectation = expectationWithDescription("\(URL)")
+        
+        let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
+        
+        delegate.taskWillPerformHTTPRedirection = { (session: NSURLSession!, task: NSURLSessionTask!, response: NSHTTPURLResponse!, request: NSURLRequest!) in
+            // Accept the redirect by returning the updated request.
+            return request
+        }
+        
+        Alamofire.request(.GET, URL)
+            .response { (request, response, data, error) in
+                expectation.fulfill()
+                XCTAssertNotNil(request, "request should not be nil")
+                XCTAssertNotNil(response, "response should not be nil")
+                XCTAssertNotNil(data, "data should not be nil")
+                XCTAssertNil(error, "error should be nil")
+                
+                XCTAssertEqual(response!.URL!, NSURL(string: "http://www.google.com/")!, "request should have followed a redirect")
+        }
+        
+        waitForExpectationsWithTimeout(10) { (error) in
+            XCTAssertNil(error, "\(error)")
+        }
+    }
+    
+    func testGETRequestDisallowRedirectResponse() {
+        let URL = "http://google.com/"
+        let expectation = expectationWithDescription("\(URL)")
+        
+        let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
+        
+        delegate.taskWillPerformHTTPRedirection = { (session: NSURLSession!, task: NSURLSessionTask!, response: NSHTTPURLResponse!, request: NSURLRequest!) in
+            // Disallow redirects by returning nil.
+            // TODO: NSURLSessionDelegate's URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
+            // suggests that returning nil should refuse the redirect, but this causes a deadlock/timeout
+            
+            return NSURLRequest(URL: NSURL(string: URL)!)
+        }
+        
+        Alamofire.request(.GET, URL)
+            .response { (request, response, data, error) in
+                expectation.fulfill()
+                XCTAssertNotNil(request, "request should not be nil")
+                XCTAssertNotNil(response, "response should not be nil")
+                XCTAssertNotNil(data, "data should not be nil")
+                XCTAssertNil(error, "error should be nil")
+                
+                XCTAssertEqual(response!.URL!, NSURL(string: URL)!, "request should not have followed a redirect")
+        }
+        
+        waitForExpectationsWithTimeout(10) { (error) in
+            XCTAssertNil(error, "\(error)")
+        }
+    }
+}