Skip to content

Commit

Permalink
Added tests for the redirect with completion.
Browse files Browse the repository at this point in the history
These are currently failing
  • Loading branch information
kcharwood committed Mar 25, 2016
1 parent fad7390 commit 3ea5db0
Showing 1 changed file with 145 additions and 13 deletions.
158 changes: 145 additions & 13 deletions Tests/ResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,15 @@ 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()
Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil
}

// MARK: Tests
Expand All @@ -292,7 +295,7 @@ class RedirectResponseTestCase: BaseTestCase {
var error: NSError?

// When
Alamofire.request(.GET, URLString)
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
Expand Down Expand Up @@ -327,7 +330,7 @@ class RedirectResponseTestCase: BaseTestCase {
var error: NSError?

// When
Alamofire.request(.GET, URLString)
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
Expand Down Expand Up @@ -355,9 +358,11 @@ class RedirectResponseTestCase: BaseTestCase {
let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"

let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
let callbackExpectation = expectationWithDescription("Redirect callback should be made")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate

delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
callbackExpectation.fulfill()
return request
}

Expand All @@ -367,7 +372,7 @@ class RedirectResponseTestCase: BaseTestCase {
var error: NSError?

// When
Alamofire.request(.GET, URLString)
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
Expand All @@ -389,15 +394,101 @@ class RedirectResponseTestCase: BaseTestCase {
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 delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
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
}

Expand All @@ -407,15 +498,15 @@ class RedirectResponseTestCase: BaseTestCase {
var error: NSError?

// When
Alamofire.request(.GET, URLString)
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError

expectation.fulfill()
}
}

waitForExpectationsWithTimeout(timeout, handler: nil)

Expand All @@ -435,7 +526,7 @@ class RedirectResponseTestCase: BaseTestCase {
let URLString = "https://httpbin.org/redirect/5"

let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var totalRedirectCount = 0

delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
Expand All @@ -449,7 +540,7 @@ class RedirectResponseTestCase: BaseTestCase {
var error: NSError?

// When
Alamofire.request(.GET, URLString)
manager.request(.GET, URLString)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
Expand All @@ -472,6 +563,49 @@ class RedirectResponseTestCase: BaseTestCase {
XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
}

func testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects() {
// Given
let redirectURLString = "https://httpbin.org/get"
let URLString = "https://httpbin.org/redirect/5"

let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
var totalRedirectCount = 0

delegate.taskWillPerformHTTPRedirectionWithCompletion = {_, _, _, request, completion in
totalRedirectCount += 1
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")
XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
}

func testThatRedirectedRequestContainsAllHeadersFromOriginalRequest() {
// Given
let redirectURLString = "https://httpbin.org/get"
Expand All @@ -485,8 +619,6 @@ class RedirectResponseTestCase: BaseTestCase {
// 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.

let manager = Manager(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())

manager.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
var redirectedRequest = request

Expand Down

0 comments on commit 3ea5db0

Please sign in to comment.