1 Star 0 Fork 0

azure/firefox-ios

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Mocking.swift 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
/* 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> {
return Sync15BatchClient(config: self.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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/xstudio/firefox-ios.git
[email protected]:xstudio/firefox-ios.git
xstudio
firefox-ios
firefox-ios
master

搜索帮助