Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make URLSession injectable into Networking for easy mocking. #115

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "Quick/Nimble" "v7.3.1"
github "Quick/Quick" "v1.3.2"
github "Quick/Nimble" "v8.0.1"
github "Quick/Quick" "v2.1.0"
github "vadymmarkov/When" "4.0.0"
29 changes: 14 additions & 15 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,15 @@ public final class Networking<R: RequestConvertible>: NSObject, URLSessionDelega
promise.resolve(Void())
}

let sessionConfiguration: SessionConfiguration
let mockProvider: MockProvider<R>?
let queue: OperationQueue

let session: URLSession

var customHeaders = [String: String]()
var requestStorage = RequestStorage()
var mode: NetworkingMode = .async

weak var sessionDelegate: URLSessionDelegate?

lazy var session: URLSession = { [unowned self] in
let session = URLSession(
configuration: self.sessionConfiguration.value,
delegate: self.sessionDelegate ?? self,
delegateQueue: nil)
return session
}()

var requestHeaders: [String: String] {
var headers = customHeaders
headers["Accept-Language"] = Header.acceptLanguage
Expand All @@ -68,16 +59,24 @@ public final class Networking<R: RequestConvertible>: NSObject, URLSessionDelega

public init(mode: NetworkingMode = .async,
mockProvider: MockProvider<R>? = nil,
sessionConfiguration: SessionConfiguration = SessionConfiguration.default,
sessionDelegate: URLSessionDelegate? = nil) {
session: URLSession) {
self.mockProvider = mockProvider
self.sessionConfiguration = sessionConfiguration
self.sessionDelegate = sessionDelegate
queue = OperationQueue()
self.session = session
super.init()
reset(mode: mode)
}

public init(mode: NetworkingMode = .async,
mockProvider: MockProvider<R>? = nil,
configuration: SessionConfiguration = .default) {
self.mockProvider = mockProvider
queue = OperationQueue()
self.session = URLSession(configuration: configuration.value)
super.init()
reset(mode: mode)
}

public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
Expand Down
10 changes: 7 additions & 3 deletions Sources/Request/RequestStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ final class RequestStorage {
}

func saveAll() {
let data = NSKeyedArchiver.archivedData(withRootObject: requests)
userDefaults.set(data, forKey: key)
userDefaults.synchronize()
if #available(OSX 10.11, *) {
let data = NSKeyedArchiver.archivedData(withRootObject: requests)
userDefaults.set(data, forKey: key)
userDefaults.synchronize()
} else {
// Fallback on earlier versions
}
}

// MARK: - Remove
Expand Down