Skip to content

[SR-5548] Remove invalid cast in URLSessionTask #1132

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 25, 2017
Merged
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions Foundation/NSURLSession/NSURLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,12 @@ extension _ProtocolClient : URLProtocolClient {
guard let task = `protocol`.task else { fatalError() }
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate) where delegate is URLSessionDownloadDelegate:
let downloadDelegate = delegate as! URLSessionDownloadDelegate
let downloadTask = task as! URLSessionDownloadTask
session.delegateQueue.addOperation {
downloadDelegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: `protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as! URL)
}
case .taskDelegate(let delegate):
if let downloadDelegate = delegate as? URLSessionDownloadDelegate, let downloadTask = task as? URLSessionDownloadTask {
session.delegateQueue.addOperation {
downloadDelegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: `protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as! URL)
}
}
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: nil)
task.state = .completed
Expand Down
30 changes: 30 additions & 0 deletions TestFoundation/TestNSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TestURLSession : LoopbackServerTest {
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
("test_missingContentLengthButStillABody", test_missingContentLengthButStillABody),
("test_illegalHTTPServerResponses", test_illegalHTTPServerResponses),
("test_dataTaskWithSharedDelegate", test_dataTaskWithSharedDelegate),
]
}

Expand Down Expand Up @@ -429,8 +430,37 @@ class TestURLSession : LoopbackServerTest {
waitForExpectations(timeout: 12)
}
}

func test_dataTaskWithSharedDelegate() {
let sharedDelegate = SharedDelegate()
let urlString0 = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let session = URLSession(configuration: .default, delegate: sharedDelegate, delegateQueue: nil)

let dataRequest = URLRequest(url: URL(string: urlString0)!)
let dataTask = session.dataTask(with: dataRequest)

sharedDelegate.dataCompletionExpectation = expectation(description: "GET \(urlString0)")
dataTask.resume()
waitForExpectations(timeout: 20)
}
}

class SharedDelegate: NSObject {
var dataCompletionExpectation: XCTestExpectation!
}

extension SharedDelegate: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
dataCompletionExpectation.fulfill()
}
}

extension SharedDelegate: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
}


class SessionDelegate: NSObject, URLSessionDelegate {
let invalidateExpectation: XCTestExpectation
init(invalidateExpectation: XCTestExpectation){
Expand Down