-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathMockAPIServer.swift
More file actions
149 lines (119 loc) · 4.95 KB
/
MockAPIServer.swift
File metadata and controls
149 lines (119 loc) · 4.95 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import Foundation
final class MockAPIServer {
static let shared = MockAPIServer()
// MARK: - API Response Mode (controlled by JWT Auth Retry panel)
//
// Matches Android MockServer.ResponseMode + the UI-only CONN_ERROR:
// - .normal → proxy to real Iterable API (see MockAPIServerURLProtocol)
// - .jwt401 → proxy to real Iterable API with Authorization swapped
// to an expired JWT; real backend returns real 401
// - .server500 → local synthesized 500 (can't force 500 from prod)
// - .connectionError → local synthesized connection error (iOS equivalent of
// Android's DEAD_PORT trick)
enum APIResponseMode: String, CaseIterable {
case normal = "Normal"
case jwt401 = "401"
case server500 = "500"
case connectionError = "Conn Err"
}
var apiResponseMode: APIResponseMode = .normal
/// JWT signing secret used by MockAPIServerURLProtocol when forging the
/// expired token for `.jwt401`. Populated by AppDelegate during SDK re-init.
var jwtSecret: String?
// MARK: - State
private(set) var isActive: Bool = false
private(set) var requestCount: Int = 0
private var authObserver: NSObjectProtocol?
private init() {}
// MARK: - Activation
func activate() {
guard !isActive else { return }
isActive = true
requestCount = 0
NetworkMonitor.registerProtocolClass(MockAPIServerURLProtocol.self)
print("[MOCK SERVER] Activated")
}
func deactivate() {
guard isActive else { return }
isActive = false
requestCount = 0
NetworkMonitor.unregisterProtocolClass(MockAPIServerURLProtocol.self)
if let observer = authObserver {
NotificationCenter.default.removeObserver(observer)
authObserver = nil
}
print("[MOCK SERVER] Deactivated")
}
// MARK: - Mock Response Generation
struct MockResponse {
let statusCode: Int
let data: Data
let headers: [String: String]
let error: Error? // non-nil for connection errors
init(statusCode: Int, data: Data, headers: [String: String], error: Error? = nil) {
self.statusCode = statusCode
self.data = data
self.headers = headers
self.error = error
}
}
/// Returns true if this request should be intercepted (not passed through to real network).
func shouldIntercept(request: URLRequest) -> Bool {
guard isActive, let url = request.url else { return false }
// getRemoteConfiguration always passes through to ConfigOverrideURLProtocol
if url.path.contains("getRemoteConfiguration") {
return false
}
// All other Iterable API requests are intercepted
guard url.host?.contains("iterable.com") == true else { return false }
return true
}
/// Returns a locally-synthesized mock response for `.server500` / `.connectionError`.
/// For `.normal` / `.jwt401` the request is proxied to the real Iterable API by
/// MockAPIServerURLProtocol; this method returns nil in those cases.
func mockResponse(for request: URLRequest) -> MockResponse? {
guard isActive, let url = request.url else { return nil }
if url.path.contains("getRemoteConfiguration") { return nil }
guard url.host?.contains("iterable.com") == true else { return nil }
requestCount += 1
let endpoint = url.path.split(separator: "/").last.map(String.init) ?? url.path
switch apiResponseMode {
case .normal, .jwt401:
return nil // handled by proxy
case .server500:
return server500Response(endpoint: endpoint)
case .connectionError:
return connectionErrorResponse()
}
}
// MARK: - Response Helpers
private func server500Response(endpoint: String) -> MockResponse {
let json: [String: Any] = [
"code": "InternalServerError",
"msg": "Mock 500 response"
]
let data = (try? JSONSerialization.data(withJSONObject: json)) ?? Data()
print("[MOCK SERVER] 500 \(endpoint)")
LogStore.shared.log("📤 \(endpoint) → 500 ❌ (mocked)")
return MockResponse(
statusCode: 500,
data: data,
headers: ["Content-Type": "application/json", "Connection": "close"]
)
}
private func connectionErrorResponse() -> MockResponse {
let error = NSError(
domain: NSURLErrorDomain,
code: NSURLErrorNotConnectedToInternet,
userInfo: [NSLocalizedDescriptionKey: "Mock: not connected to the Internet"]
)
print("[MOCK SERVER] Connection error")
LogStore.shared.log("📤 → Conn Err ❌")
return MockResponse(
statusCode: 0,
data: Data(),
headers: [:],
error: error
)
}
}