-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathNetworkSessionTests.swift
More file actions
125 lines (101 loc) · 4.33 KB
/
Copy pathNetworkSessionTests.swift
File metadata and controls
125 lines (101 loc) · 4.33 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
//
// NetworkSessionTests.swift
//
//
// Created by Noah Durell on 11/18/22.
//
@_spi(KlaviyoPrivate) @testable import KlaviyoCore
import SnapshotTesting
import XCTest
@MainActor
class NetworkSessionTests: XCTestCase {
override func setUpWithError() throws {
environment = KlaviyoEnvironment.test()
}
func testDefaultUserAgent() {
assertSnapshot(matching: NetworkSession.defaultUserAgent, as: .dump)
}
func testCreateEmphemeralSesionHeaders() {
assertSnapshot(matching: createEmphemeralSession().configuration.httpAdditionalHeaders, as: .dump)
}
func testSessionDataTask() async throws {
URLProtocolOverrides.protocolClasses = [SimpleMockURLProtocol.self]
let session = NetworkSession.production
let sampleRequest = KlaviyoRequest(endpoint: .registerPushToken("foo", .test))
let attemptInfo = try RequestAttemptInfo(attemptNumber: 1, maxAttempts: 50)
let (data, response) = try await session.data(sampleRequest.urlRequest(attemptInfo: attemptInfo))
assertSnapshot(matching: data, as: .dump)
assertSnapshot(matching: response, as: .dump)
}
func testGetPluginConfigurationWithValidPlist() {
// Create a temporary plist file
let tempDir = FileManager.default.temporaryDirectory
let plistURL = tempDir.appendingPathComponent("klaviyo-plugin-configuration.plist")
// Create plist content
let plistContent: [String: Any] = [
"klaviyo_sdk_plugin_name_override": "test-plugin",
"klaviyo_sdk_plugin_version_override": "1.0.0"
]
do {
// Write plist to temporary location
let plistData = try PropertyListSerialization.data(
fromPropertyList: plistContent,
format: .xml,
options: 0
)
try plistData.write(to: plistURL)
// Create a mock bundle that returns our temporary plist
let mockBundle = MockBundle(plistURL: plistURL)
// Call the function with our mock bundle
let result = NetworkSession.defaultUserAgent(bundle: mockBundle)
// Verify the result
XCTAssertNotNil(result)
XCTAssertEqual(result, "FooApp/1.2.3 (com.klaviyo.fooapp; build:1; iOS 1.1.1) klaviyo-swift/5.2.2 (test-plugin/1.0.0)")
// Clean up
try FileManager.default.removeItem(at: plistURL)
} catch {
XCTFail("Failed to create test plist: \(error)")
}
}
func testGetPluginConfigurationWithMissingPlist() {
// Create a mock bundle that returns nil for the plist URL
let mockBundle = MockBundle(plistURL: nil)
// Call the function with our mock bundle
let result = NetworkSession.defaultUserAgent(bundle: mockBundle)
XCTAssertEqual(result, "FooApp/1.2.3 (com.klaviyo.fooapp; build:1; iOS 1.1.1) klaviyo-swift/5.2.2")
}
func testGetPluginConfigurationWithInvalidPlist() {
// Create a temporary plist file with invalid content
let tempDir = FileManager.default.temporaryDirectory
let plistURL = tempDir.appendingPathComponent("klaviyo-plugin-configuration.plist")
do {
// Write invalid data to the plist
let invalidData = "invalid plist data".data(using: .utf8)!
try invalidData.write(to: plistURL)
// Create a mock bundle that returns our temporary plist
let mockBundle = MockBundle(plistURL: plistURL)
// Call the function with our mock bundle
let result = NetworkSession.defaultUserAgent(bundle: mockBundle)
// Verify the result is default
XCTAssertEqual(result, "FooApp/1.2.3 (com.klaviyo.fooapp; build:1; iOS 1.1.1) klaviyo-swift/5.2.2")
// Clean up
try FileManager.default.removeItem(at: plistURL)
} catch {
XCTFail("Failed to create test plist: \(error)")
}
}
}
// Mock Bundle class for testing
private class MockBundle: Bundle {
private let mockPlistURL: URL?
init(plistURL: URL?) {
mockPlistURL = plistURL
super.init()
}
override func url(forResource name: String?, withExtension ext: String?) -> URL? {
if name == "klaviyo-plugin-configuration" && ext == "plist" {
return mockPlistURL
}
return nil
}
}