Skip to content

Commit 5f5c273

Browse files
authored
Merge pull request swiftlang#1365 from pushkarnk/urlsession-shared
2 parents e573221 + b462bf2 commit 5f5c273

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Foundation/URLProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ open class URLProtocol : NSObject {
386386
}
387387

388388
internal class func getProtocols() -> [AnyClass]? {
389+
_classesLock.lock()
390+
defer { _classesLock.unlock() }
389391
return _registeredProtocolClasses
390392
}
391393
/*!

Foundation/URLSession/URLSession.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,19 @@ open class URLSession : NSObject {
201201
* The shared session uses the currently set global URLCache,
202202
* HTTPCookieStorage and URLCredential.Storage objects.
203203
*/
204-
open class var shared: URLSession { NSUnimplemented() }
205-
204+
open class var shared: URLSession {
205+
return _shared
206+
}
207+
208+
fileprivate static let _shared: URLSession = {
209+
var configuration = URLSessionConfiguration.default
210+
configuration.httpCookieStorage = HTTPCookieStorage.shared
211+
//TODO: Set urlCache to URLCache.shared. Needs implementation of URLCache.
212+
//TODO: Set urlCredentialStorage to `URLCredentialStorage.shared`. Needs implementation of URLCredentialStorage.
213+
configuration.protocolClasses = URLProtocol.getProtocols()
214+
return URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
215+
}()
216+
206217
/*
207218
* Customization of URLSession occurs during creation of a new session.
208219
* If you only need to use the convenience routines with custom

TestFoundation/TestURLSession.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,19 @@ class TestURLSession : LoopbackServerTest {
5959
}
6060

6161
func test_dataTaskWithURLCompletionHandler() {
62-
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/USA"
63-
let url = URL(string: urlString)!
62+
//shared session
63+
dataTaskWithURLCompletionHandler(with: URLSession.shared)
64+
65+
//new session
6466
let config = URLSessionConfiguration.default
6567
config.timeoutIntervalForRequest = 8
6668
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
69+
dataTaskWithURLCompletionHandler(with: session)
70+
}
71+
72+
func dataTaskWithURLCompletionHandler(with session: URLSession) {
73+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/USA"
74+
let url = URL(string: urlString)!
6775
let expect = expectation(description: "GET \(urlString): with a completion handler")
6876
var expectedResult = "unknown"
6977
let task = session.dataTask(with: url) { data, response, error in
@@ -130,10 +138,18 @@ class TestURLSession : LoopbackServerTest {
130138
}
131139

132140
func test_downloadTaskWithRequestAndHandler() {
141+
//shared session
142+
downloadTaskWithRequestAndHandler(with: URLSession.shared)
143+
144+
//newly created session
133145
let config = URLSessionConfiguration.default
134146
config.timeoutIntervalForRequest = 8
135-
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
136147
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
148+
downloadTaskWithRequestAndHandler(with: session)
149+
}
150+
151+
func downloadTaskWithRequestAndHandler(with session: URLSession) {
152+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
137153
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
138154
let req = URLRequest(url: URL(string: urlString)!)
139155
let task = session.downloadTask(with: req) { (_, _, error) -> Void in

0 commit comments

Comments
 (0)