提交 ca4a8a51 编写于 作者: C Christian Noon

Moved the redirect tests into a new SessionDelegateTests file.

上级 8ea36fcc
......@@ -117,6 +117,9 @@
4C83F41B1B749E0E00203445 /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C83F41A1B749E0E00203445 /* Stream.swift */; };
4C83F41C1B749E0E00203445 /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C83F41A1B749E0E00203445 /* Stream.swift */; };
4C83F41D1B749E0E00203445 /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C83F41A1B749E0E00203445 /* Stream.swift */; };
4C9DCE781CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9DCE771CB1BCE2003E6463 /* SessionDelegateTests.swift */; };
4C9DCE791CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9DCE771CB1BCE2003E6463 /* SessionDelegateTests.swift */; };
4C9DCE7A1CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9DCE771CB1BCE2003E6463 /* SessionDelegateTests.swift */; };
4CA028C51B7466C500C84163 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA028C41B7466C500C84163 /* ResultTests.swift */; };
4CA028C61B7466C500C84163 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA028C41B7466C500C84163 /* ResultTests.swift */; };
4CB928291C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
......@@ -272,6 +275,7 @@
4C812C5F1B535F6D0017E0BF /* root-ca-disig.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = "root-ca-disig.cer"; path = "disig.sk/root-ca-disig.cer"; sourceTree = "<group>"; };
4C812C601B535F6D0017E0BF /* testssl-expire.disig.sk.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = "testssl-expire.disig.sk.cer"; path = "disig.sk/testssl-expire.disig.sk.cer"; sourceTree = "<group>"; };
4C83F41A1B749E0E00203445 /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stream.swift; sourceTree = "<group>"; };
4C9DCE771CB1BCE2003E6463 /* SessionDelegateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SessionDelegateTests.swift; sourceTree = "<group>"; };
4CA028C41B7466C500C84163 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = "<group>"; };
4CB928281C66BFBC00CE5F08 /* Notifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
4CCFA7991B2BE71600B6F460 /* URLProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLProtocolTests.swift; sourceTree = "<group>"; };
......@@ -377,6 +381,7 @@
F8111E5D19A9674D0040E7D1 /* RequestTests.swift */,
F8111E5E19A9674D0040E7D1 /* ResponseTests.swift */,
4CA028C41B7466C500C84163 /* ResultTests.swift */,
4C9DCE771CB1BCE2003E6463 /* SessionDelegateTests.swift */,
);
name = Core;
sourceTree = "<group>";
......@@ -989,6 +994,7 @@
4CF627181BA7CC240011A099 /* RequestTests.swift in Sources */,
4CF627211BA7CC240011A099 /* TLSEvaluationTests.swift in Sources */,
4CF627221BA7CC240011A099 /* UploadTests.swift in Sources */,
4C9DCE7A1CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */,
4C80F9F91BB730F6001B46D2 /* String+AlamofireTests.swift in Sources */,
4CF6271E1BA7CC240011A099 /* MultipartFormDataTests.swift in Sources */,
4CF627201BA7CC240011A099 /* ServerTrustPolicyTests.swift in Sources */,
......@@ -1087,6 +1093,7 @@
4C3238E71B3604DB00FE04AE /* MultipartFormDataTests.swift in Sources */,
4C33A1431B52089C00873DFF /* ServerTrustPolicyTests.swift in Sources */,
4C341BBA1B1A865A00C1B34D /* CacheTests.swift in Sources */,
4C9DCE781CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */,
4C4CBE7B1BAF700C0024D659 /* String+AlamofireTests.swift in Sources */,
4CA028C51B7466C500C84163 /* ResultTests.swift in Sources */,
4CCFA79A1B2BE71600B6F460 /* URLProtocolTests.swift in Sources */,
......@@ -1113,6 +1120,7 @@
4C3238E81B3604DB00FE04AE /* MultipartFormDataTests.swift in Sources */,
4C33A1441B52089C00873DFF /* ServerTrustPolicyTests.swift in Sources */,
4C341BBB1B1A865A00C1B34D /* CacheTests.swift in Sources */,
4C9DCE791CB1BCE2003E6463 /* SessionDelegateTests.swift in Sources */,
4C4CBE7C1BAF700C0024D659 /* String+AlamofireTests.swift in Sources */,
4CA028C61B7466C500C84163 /* ResultTests.swift in Sources */,
4CCFA79B1B2BE71600B6F460 /* URLProtocolTests.swift in Sources */,
......
......@@ -265,421 +265,3 @@ class ResponseJSONTestCase: BaseTestCase {
}
}
}
// MARK: -
class RedirectResponseTestCase: BaseTestCase {
var manager: Alamofire.Manager!
// MARK: Setup and Teardown
override func setUp() {
super.setUp()
manager = Alamofire.Manager(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
}
override func tearDown() {
super.tearDown()
}
// MARK: Tests
func testThatRequestWillPerformHTTPRedirectionByDefault() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
// Given
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/5"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
callbackExpectation.fulfill()
return request
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, request, completion in
completion(request)
callbackExpectation.fulfill()
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, _, completion in
callbackExpectation.fulfill()
completion(nil)
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", URLString, "response URL should match the origin URL")
XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
}
func testThatTaskOverrideClosureWithCompletionCanCancelHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
callbackExpectation.fulfill()
return nil
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", URLString, "response URL should match the origin URL")
XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
}
func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
// Given
let redirectCount = 5
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/\(redirectCount)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var redirectExpectations = [XCTestExpectation]()
for index in 0..<redirectCount {
redirectExpectations.insert(expectationWithDescription("Redirect #\(index) callback was received"), atIndex: 0)
}
delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
if let redirectExpectation = redirectExpectations.popLast() {
redirectExpectation.fulfill()
} else {
XCTFail("Too many redirect callbacks were received")
}
return request
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects() {
// Given
let redirectCount = 5
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/\(redirectCount)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var redirectExpectations = [XCTestExpectation]()
for index in 0..<redirectCount {
redirectExpectations.insert(expectationWithDescription("Redirect #\(index) callback was received"), atIndex: 0)
}
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, request, completion in
if let redirectExpectation = redirectExpectations.popLast() {
redirectExpectation.fulfill()
} else {
XCTFail("Too many redirect callbacks were received")
}
completion(request)
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatRedirectedRequestContainsAllHeadersFromOriginalRequest() {
// Given
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let headers = [
"Authorization": "1234",
"Custom-Header": "foobar",
]
// NOTE: It appears that most headers are maintained during a redirect with the exception of the `Authorization`
// header. It appears that Apple's strips the `Authorization` header from the redirected URL request. If you
// need to maintain the `Authorization` header, you need to manually append it to the redirected request.
manager.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
var redirectedRequest = request
if let
originalRequest = task.originalRequest,
headers = originalRequest.allHTTPHeaderFields,
authorizationHeaderValue = headers["Authorization"]
{
let mutableRequest = request.mutableCopy() as! NSMutableURLRequest
mutableRequest.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
redirectedRequest = mutableRequest
}
return redirectedRequest
}
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var response: Response<AnyObject, NSError>?
// When
manager.request(.GET, URLString, headers: headers)
.responseJSON { closureResponse in
response = closureResponse
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
XCTAssertNotNil(response?.request, "request should not be nil")
XCTAssertNotNil(response?.response, "response should not be nil")
XCTAssertNotNil(response?.data, "data should not be nil")
XCTAssertTrue(response?.result.isSuccess ?? false, "response result should be a success")
if let
JSON = response?.result.value as? [String: AnyObject],
headers = JSON["headers"] as? [String: String]
{
XCTAssertEqual(headers["Custom-Header"], "foobar", "Custom-Header should be equal to foobar")
XCTAssertEqual(headers["Authorization"], "1234", "Authorization header should be equal to 1234")
}
}
}
// SessionDelegateTests.swift
//
// Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
@testable import Alamofire
import Foundation
import XCTest
class SessionDelegateTestCase: BaseTestCase {
var manager: Alamofire.Manager!
// MARK: - Setup and Teardown
override func setUp() {
super.setUp()
manager = Alamofire.Manager(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
}
override func tearDown() {
super.tearDown()
}
// MARK: - Tests - Redirects
func testThatRequestWillPerformHTTPRedirectionByDefault() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
// Given
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/5"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
callbackExpectation.fulfill()
return request
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, request, completion in
completion(request)
callbackExpectation.fulfill()
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, _, completion in
callbackExpectation.fulfill()
completion(nil)
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", URLString, "response URL should match the origin URL")
XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
}
func testThatTaskOverrideClosureWithCompletionCanCancelHTTPRedirection() {
// Given
let redirectURLString = "https://www.apple.com"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
callbackExpectation.fulfill()
return nil
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", URLString, "response URL should match the origin URL")
XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
}
func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
// Given
let redirectCount = 5
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/\(redirectCount)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var redirectExpectations = [XCTestExpectation]()
for index in 0..<redirectCount {
redirectExpectations.insert(expectationWithDescription("Redirect #\(index) callback was received"), atIndex: 0)
}
delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
if let redirectExpectation = redirectExpectations.popLast() {
redirectExpectation.fulfill()
} else {
XCTFail("Too many redirect callbacks were received")
}
return request
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects() {
// Given
let redirectCount = 5
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/\(redirectCount)"
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var redirectExpectations = [XCTestExpectation]()
for index in 0..<redirectCount {
redirectExpectations.insert(expectationWithDescription("Redirect #\(index) callback was received"), atIndex: 0)
}
delegate.taskWillPerformHTTPRedirectionWithCompletion = { _, _, _, request, completion in
if let redirectExpectation = redirectExpectations.popLast() {
redirectExpectation.fulfill()
} else {
XCTFail("Too many redirect callbacks were received")
}
completion(request)
}
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?
// When
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
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?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
}
func testThatRedirectedRequestContainsAllHeadersFromOriginalRequest() {
// Given
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
let headers = [
"Authorization": "1234",
"Custom-Header": "foobar",
]
// NOTE: It appears that most headers are maintained during a redirect with the exception of the `Authorization`
// header. It appears that Apple's strips the `Authorization` header from the redirected URL request. If you
// need to maintain the `Authorization` header, you need to manually append it to the redirected request.
manager.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
var redirectedRequest = request
if let
originalRequest = task.originalRequest,
headers = originalRequest.allHTTPHeaderFields,
authorizationHeaderValue = headers["Authorization"]
{
let mutableRequest = request.mutableCopy() as! NSMutableURLRequest
mutableRequest.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
redirectedRequest = mutableRequest
}
return redirectedRequest
}
let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
var response: Response<AnyObject, NSError>?
// When
manager.request(.GET, URLString, headers: headers)
.responseJSON { closureResponse in
response = closureResponse
expectation.fulfill()
}
waitForExpectationsWithTimeout(timeout, handler: nil)
// Then
XCTAssertNotNil(response?.request, "request should not be nil")
XCTAssertNotNil(response?.response, "response should not be nil")
XCTAssertNotNil(response?.data, "data should not be nil")
XCTAssertTrue(response?.result.isSuccess ?? false, "response result should be a success")
if let
JSON = response?.result.value as? [String: AnyObject],
headers = JSON["headers"] as? [String: String]
{
XCTAssertEqual(headers["Custom-Header"], "foobar", "Custom-Header should be equal to foobar")
XCTAssertEqual(headers["Authorization"], "1234", "Authorization header should be equal to 1234")
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册