Skip to content

Commit

Permalink
Merge pull request Alamofire#507 from Alamofire/test/httpbin_redirect…
Browse files Browse the repository at this point in the history
…_tests

Added more robust redirect tests and migrated to httpbin
  • Loading branch information
cnoon committed Jun 3, 2015
2 parents 0b8f207 + af97960 commit 57780a2
Showing 1 changed file with 128 additions and 17 deletions.
145 changes: 128 additions & 17 deletions Tests/ResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,88 @@ class JSONResponseTestCase: BaseTestCase {
// MARK: -

class RedirectResponseTestCase: BaseTestCase {
func testGETRequestRedirectResponse() {
func testThatRequestWillPerformHTTPRedirectionByDefault() {
// Given
let URLString = "http://google.com"
let redirectURLString = "http://www.apple.com"
let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"

let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: AnyObject?
var error: NSError?

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

expectation.fulfill()
}

waitForExpectationsWithTimeout(self.defaultTimeout, 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 = "http://httpbin.org/get"
let URLString = "http://httpbin.org/redirect/5"

let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: AnyObject?
var error: NSError?

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

expectation.fulfill()
}

waitForExpectationsWithTimeout(self.defaultTimeout, 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 = "http://www.apple.com"
let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"

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

delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
// Accept the redirect by returning the updated request.
delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
return request
}

let expectation = expectationWithDescription("\(URLString)")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: AnyObject?
Expand All @@ -137,23 +207,63 @@ class RedirectResponseTestCase: BaseTestCase {
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")

XCTAssertEqual(response?.URL ?? NSURL(), NSURL(string: "http://www.google.com/")!, "request should have followed a redirect")
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 testGETRequestDisallowRedirectResponse() {
func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
// Given
let URLString = "http://google.com/"
let redirectURLString = "http://www.apple.com"
let URLString = "http://httpbin.org/redirect-to?url=\(redirectURLString)"

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

delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
// Disallow redirects by returning nil.
// NOTE: 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: URLString)!)
delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
return nil
}

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: AnyObject?
var error: NSError?

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

expectation.fulfill()
}

let expectation = expectationWithDescription("\(URLString)")
waitForExpectationsWithTimeout(self.defaultTimeout, 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 redirectURLString = "http://httpbin.org/get"
let URLString = "http://httpbin.org/redirect/5"

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

delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
++totalRedirectCount
return request
}

var request: NSURLRequest?
var response: NSHTTPURLResponse?
Expand All @@ -179,7 +289,8 @@ class RedirectResponseTestCase: BaseTestCase {
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")

XCTAssertEqual(response?.URL ?? NSURL(string: "")!, NSURL(string: URLString)!, "request should not have followed a redirect")
XCTAssertEqual(response?.statusCode ?? -1, 301, "response should have a 301 status code")
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")
}
}

0 comments on commit 57780a2

Please sign in to comment.