Skip to content

Commit dc1addd

Browse files
authored
Merge pull request #1132 from pushkarnk/casting-issue-urlsession
2 parents bf325c1 + 3d4ea1d commit dc1addd

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,12 @@ extension _ProtocolClient : URLProtocolClient {
546546
guard let task = `protocol`.task else { fatalError() }
547547
guard let session = task.session as? URLSession else { fatalError() }
548548
switch session.behaviour(for: task) {
549-
case .taskDelegate(let delegate) where delegate is URLSessionDownloadDelegate:
550-
let downloadDelegate = delegate as! URLSessionDownloadDelegate
551-
let downloadTask = task as! URLSessionDownloadTask
552-
session.delegateQueue.addOperation {
553-
downloadDelegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: `protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as! URL)
554-
}
555549
case .taskDelegate(let delegate):
550+
if let downloadDelegate = delegate as? URLSessionDownloadDelegate, let downloadTask = task as? URLSessionDownloadTask {
551+
session.delegateQueue.addOperation {
552+
downloadDelegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: `protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as! URL)
553+
}
554+
}
556555
session.delegateQueue.addOperation {
557556
delegate.urlSession(session, task: task, didCompleteWithError: nil)
558557
task.state = .completed

TestFoundation/TestNSURLSession.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestURLSession : LoopbackServerTest {
4141
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
4242
("test_missingContentLengthButStillABody", test_missingContentLengthButStillABody),
4343
("test_illegalHTTPServerResponses", test_illegalHTTPServerResponses),
44+
("test_dataTaskWithSharedDelegate", test_dataTaskWithSharedDelegate),
4445
]
4546
}
4647

@@ -429,8 +430,37 @@ class TestURLSession : LoopbackServerTest {
429430
waitForExpectations(timeout: 12)
430431
}
431432
}
433+
434+
func test_dataTaskWithSharedDelegate() {
435+
let sharedDelegate = SharedDelegate()
436+
let urlString0 = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
437+
let session = URLSession(configuration: .default, delegate: sharedDelegate, delegateQueue: nil)
438+
439+
let dataRequest = URLRequest(url: URL(string: urlString0)!)
440+
let dataTask = session.dataTask(with: dataRequest)
441+
442+
sharedDelegate.dataCompletionExpectation = expectation(description: "GET \(urlString0)")
443+
dataTask.resume()
444+
waitForExpectations(timeout: 20)
445+
}
432446
}
433447

448+
class SharedDelegate: NSObject {
449+
var dataCompletionExpectation: XCTestExpectation!
450+
}
451+
452+
extension SharedDelegate: URLSessionDataDelegate {
453+
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
454+
dataCompletionExpectation.fulfill()
455+
}
456+
}
457+
458+
extension SharedDelegate: URLSessionDownloadDelegate {
459+
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
460+
}
461+
}
462+
463+
434464
class SessionDelegate: NSObject, URLSessionDelegate {
435465
let invalidateExpectation: XCTestExpectation
436466
init(invalidateExpectation: XCTestExpectation){

0 commit comments

Comments
 (0)