Skip to content

Commit b014c3d

Browse files
committed
🧪 Added tests
1 parent ec9bfc8 commit b014c3d

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

tests/offline-events-tests/RequestHandlerTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,53 @@ class RequestHandlerTests: XCTestCase {
854854
NetworkHelper.isNetworkLoggingEnabled = false
855855
}
856856

857+
func testNetworkLoggingGetRequest() throws {
858+
// 1. Setup Mock Log Delegate
859+
class MockLogDelegate: NSObject, IterableLogDelegate {
860+
var loggedMessages: [String] = []
861+
func log(level: LogLevel, message: String) {
862+
loggedMessages.append(message)
863+
}
864+
}
865+
let mockLogDelegate = MockLogDelegate()
866+
IterableLogUtil.sharedInstance = IterableLogUtil(dateProvider: SystemDateProvider(), logDelegate: mockLogDelegate)
867+
868+
// 2. Enable Network Logging
869+
NetworkHelper.isNetworkLoggingEnabled = true
870+
871+
// 3. Perform GET Request
872+
let expectation1 = expectation(description: "GET Request success")
873+
let networkSession = MockNetworkSession(statusCode: 200, data: "{}".data(using: .utf8)!)
874+
875+
networkSession.requestCallback = { request in
876+
expectation1.fulfill()
877+
}
878+
879+
let requestHandler = createRequestHandler(networkSession: networkSession, notificationCenter: MockNotificationCenter(), selectOffline: false)
880+
881+
let _ = requestHandler.getRemoteConfiguration()
882+
883+
wait(for: [expectation1], timeout: testExpectationTimeout)
884+
885+
// Wait a little for async logging dispatch
886+
let loggingExpectation = expectation(description: "Logging wait")
887+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
888+
loggingExpectation.fulfill()
889+
}
890+
wait(for: [loggingExpectation], timeout: 1.0)
891+
892+
// 4. Verify Logs
893+
// Should see "sending request" but NOT "request body"
894+
let requestLogs = mockLogDelegate.loggedMessages.filter { $0.contains("sending request") }
895+
let bodyLogs = mockLogDelegate.loggedMessages.filter { $0.contains("request body") }
896+
897+
XCTAssertTrue(requestLogs.count > 0, "Should have logged request sending")
898+
XCTAssertTrue(bodyLogs.count == 0, "Should NOT have logged body for GET request")
899+
900+
// Cleanup
901+
NetworkHelper.isNetworkLoggingEnabled = false
902+
}
903+
857904
func testCreatedAtSentAtForOffline() throws {
858905
let expectation1 = expectation(description: #function)
859906
let date = Date().addingTimeInterval(-5000)

tests/unit-tests/IterableAPITests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,4 +1417,24 @@ class IterableAPITests: XCTestCase {
14171417
XCTAssertEqual(dateFromMilliseconds, testDate)
14181418
}
14191419

1420+
func testInitializeWithNetworkLoggingEnabled() {
1421+
let mockLocalStorage = MockLocalStorage()
1422+
mockLocalStorage.networkLoggingEnabled = true
1423+
1424+
_ = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, localStorage: mockLocalStorage)
1425+
1426+
XCTAssertTrue(NetworkHelper.isNetworkLoggingEnabled)
1427+
1428+
// Cleanup
1429+
NetworkHelper.isNetworkLoggingEnabled = false
1430+
}
1431+
1432+
func testInitializeWithNetworkLoggingDisabled() {
1433+
let mockLocalStorage = MockLocalStorage()
1434+
mockLocalStorage.networkLoggingEnabled = false
1435+
1436+
_ = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, localStorage: mockLocalStorage)
1437+
1438+
XCTAssertFalse(NetworkHelper.isNetworkLoggingEnabled)
1439+
}
14201440
}

tests/unit-tests/LocalStorageTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ class LocalStorageTests: XCTestCase {
149149
testLocalStorage(saver: saver, retriever: retriever, value: false)
150150
}
151151

152+
func testNetworkLoggingEnabled() {
153+
let saver = { (storage: LocalStorageProtocol, value: Bool) -> Void in
154+
var localStorage = storage
155+
localStorage.networkLoggingEnabled = value
156+
}
157+
let retriever = { (storage: LocalStorageProtocol) -> Bool? in
158+
storage.networkLoggingEnabled
159+
}
160+
161+
testLocalStorage(saver: saver, retriever: retriever, value: true)
162+
testLocalStorage(saver: saver, retriever: retriever, value: false)
163+
}
164+
152165
private func testLocalStorage<T>(saver: (LocalStorageProtocol, T) -> Void,
153166
retriever: (LocalStorageProtocol) -> T?, value: T) where T: Equatable {
154167
let localStorage = LocalStorage(userDefaults: LocalStorageTests.getTestUserDefaults())

0 commit comments

Comments
 (0)