@@ -142,8 +142,80 @@ class TaskProcessorTests: XCTestCase {
142142 try persistenceProvider. mainQueueContext ( ) . save ( )
143143 wait ( for: [ expectation1] , timeout: 15.0 )
144144 }
145+
146+ func testSentAtInHeader( ) throws {
147+ let expectation1 = expectation ( description: #function)
148+ let task = try createSampleTask ( ) !
149+ let date = Date ( )
150+ let sentAtTime = " \( Int ( date. timeIntervalSince1970 * 1000 ) ) "
151+ let dateProvider = MockDateProvider ( )
152+ dateProvider. currentDate = date
153+
154+ let networkSession = MockNetworkSession ( )
155+ networkSession. requestCallback = { request in
156+ if request. allHTTPHeaderFields!. contains ( where: { $0. key == " sentAt " && $0. value == sentAtTime } ) {
157+ expectation1. fulfill ( )
158+ }
159+ }
160+
161+ // process data
162+ let processor = IterableAPICallTaskProcessor ( networkSession: networkSession, dateProvider: dateProvider)
163+ try processor. process ( task: task)
164+ . onSuccess { taskResult in
165+ switch taskResult {
166+ case . success( detail: _) :
167+ break
168+ case . failureWithNoRetry( detail: _) :
169+ XCTFail ( " not expecting failure with no retry " )
170+ case . failureWithRetry( retryAfter: _, detail: _) :
171+ XCTFail ( " not expecting failure with retry " )
172+ }
173+ }
174+ . onError { _ in
175+ XCTFail ( )
176+ }
145177
146- private func createSampleTask( ) throws -> IterableTask ? {
178+ try persistenceProvider. mainQueueContext ( ) . delete ( task: task)
179+ try persistenceProvider. mainQueueContext ( ) . save ( )
180+ wait ( for: [ expectation1] , timeout: 5.0 )
181+ }
182+
183+ func testCreatedAtInBody( ) throws {
184+ let expectation1 = expectation ( description: #function)
185+ let date = Date ( )
186+ let createdAtTime = Int ( date. timeIntervalSince1970 * 1000 )
187+ let task = try createSampleTask ( scheduledAt: date) !
188+
189+ let networkSession = MockNetworkSession ( )
190+ networkSession. requestCallback = { request in
191+ if request. bodyDict. contains ( where: { $0. key == " createdAt " && ( $0. value as! Int ) == createdAtTime } ) {
192+ expectation1. fulfill ( )
193+ }
194+ }
195+
196+ // process data
197+ let processor = IterableAPICallTaskProcessor ( networkSession: networkSession)
198+ try processor. process ( task: task)
199+ . onSuccess { taskResult in
200+ switch taskResult {
201+ case . success( detail: _) :
202+ break
203+ case . failureWithNoRetry( detail: _) :
204+ XCTFail ( " not expecting failure with no retry " )
205+ case . failureWithRetry( retryAfter: _, detail: _) :
206+ XCTFail ( " not expecting failure with retry " )
207+ }
208+ }
209+ . onError { _ in
210+ XCTFail ( )
211+ }
212+
213+ try persistenceProvider. mainQueueContext ( ) . delete ( task: task)
214+ try persistenceProvider. mainQueueContext ( ) . save ( )
215+ wait ( for: [ expectation1] , timeout: 5.0 )
216+ }
217+
218+ private func createSampleTask( scheduledAt: Date = Date ( ) , requestedAt: Date = Date ( ) ) throws -> IterableTask ? {
147219 let apiKey = " test-api-key "
148220149221 let eventName = " CustomEvent1 "
@@ -169,9 +241,9 @@ class TaskProcessorTests: XCTestCase {
169241 let taskId = IterableUtil . generateUUID ( )
170242 try persistenceProvider. mainQueueContext ( ) . create ( task: IterableTask ( id: taskId,
171243 type: . apiCall,
172- scheduledAt: Date ( ) ,
244+ scheduledAt: scheduledAt ,
173245 data: data,
174- requestedAt: Date ( ) ) )
246+ requestedAt: requestedAt ) )
175247 try persistenceProvider. mainQueueContext ( ) . save ( )
176248
177249 return try persistenceProvider. mainQueueContext ( ) . findTask ( withId: taskId)
0 commit comments