Skip to content

Commit afd53fc

Browse files
author
Pushkar N Kulkarni
authored
Merge pull request #713 from naithar/URLSessionTask
Implementation of URLSessionTask '.error', NSCopying
2 parents fc8655e + 4c2d2c2 commit afd53fc

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ There is no _Complete_ status for test coverage because there are always additio
7878
| `URLSession` | Mostly Complete | Incomplete | `shared`, invalidation, resetting, flushing, getting tasks, and others remain unimplemented |
7979
| `URLSessionConfiguration` | Mostly Complete | Incomplete | `ephemeral` and `background(withIdentifier:)` remain unimplemented |
8080
| `URLSessionDelegate` | Complete | N/A | |
81-
| `URLSessionTask` | Mostly Complete | Incomplete | `NSCopying`, `cancel()`, `error`, `createTransferState(url:)` with streams, and others remain unimplemented |
81+
| `URLSessionTask` | Mostly Complete | Incomplete | `cancel()`, `createTransferState(url:)` with streams, and others remain unimplemented |
8282
| `URLSessionDataTask` | Complete | Incomplete | |
8383
| `URLSessionUploadTask` | Complete | None | |
8484
| `URLSessionDownloadTask` | Incomplete | Incomplete | `cancel(byProducingResumeData:)` remains unimplemented |

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ open class URLSessionTask : NSObject, NSCopying {
128128
}
129129

130130
open func copy(with zone: NSZone?) -> Any {
131-
NSUnimplemented()
131+
return self
132132
}
133133

134134
/// An identifier for this task, assigned by and unique to the owning session
@@ -221,7 +221,10 @@ open class URLSessionTask : NSObject, NSCopying {
221221
* The error, if any, delivered via -URLSession:task:didCompleteWithError:
222222
* This property will be nil in the event that no error occured.
223223
*/
224-
/*@NSCopying*/ open var error: NSError? { NSUnimplemented() }
224+
fileprivate var _error: NSError?
225+
/*@NSCopying*/ open var error: NSError? {
226+
return self._error
227+
}
225228

226229
/// Suspend the task.
227230
///
@@ -877,6 +880,8 @@ extension URLSessionTask {
877880
}
878881
}
879882
func completeTask(withError error: NSError) {
883+
self._error = error
884+
880885
guard case .transferFailed = internalState else {
881886
fatalError("Trying to complete the task, but its transfer isn't complete / failed.")
882887
}
@@ -1032,8 +1037,8 @@ fileprivate extension URLSessionTask {
10321037
guard case .waitingForResponseCompletionHandler(let ts) = internalState else { fatalError("Received response disposition, but we're not waiting for it.") }
10331038
switch disposition {
10341039
case .cancel:
1035-
//TODO: Fail the task with NSURLErrorCancelled
1036-
NSUnimplemented()
1040+
let error = NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled)
1041+
self.completeTask(withError: error)
10371042
case .allow:
10381043
// Continue the transfer. This will unpause the easy handle.
10391044
internalState = .transferInProgress(ts)

TestFoundation/TestNSURLSession.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class TestURLSession : XCTestCase {
2929
("test_downloadTaskWithURLRequest", test_downloadTaskWithURLRequest),
3030
("test_downloadTaskWithRequestAndHandler", test_downloadTaskWithRequestAndHandler),
3131
("test_downloadTaskWithURLAndHandler", test_downloadTaskWithURLAndHandler),
32-
("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate)
32+
("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate),
33+
("test_taskError", test_taskError),
34+
("test_taskCopy", test_taskCopy),
3335
]
3436
}
3537

@@ -262,6 +264,39 @@ class TestURLSession : XCTestCase {
262264
session.finishTasksAndInvalidate()
263265
waitForExpectations(timeout: 12)
264266
}
267+
268+
func test_taskError() {
269+
let url = URL(string: "http://127.0.0.1:\(serverPort)/Nepal")!
270+
let session = URLSession(configuration: URLSessionConfiguration.default,
271+
delegate: nil,
272+
delegateQueue: nil)
273+
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
274+
let task = session.dataTask(with: url) { result in
275+
let error = result.2
276+
XCTAssertNotNil(error)
277+
XCTAssertEqual(error?.code, NSURLErrorBadURL)
278+
completionExpectation.fulfill()
279+
}
280+
//should result in Bad URL error
281+
task.resume()
282+
283+
waitForExpectations(timeout: 5) { error in
284+
XCTAssertNil(error)
285+
286+
XCTAssertNotNil(task.error)
287+
XCTAssertEqual(task.error?.code, NSURLErrorBadURL)
288+
}
289+
}
290+
291+
func test_taskCopy() {
292+
let url = URL(string: "http://127.0.0.1:\(serverPort)/Nepal")!
293+
let session = URLSession(configuration: URLSessionConfiguration.default,
294+
delegate: nil,
295+
delegateQueue: nil)
296+
let task = session.dataTask(with: url)
297+
298+
XCTAssert(task.isEqual(task.copy()))
299+
}
265300
}
266301

267302
class SessionDelegate: NSObject, URLSessionDelegate {

0 commit comments

Comments
 (0)