mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.9 KiB
Swift
119 lines
3.9 KiB
Swift
// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
import SessionUtilitiesKit
|
|
|
|
// MARK: - MockNetwork
|
|
|
|
class MockNetwork: Mock<NetworkType>, NetworkType {
|
|
var requestData: RequestData?
|
|
|
|
func send<T>(_ request: Network.RequestType<T>) -> AnyPublisher<(ResponseInfoType, T), Error> {
|
|
requestData = request.data
|
|
|
|
return mock(funcName: "send<\(T.self)>(\(request.id))", args: request.args)
|
|
}
|
|
|
|
static func response<T: Encodable>(info: MockResponseInfo = .mock, with value: T) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return Just((info, try? JSONEncoder().with(outputFormatting: .sortedKeys).encode(value)))
|
|
.setFailureType(to: Error.self)
|
|
.eraseToAnyPublisher()
|
|
}
|
|
|
|
static func response<T: Mocked & Encodable>(info: MockResponseInfo = .mock, type: T.Type) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return response(info: info, with: T.mock)
|
|
}
|
|
|
|
static func response<T: Mocked & Encodable>(info: MockResponseInfo = .mock, type: Array<T>.Type) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return response(info: info, with: [T.mock])
|
|
}
|
|
|
|
static func batchResponseData<E: EndpointType>(
|
|
info: MockResponseInfo = .mock,
|
|
with value: [(endpoint: E, data: Data)]
|
|
) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
let data: Data = "[\(value.map { String(data: $0.data, encoding: .utf8)! }.joined(separator: ","))]"
|
|
.data(using: .utf8)!
|
|
|
|
return Just((info, data))
|
|
.setFailureType(to: Error.self)
|
|
.eraseToAnyPublisher()
|
|
}
|
|
|
|
static func response(info: MockResponseInfo = .mock, data: Data) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return Just((info, data))
|
|
.setFailureType(to: Error.self)
|
|
.eraseToAnyPublisher()
|
|
}
|
|
|
|
static func nullResponse(info: MockResponseInfo = .mock) -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return Just((info, nil))
|
|
.setFailureType(to: Error.self)
|
|
.eraseToAnyPublisher()
|
|
}
|
|
|
|
static func errorResponse() -> AnyPublisher<(ResponseInfoType, Data?), Error> {
|
|
return Fail(error: TestError.mock).eraseToAnyPublisher()
|
|
}
|
|
}
|
|
|
|
// MARK: - MockResponseInfo
|
|
|
|
struct MockResponseInfo: ResponseInfoType, Mocked {
|
|
static let mock: MockResponseInfo = MockResponseInfo(requestData: .fallbackData, code: 200, headers: [:])
|
|
|
|
let requestData: RequestData
|
|
let code: Int
|
|
let headers: [String: String]
|
|
|
|
init(requestData: RequestData, code: Int, headers: [String: String]) {
|
|
self.requestData = requestData
|
|
self.code = code
|
|
self.headers = headers
|
|
}
|
|
}
|
|
|
|
struct RequestData: Codable {
|
|
static let fallbackData: RequestData = RequestData(urlString: nil, httpMethod: "GET", headers: [:], body: nil)
|
|
|
|
let urlString: String?
|
|
let httpMethod: String
|
|
let headers: [String: String]
|
|
let body: Data?
|
|
}
|
|
|
|
extension Network.RequestType {
|
|
var data: RequestData {
|
|
return RequestData(
|
|
urlString: url,
|
|
httpMethod: (method ?? ""),
|
|
headers: (headers ?? [:]),
|
|
body: body
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - HTTP.BatchSubResponse Encoding Convenience
|
|
|
|
extension Encodable where Self: Codable {
|
|
func batchSubResponse() -> Data {
|
|
return try! JSONEncoder().with(outputFormatting: .sortedKeys).encode(
|
|
HTTP.BatchSubResponse(
|
|
code: 200,
|
|
headers: [:],
|
|
body: self,
|
|
failedToParseBody: false
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
extension Mocked where Self: Codable {
|
|
static func mockBatchSubResponse() -> Data { return mock.batchSubResponse() }
|
|
}
|
|
|
|
extension Array where Element: Mocked, Element: Codable {
|
|
static func mockBatchSubResponse() -> Data { return [Element.mock].batchSubResponse() }
|
|
}
|