@@ -147,15 +147,15 @@ class TestURLSession : LoopbackServerTest {
147
147
func test_downloadTaskWithURL( ) {
148
148
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
149
149
let url = URL ( string: urlString) !
150
- let d = DownloadTask ( with : expectation ( description: " Download GET \( urlString) : with a delegate " ) )
150
+ let d = DownloadTask ( testCase : self , description: " Download GET \( urlString) : with a delegate " )
151
151
d. run ( with: url)
152
152
waitForExpectations ( timeout: 12 )
153
153
}
154
154
155
155
func test_downloadTaskWithURLRequest( ) {
156
156
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
157
157
let urlRequest = URLRequest ( url: URL ( string: urlString) !)
158
- let d = DownloadTask ( with : expectation ( description: " Download GET \( urlString) : with a delegate " ) )
158
+ let d = DownloadTask ( testCase : self , description: " Download GET \( urlString) : with a delegate " )
159
159
d. run ( with: urlRequest)
160
160
waitForExpectations ( timeout: 12 )
161
161
}
@@ -203,7 +203,7 @@ class TestURLSession : LoopbackServerTest {
203
203
func test_gzippedDownloadTask( ) {
204
204
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /gzipped-response "
205
205
let url = URL ( string: urlString) !
206
- let d = DownloadTask ( with : expectation ( description: " GET \( urlString) : gzipped response " ) )
206
+ let d = DownloadTask ( testCase : self , description: " GET \( urlString) : gzipped response " )
207
207
d. run ( with: url)
208
208
waitForExpectations ( timeout: 12 )
209
209
if d. totalBytesWritten != " Hello World! " . utf8. count {
@@ -986,7 +986,7 @@ class TestURLSession : LoopbackServerTest {
986
986
} . resume ( )
987
987
waitForExpectations ( timeout: 20 )
988
988
989
- let d = DownloadTask ( with : expectation ( description: " Invalid resume data for download task (with delegate) " ) )
989
+ let d = DownloadTask ( testCase : self , description: " Invalid resume data for download task " )
990
990
d. run { ( session) -> DownloadTask . Configuration in
991
991
return DownloadTask . Configuration ( task: session. downloadTask ( withResumeData: Data ( ) ) ,
992
992
errorExpectation:
@@ -1234,16 +1234,28 @@ extension DataTask : URLSessionTaskDelegate {
1234
1234
1235
1235
class DownloadTask : NSObject {
1236
1236
var totalBytesWritten : Int64 = 0
1237
- let dwdExpectation : XCTestExpectation !
1237
+ var didDownloadExpectation : XCTestExpectation ?
1238
+ let didCompleteExpectation : XCTestExpectation
1238
1239
var session : URLSession ! = nil
1239
1240
var task : URLSessionDownloadTask ! = nil
1240
1241
var errorExpectation : ( ( Error ) -> Void ) ?
1242
+ weak var testCase : XCTestCase ?
1243
+ var expectationsDescription : String
1241
1244
1242
- init ( with expectation: XCTestExpectation ) {
1243
- dwdExpectation = expectation
1245
+ init ( testCase: XCTestCase , description: String ) {
1246
+ self . expectationsDescription = description
1247
+ self . testCase = testCase
1248
+ self . didCompleteExpectation = testCase. expectation ( description: " Did complete \( description) " )
1249
+ }
1250
+
1251
+ private func makeDownloadExpectation( ) {
1252
+ guard didDownloadExpectation == nil else { return }
1253
+ self . didDownloadExpectation = testCase!. expectation ( description: " Did finish download: \( description) " )
1254
+ self . testCase = nil // No need for it any more here.
1244
1255
}
1245
1256
1246
1257
func run( with url: URL ) {
1258
+ makeDownloadExpectation ( )
1247
1259
let config = URLSessionConfiguration . default
1248
1260
config. timeoutIntervalForRequest = 8
1249
1261
session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
@@ -1252,6 +1264,7 @@ class DownloadTask : NSObject {
1252
1264
}
1253
1265
1254
1266
func run( with urlRequest: URLRequest ) {
1267
+ makeDownloadExpectation ( )
1255
1268
let config = URLSessionConfiguration . default
1256
1269
config. timeoutIntervalForRequest = 8
1257
1270
session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
@@ -1272,6 +1285,9 @@ class DownloadTask : NSObject {
1272
1285
1273
1286
task = taskConfiguration. task
1274
1287
errorExpectation = taskConfiguration. errorExpectation
1288
+ if errorExpectation == nil {
1289
+ makeDownloadExpectation ( )
1290
+ }
1275
1291
task. resume ( )
1276
1292
}
1277
1293
}
@@ -1284,7 +1300,7 @@ extension DownloadTask : URLSessionDownloadDelegate {
1284
1300
}
1285
1301
1286
1302
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didFinishDownloadingTo location: URL ) {
1287
- defer { dwdExpectation . fulfill ( ) }
1303
+ defer { didDownloadExpectation ? . fulfill ( ) }
1288
1304
1289
1305
guard self . errorExpectation == nil else {
1290
1306
XCTFail ( " Expected an error, but got …didFinishDownloadingTo… from download task \( downloadTask) (at \( location) ) " )
@@ -1302,7 +1318,7 @@ extension DownloadTask : URLSessionDownloadDelegate {
1302
1318
1303
1319
extension DownloadTask : URLSessionTaskDelegate {
1304
1320
public func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
1305
- defer { dwdExpectation . fulfill ( ) }
1321
+ defer { didCompleteExpectation . fulfill ( ) }
1306
1322
1307
1323
if let errorExpectation = self . errorExpectation {
1308
1324
if let error = error {
0 commit comments