forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mocking.swift
74 lines (62 loc) · 2.91 KB
/
Mocking.swift
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
69
70
71
72
73
74
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
import SwiftyJSON
@testable import Sync
class MockBackoffStorage: BackoffStorage {
var serverBackoffUntilLocalTimestamp: Timestamp?
func clearServerBackoff() {
serverBackoffUntilLocalTimestamp = nil
}
func isInBackoff(_ now: Timestamp) -> Timestamp? {
return nil
}
}
class MockSyncCollectionClient<T: CleartextPayloadJSON>: Sync15CollectionClient<T> {
let uploader: BatchUploadFunction
let infoConfig: InfoConfiguration
init(uploader: @escaping BatchUploadFunction,
infoConfig: InfoConfiguration,
collection: String,
encrypter: RecordEncrypter<T>,
client: Sync15StorageClient = getClient(server: getServer(preStart: { _ in })),
serverURI: URL = URL(string: "http://localhost/collections")!)
{
self.uploader = uploader
self.infoConfig = infoConfig
super.init(client: client, serverURI: serverURI, collection: collection, encrypter: encrypter)
}
override func newBatch(ifUnmodifiedSince: Timestamp? = nil, onCollectionUploaded: @escaping (POSTResult, Timestamp?) -> DeferredTimestamp) -> Sync15BatchClient<T> {
let infoConfig = InfoConfiguration(maxRequestBytes: 1000,
maxPostRecords: 10,
maxPostBytes: 1000,
maxTotalRecords: 10,
maxTotalBytes: 1000)
return Sync15BatchClient(config: infoConfig,
ifUnmodifiedSince: ifUnmodifiedSince,
serializeRecord: self.serializeRecord,
uploader: self.uploader,
onCollectionUploaded: onCollectionUploaded)
}
}
// MARK: Various mocks
// Non-encrypting 'encrypter'.
func getEncrypter() -> RecordEncrypter<CleartextPayloadJSON> {
let serializer: (Record<CleartextPayloadJSON>) -> JSON? = { $0.payload.json }
let factory: (String) -> CleartextPayloadJSON = { CleartextPayloadJSON($0) }
return RecordEncrypter(serializer: serializer, factory: factory)
}
func getClient(server: MockSyncServer) -> Sync15StorageClient {
let url = server.baseURL.asURL!
let authorizer: Authorizer = identity
let queue = DispatchQueue.global(qos: DispatchQoS.background.qosClass)
return Sync15StorageClient(serverURI: url, authorizer: authorizer, workQueue: queue, resultQueue: queue, backoff: MockBackoffStorage())
}
func getServer(username: String = "1234567", preStart: (MockSyncServer) -> Void) -> MockSyncServer {
let server = MockSyncServer(username: username)
preStart(server)
server.start()
return server
}