@@ -41,17 +41,41 @@ class InitializeSDKTests: XCTestCase {
41
41
42
42
override func tearDownWithError( ) throws {
43
43
try super. tearDownWithError ( )
44
- MockURLProtocol . removeAll ( )
45
44
#if !os(Linux) && !os(Android)
46
45
try KeychainStore . shared. deleteAll ( )
47
46
if let identifier = Bundle . main. bundleIdentifier {
48
47
try KeychainStore ( service: " \( identifier) .com.parse.sdk " ) . deleteAll ( )
49
48
}
49
+ URLSession . shared. configuration. urlCache? . removeAllCachedResponses ( )
50
50
#endif
51
51
try ParseStorage . shared. deleteAll ( )
52
52
}
53
53
54
54
#if !os(Linux) && !os(Android)
55
+ func addCachedResponse( ) {
56
+ if URLSession . parse. configuration. urlCache == nil {
57
+ URLSession . parse. configuration. urlCache = . init( )
58
+ }
59
+ guard let server = URL ( string: " http://parse.com " ) ,
60
+ let data = " Test " . data ( using: . utf8) else {
61
+ XCTFail ( " Should have unwrapped " )
62
+ return
63
+ }
64
+
65
+ let response = URLResponse ( url: server, mimeType: nil ,
66
+ expectedContentLength: data. count,
67
+ textEncodingName: nil )
68
+ URLSession . parse. configuration. urlCache?
69
+ . storeCachedResponse ( . init( response: response,
70
+ data: data) ,
71
+ for: . init( url: server) )
72
+ guard let currentCache = URLSession . parse. configuration. urlCache else {
73
+ XCTFail ( " Should have unwrapped " )
74
+ return
75
+ }
76
+ XCTAssertTrue ( currentCache. currentMemoryUsage > 0 )
77
+ }
78
+ /*
55
79
func testDeleteKeychainOnFirstRun() throws {
56
80
let memory = InMemoryKeyValueStore()
57
81
ParseStorage.shared.use(memory)
@@ -65,42 +89,57 @@ class InitializeSDKTests: XCTestCase {
65
89
let key = "Hello"
66
90
let value = "World"
67
91
try KeychainStore.shared.set(value, for: key)
92
+ addCachedResponse()
68
93
69
94
// Keychain should contain value on first run
70
95
ParseSwift.deleteKeychainIfNeeded()
71
- let storedValue : String ? = try KeychainStore . shared. get ( valueFor: key)
72
- XCTAssertEqual ( storedValue, value)
73
- guard let firstRun = UserDefaults . standard. object ( forKey: ParseConstants . bundlePrefix) as? String else {
74
- XCTFail ( " Should have unwrapped " )
75
- return
76
- }
77
- XCTAssertEqual ( firstRun, ParseConstants . bundlePrefix)
78
-
79
- // Keychain should remain unchanged on 2+ runs
80
- ParseSwift . configuration. deleteKeychainIfNeeded = true
81
- ParseSwift . deleteKeychainIfNeeded ( )
82
- let storedValue2 : String ? = try KeychainStore . shared. get ( valueFor: key)
83
- XCTAssertEqual ( storedValue2, value)
84
- guard let firstRun2 = UserDefaults . standard. object ( forKey: ParseConstants . bundlePrefix) as? String else {
85
- XCTFail ( " Should have unwrapped " )
86
- return
87
- }
88
- XCTAssertEqual ( firstRun2, ParseConstants . bundlePrefix)
89
96
90
- // Keychain should delete on first run
91
- UserDefaults . standard. removeObject ( forKey: ParseConstants . bundlePrefix)
92
- UserDefaults . standard. synchronize ( )
93
- let firstRun3 = UserDefaults . standard. object ( forKey: ParseConstants . bundlePrefix) as? String
94
- XCTAssertNil ( firstRun3)
95
- ParseSwift . deleteKeychainIfNeeded ( )
96
- let storedValue3 : String ? = try KeychainStore . shared. get ( valueFor: key)
97
- XCTAssertNil ( storedValue3)
98
- guard let firstRun4 = UserDefaults . standard. object ( forKey: ParseConstants . bundlePrefix) as? String else {
99
- XCTFail ( " Should have unwrapped " )
100
- return
97
+ do {
98
+ let storedValue: String? = try KeychainStore.shared.get(valueFor: key)
99
+ XCTAssertEqual(storedValue, value)
100
+ guard let firstRun = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String else {
101
+ XCTFail("Should have unwrapped")
102
+ return
103
+ }
104
+ XCTAssertEqual(firstRun, ParseConstants.bundlePrefix)
105
+
106
+ // Keychain should remain unchanged on 2+ runs
107
+ ParseSwift.configuration.deleteKeychainIfNeeded = true
108
+ ParseSwift.deleteKeychainIfNeeded()
109
+ let storedValue2: String? = try KeychainStore.shared.get(valueFor: key)
110
+ XCTAssertEqual(storedValue2, value)
111
+ guard let firstRun2 = UserDefaults.standard
112
+ .object(forKey: ParseConstants.bundlePrefix) as? String else {
113
+ XCTFail("Should have unwrapped")
114
+ return
115
+ }
116
+ XCTAssertEqual(firstRun2, ParseConstants.bundlePrefix)
117
+
118
+ // Keychain should delete on first run
119
+ UserDefaults.standard.removeObject(forKey: ParseConstants.bundlePrefix)
120
+ UserDefaults.standard.synchronize()
121
+ let firstRun3 = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String
122
+ XCTAssertNil(firstRun3)
123
+ addCachedResponse()
124
+ ParseSwift.deleteKeychainIfNeeded()
125
+ let storedValue3: String? = try KeychainStore.shared.get(valueFor: key)
126
+ XCTAssertNil(storedValue3)
127
+ guard let firstRun4 = UserDefaults.standard
128
+ .object(forKey: ParseConstants.bundlePrefix) as? String else {
129
+ XCTFail("Should have unwrapped")
130
+ return
131
+ }
132
+ XCTAssertEqual(firstRun4, ParseConstants.bundlePrefix)
133
+
134
+ guard let currentCache = URLSession.parse.configuration.urlCache else {
135
+ XCTFail("Should have unwrapped")
136
+ return
137
+ }
138
+ XCTAssertTrue(currentCache.currentMemoryUsage == 0)
139
+ } catch {
140
+ XCTFail("\(error)")
101
141
}
102
- XCTAssertEqual ( firstRun4, ParseConstants . bundlePrefix)
103
- }
142
+ }*/
104
143
#endif
105
144
106
145
func testCreateParseInstallationOnInit( ) {
@@ -151,25 +190,27 @@ class InitializeSDKTests: XCTestCase {
151
190
Installation . saveCurrentContainerToKeychain ( )
152
191
ParseVersion . current = ParseConstants . version
153
192
193
+ var foundInstallation = Installation ( )
194
+ foundInstallation. updateAutomaticInfo ( )
195
+ foundInstallation. objectId = " yarr "
196
+ foundInstallation. installationId = installationId
197
+
198
+ let results = QueryResponse < Installation > ( results: [ foundInstallation] , count: 1 )
199
+ MockURLProtocol . mockRequests { _ in
200
+ do {
201
+ let encoded = try ParseCoding . jsonEncoder ( ) . encode ( results)
202
+ return MockURLResponse ( data: encoded, statusCode: 200 , delay: 0.0 )
203
+ } catch {
204
+ return nil
205
+ }
206
+ }
207
+
154
208
let expectation1 = XCTestExpectation ( description: " Wait " )
155
209
DispatchQueue . main. asyncAfter ( deadline: . now( ) + 2 ) {
156
- var foundInstallation = Installation ( )
157
- foundInstallation. updateAutomaticInfo ( )
158
- foundInstallation. objectId = " yarr "
159
- foundInstallation. installationId = installationId
160
-
161
- let results = QueryResponse < Installation > ( results: [ foundInstallation] , count: 1 )
162
- MockURLProtocol . mockRequests { _ in
163
- do {
164
- let encoded = try ParseCoding . jsonEncoder ( ) . encode ( results)
165
- return MockURLResponse ( data: encoded, statusCode: 200 , delay: 0.0 )
166
- } catch {
167
- return nil
168
- }
169
- }
170
210
171
211
guard let url = URL ( string: " http://localhost:1337/1 " ) else {
172
212
XCTFail ( " Should create valid URL " )
213
+ expectation1. fulfill ( )
173
214
return
174
215
}
175
216
@@ -186,7 +227,7 @@ class InitializeSDKTests: XCTestCase {
186
227
return
187
228
}
188
229
189
- XCTAssertEqual ( currentInstallation, foundInstallation )
230
+ XCTAssertEqual ( currentInstallation. installationId , installationId )
190
231
191
232
// Should be in Keychain
192
233
guard let memoryInstallation: CurrentInstallationContainer < Installation >
@@ -197,15 +238,15 @@ class InitializeSDKTests: XCTestCase {
197
238
}
198
239
XCTAssertEqual ( memoryInstallation. currentInstallation, currentInstallation)
199
240
200
- #if !os(Linux) && !os(Android)
201
241
// Should be in Keychain
202
242
guard let keychainInstallation: CurrentInstallationContainer < Installation >
203
243
= try ? KeychainStore . shared. get ( valueFor: ParseStorage . Keys. currentInstallation) else {
204
244
XCTFail ( " Should get object from Keychain " )
245
+ expectation1. fulfill ( )
205
246
return
206
247
}
207
248
XCTAssertEqual ( keychainInstallation. currentInstallation, currentInstallation)
208
- #endif
249
+ MockURLProtocol . removeAll ( )
209
250
expectation1. fulfill ( )
210
251
}
211
252
wait ( for: [ expectation1] , timeout: 20.0 )
0 commit comments