@@ -13,6 +13,13 @@ final public class URLProtocolStubNetworkEngine: NetworkEngine {
1313 private( set) var session : URLSession !
1414 internal let protocolClasses : [ AnyClass ]
1515
16+ /// Whether created background upload and download tasks should be autoresumed.
17+ /// Default = `true`.
18+ ///
19+ /// For unit testing it is sometimes useful to set this to `false` and manually call `resume()`
20+ /// on the `URLSessionTask` returned from the submit methods.
21+ public var autoResumesBackgroundTasks : Bool = true
22+
1623 /// Initializes an URLProtocolStubNetworkEngine with protocolClasses
1724 /// - Parameter protocolClasses: URLProtocol class that need to be stub
1825 /// default class will be URLProtocolStub
@@ -75,7 +82,9 @@ final public class URLProtocolStubNetworkEngine: NetworkEngine {
7582 // make a URLSessionDownloadTask, resume it, then return it
7683 let downloadTask = session. downloadTask ( with: request)
7784 downloadTask. taskDescription = " \( request: request) "
78- downloadTask. resume ( )
85+ if autoResumesBackgroundTasks {
86+ downloadTask. resume ( )
87+ }
7988 return downloadTask
8089 }
8190
@@ -100,7 +109,9 @@ final public class URLProtocolStubNetworkEngine: NetworkEngine {
100109 // make a URLSessionUploadTask, resume it, then return it
101110 let uploadTask = session. uploadTask ( with: request, from: data)
102111 uploadTask. taskDescription = " \( request: request) "
103- uploadTask. resume ( )
112+ if autoResumesBackgroundTasks {
113+ uploadTask. resume ( )
114+ }
104115 return uploadTask
105116 }
106117}
0 commit comments