-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOSIABCacheManagerStub.swift
More file actions
68 lines (57 loc) · 2.23 KB
/
OSIABCacheManagerStub.swift
File metadata and controls
68 lines (57 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import OSInAppBrowserLib
import WebKit
final class OSIABCacheManagerStub: OSIABCacheManager {
var cacheWasCleared: Bool
var sessionCacheWasCleared: Bool
init(cacheWasCleared: Bool = false, sessionCacheWasCleared: Bool = false) {
self.cacheWasCleared = cacheWasCleared
self.sessionCacheWasCleared = sessionCacheWasCleared
}
func clearCache(_ completionHandler: CacheCompletion?) {
self.cacheWasCleared = true
completionHandler?()
}
func clearSessionCache(_ completionHandler: CacheCompletion?) {
self.sessionCacheWasCleared = true
completionHandler?()
}
}
final class OSIABWebsiteDataStoreStub: WKWebsiteDataStore {
static func dataStore(numberOfCookiesToAdd: Int = 0, numberOfSessionCookiesToAdd: Int = 0, _ completionHandler: @escaping () -> Void) -> WKWebsiteDataStore {
var cookieArray = [HTTPCookie]()
if numberOfCookiesToAdd > 0 {
cookieArray += Array(repeating: OSIABCookieMock(), count: numberOfCookiesToAdd)
}
if numberOfSessionCookiesToAdd > 0 {
cookieArray += Array(repeating: OSIABCookieMock(isSessionCookie: true), count: numberOfSessionCookiesToAdd)
}
let dataStore = Self.nonPersistent()
dataStore.addCookies(cookieArray, completionHandler)
return dataStore
}
}
extension WKWebsiteDataStore {
func addCookies(_ cookieArray: [HTTPCookie], _ completionHandler: @escaping () -> Void) {
guard let cookie = cookieArray.first else {
return completionHandler()
}
self.httpCookieStore.setCookie(cookie) { [weak self] in
self?.addCookies(Array(cookieArray.dropFirst()), completionHandler)
}
}
}
final class OSIABCookieMock: HTTPCookie {
var isSessionCookie: Bool
init(isSessionCookie: Bool = false) {
self.isSessionCookie = isSessionCookie
super.init(properties: [
.domain: "example.com",
.path: "/",
.name: "MyCookieName",
.value: "MyCookieValue",
.secure: "TRUE",
.expires: NSDate(timeIntervalSinceNow: 31556926)
])!
}
override var isSessionOnly: Bool { self.isSessionCookie }
}