@@ -15,6 +15,7 @@ class EventRecorderTest: XCTestCase {
1515 let testSuccessEndpoint = " http://localhost:8080/collect "
1616 let testFailEndpoint = " http://localhost:8080/collect/fail "
1717 let testSuccessWithDelayEndpoint = " http://localhost:8080/collect/success/delay "
18+ let testHashCodeEndpoint = " http://localhost:8080/collect/hashcode "
1819 var dbUtil : ClickstreamDBProtocol !
1920 var clickstreamEvent : ClickstreamEvent !
2021 var eventRecorder : EventRecorder !
@@ -107,7 +108,7 @@ class EventRecorderTest: XCTestCase {
107108 func testGetEventWithAllAttribute( ) throws {
108109 try eventRecorder. save ( clickstreamEvent)
109110 let event = try eventRecorder. getBatchEvent ( ) . eventsJson. jsonArray ( ) [ 0 ]
110- XCTAssertNotNil ( event [ " hashCode " ] )
111+ XCTAssertNil ( event [ " hashCode " ] )
111112 XCTAssertEqual ( clickstream. userUniqueId, event [ " unique_id " ] as! String )
112113 XCTAssertEqual ( " testEvent " , event [ " event_type " ] as! String )
113114 XCTAssertNotNil ( event [ " event_id " ] )
@@ -413,34 +414,21 @@ class EventRecorderTest: XCTestCase {
413414 XCTAssertTrue ( eventRecorder. queue. operationCount > 0 )
414415 }
415416
416- func testGetEventHashCodeTwice( ) {
417- let eventJson = clickstreamEvent. toJson ( )
418- let hashCode1 = eventJson. hashCode ( )
419- let hashCode2 = eventJson. hashCode ( )
420- XCTAssertEqual ( hashCode1, hashCode2)
421- }
422-
423- func testEventModified( ) throws {
424- let originJson = clickstreamEvent. toJson ( )
425- let originJsonData = originJson. data ( using: . utf8) !
426- var jsonObject = try JSONSerialization . jsonObject ( with: originJsonData, options: [ ] ) as! [ String : Any ]
427- let originHashCode = jsonObject [ " hashCode " ] as! String
428- jsonObject [ " hashCode " ] = " "
429- jsonObject [ " event_type " ] = " testEvent1 "
430- let jsonWithoutHashCode = clickstreamEvent. getJsonStringFromObject ( jsonObject: jsonObject)
431- let computedHashCode = jsonWithoutHashCode. hashCode ( )
432- XCTAssertNotEqual ( originHashCode, computedHashCode)
433- }
434-
435- func testEventNotModified( ) throws {
436- let originJson = clickstreamEvent. toJson ( )
437- let originJsonData = originJson. data ( using: . utf8) !
438- var jsonObject = try JSONSerialization . jsonObject ( with: originJsonData, options: [ ] ) as! [ String : Any ]
439- let originHashCode = jsonObject [ " hashCode " ] as! String
440- jsonObject [ " hashCode " ] = " "
441- let jsonWithoutHashCode = clickstreamEvent. getJsonStringFromObject ( jsonObject: jsonObject)
442- let computedHashCode = jsonWithoutHashCode. hashCode ( )
443- XCTAssertEqual ( originHashCode, computedHashCode)
417+ func testVerifyHashCodeInRequestParameter( ) {
418+ clickstream. configuration. endpoint = testHashCodeEndpoint
419+ let eventJson = " [ " + clickstreamEvent. toJson ( ) + " ] "
420+ let eventJsonHashCode = eventJson. hashCode ( )
421+ server [ " /collect/hashcode " ] = { request in
422+ let queryParams = request. queryParams
423+ // swift lambda for get the hashCode value in queryParams dictionary
424+ let hashCodeValue = queryParams. first ( where: { $0. 0 == " hashCode " } ) ? . 1
425+ if hashCodeValue == eventJsonHashCode {
426+ return . ok( . text( " Success " ) )
427+ }
428+ return . badRequest( . text( " Fail " ) )
429+ }
430+ let result = NetRequest . uploadEventWithURLSession ( eventsJson: eventJson, configuration: clickstream. configuration, bundleSequenceId: 1 )
431+ XCTAssertTrue ( result)
444432 }
445433}
446434
0 commit comments