Skip to content

Better descriptions for XCTestExpectations in TestURLSession #1111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions TestFoundation/TestNSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class TestURLSession : XCTestCase {
func test_dataTaskWithURL() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "data task"))
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
d.run(with: url)
waitForExpectations(timeout: 12)
if !d.error {
Expand All @@ -97,7 +97,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler")
let expect = expectation(description: "GET \(urlString): with a completion handler")
var expectedResult = "unknown"
let task = session.dataTask(with: url) { data, response, error in
defer { expect.fulfill() }
Expand All @@ -116,7 +116,7 @@ class TestURLSession : XCTestCase {
func test_dataTaskWithURLRequest() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let urlRequest = URLRequest(url: URL(string: urlString)!)
let d = DataTask(with: expectation(description: "data task"))
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
d.run(with: urlRequest)
waitForExpectations(timeout: 12)
if !d.error {
Expand All @@ -130,7 +130,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler")
let expect = expectation(description: "GET \(urlString): with a completion handler")
var expectedResult = "unknown"
let task = session.dataTask(with: urlRequest) { data, response, error in
defer { expect.fulfill() }
Expand All @@ -149,25 +149,26 @@ class TestURLSession : XCTestCase {
func test_downloadTaskWithURL() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let url = URL(string: urlString)!
let d = DownloadTask(with: expectation(description: "download task with delegate"))
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
d.run(with: url)
waitForExpectations(timeout: 12)
}

func test_downloadTaskWithURLRequest() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let urlRequest = URLRequest(url: URL(string: urlString)!)
let d = DownloadTask(with: expectation(description: "download task with delegate"))
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
d.run(with: urlRequest)
waitForExpectations(timeout: 12)
}

func test_downloadTaskWithRequestAndHandler() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "download task with handler")
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt")!)
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
let req = URLRequest(url: URL(string: urlString)!)
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
expect.fulfill()
Expand All @@ -179,9 +180,10 @@ class TestURLSession : XCTestCase {
func test_downloadTaskWithURLAndHandler() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "download task with handler")
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt")!)
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
let req = URLRequest(url: URL(string: urlString)!)
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
if let e = error as? URLError {
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
Expand All @@ -193,12 +195,13 @@ class TestURLSession : XCTestCase {
}

func test_finishTasksAndInvalidate() {
let invalidateExpectation = expectation(description: "URLSession wasn't invalidated")
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let invalidateExpectation = expectation(description: "Session invalidation")
let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal")!
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: delegate, delegateQueue: nil)
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
let completionExpectation = expectation(description: "GET \(urlString): task completion before session invalidation")
let task = session.dataTask(with: url) { (_, _, _) in
completionExpectation.fulfill()
}
Expand All @@ -208,11 +211,12 @@ class TestURLSession : XCTestCase {
}

func test_taskError() {
let url = URL(string: "http://127.0.0.1:-1/Nepal")!
let urlString = "http://127.0.0.1:-1/Nepal"
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: nil)
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
let completionExpectation = expectation(description: "GET \(urlString): Bad URL error")
let task = session.dataTask(with: url) { (_, _, result) in
let error = result as? URLError
XCTAssertNotNil(error)
Expand Down Expand Up @@ -241,9 +245,10 @@ class TestURLSession : XCTestCase {
}

func test_cancelTask() {
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!
let d = DataTask(with: expectation(description: "Task to be canceled"))
d.cancelExpectation = expectation(description: "URLSessionTask wasn't canceled")
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
d.run(with: url)
d.cancel()
waitForExpectations(timeout: 12)
Expand All @@ -252,9 +257,10 @@ class TestURLSession : XCTestCase {
func test_verifyRequestHeaders() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
var expect = expectation(description: "download task with handler")
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders")!)
var expect = expectation(description: "POST \(urlString): get request headers")
var req = URLRequest(url: URL(string: urlString)!)
let headers = ["header1": "value1"]
req.httpMethod = "POST"
req.allHTTPHeaderFields = headers
Expand All @@ -278,9 +284,10 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
config.httpAdditionalHeaders = ["header2": "svalue2", "header3": "svalue3"]
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
var expect = expectation(description: "download task with handler")
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders")!)
var expect = expectation(description: "POST \(urlString) with additional headers")
var req = URLRequest(url: URL(string: urlString)!)
let headers = ["header1": "rvalue1", "header2": "rvalue2"]
req.httpMethod = "POST"
req.allHTTPHeaderFields = headers
Expand All @@ -301,9 +308,10 @@ class TestURLSession : XCTestCase {
func test_taskTimeout() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
var expect = expectation(description: "download task with handler")
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!)
var expect = expectation(description: "GET \(urlString): no timeout")
let req = URLRequest(url: URL(string: urlString)!)
var task = session.dataTask(with: req) { (data, _, error) -> Void in
defer { expect.fulfill() }
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
Expand All @@ -316,8 +324,9 @@ class TestURLSession : XCTestCase {
func test_timeoutInterval() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 10
let urlString = "http://127.0.0.1:-1/Peru"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
var expect = expectation(description: "download task with handler")
var expect = expectation(description: "GET \(urlString): will timeout")
var req = URLRequest(url: URL(string: "http://127.0.0.1:-1/Peru")!)
req.timeoutInterval = 1
var task = session.dataTask(with: req) { (data, _, error) -> Void in
Expand All @@ -336,7 +345,8 @@ class TestURLSession : XCTestCase {
config.protocolClasses = [CustomProtocol.self]
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with custom protocol")
let expect = expectation(description: "GET \(urlString): with a custom protocol")

let task = session.dataTask(with: url) { data, response, error in
defer { expect.fulfill() }
if let e = error as? URLError {
Expand All @@ -351,26 +361,28 @@ class TestURLSession : XCTestCase {
}

func test_customProtocolResponseWithDelegate() {
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!
let d = DataTask(with: expectation(description: "Custom protocol with delegate"), protocolClasses: [CustomProtocol.self])
d.responseReceivedExpectation = expectation(description: "A response wasn't received")
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self])
d.responseReceivedExpectation = expectation(description: "GET \(urlString): response received")
d.run(with: url)
waitForExpectations(timeout: 12)
}

func test_httpRedirection() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "data task"))
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)
}

func test_httpRedirectionTimeout() {
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates")!)
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
var req = URLRequest(url: URL(string: urlString)!)
req.timeoutInterval = 3
let config = URLSessionConfiguration.default
var expect = expectation(description: "download task with handler")
var expect = expectation(description: "GET \(urlString): timeout with redirection ")
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let task = session.dataTask(with: req) { data, response, error in
defer { expect.fulfill() }
Expand All @@ -391,7 +403,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
let expect = expectation(description: "GET \(urlString): simple HTTP/0.9 response")
var expectedResult = "unknown"
let task = session.dataTask(with: url) { data, response, error in
XCTAssertNotNil(data)
Expand Down Expand Up @@ -419,7 +431,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
let expect = expectation(description: "GET \(urlString): out of range HTTP code")
let task = session.dataTask(with: url) { data, response, error in
XCTAssertNotNil(data)
XCTAssertNotNil(response)
Expand All @@ -445,7 +457,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
let expect = expectation(description: "GET \(urlString): missing content length")
let task = session.dataTask(with: url) { data, response, error in
XCTAssertNotNil(data)
XCTAssertNotNil(response)
Expand All @@ -472,7 +484,7 @@ class TestURLSession : XCTestCase {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
let expect = expectation(description: "GET \(urlString): illegal response")
let task = session.dataTask(with: url) { data, response, error in
XCTAssertNil(data)
XCTAssertNil(response)
Expand Down