Files
suixinkan_uikit/suixinkanTests/MockURLSession.swift
汉秋 699f1ba7c4 Add networking layer and wire up v9 login flow with unit tests.
Introduce APIClient/AuthAPI, complete login with multi-account selection, and polish the login card UI with shadow and keyboard dismiss.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 15:33:34 +08:00

64 lines
1.9 KiB
Swift

//
// MockURLSession.swift
// suixinkanTests
//
import Foundation
@testable import suixinkan
/// URLSession
final class MockURLSession: URLSessionProtocol {
private(set) var requests: [URLRequest] = []
private var responses: [Result<(Data, URLResponse), Error>]
private var responseIndex = 0
init(responses: [Data], statusCode: Int = 200) {
self.responses = responses.map { data in
.success((
data,
HTTPURLResponse(
url: URL(string: "https://api-test.zhifly.cn/mock")!,
statusCode: statusCode,
httpVersion: nil,
headerFields: nil
)!
))
}
}
init(results: [Result<(Data, URLResponse), Error>]) {
self.responses = results
}
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
requests.append(request)
guard responseIndex < responses.count else {
throw URLError(.badServerResponse)
}
defer { responseIndex += 1 }
return try responses[responseIndex].get()
}
}
enum TestJSON {
static func envelope<T: Encodable>(data: T) throws -> Data {
let payload = try JSONEncoder().encode(data)
let payloadObject = try JSONSerialization.jsonObject(with: payload)
let envelope: [String: Any] = [
"code": 100000,
"msg": "success",
"data": payloadObject,
]
return try JSONSerialization.data(withJSONObject: envelope)
}
static func errorEnvelope(code: Int, msg: String) throws -> Data {
let envelope: [String: Any] = [
"code": code,
"msg": msg,
"data": NSNull(),
]
return try JSONSerialization.data(withJSONObject: envelope)
}
}