diff --git a/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift b/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift index 09bdf20..605be56 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift @@ -66,7 +66,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel { func toJson() -> String { var event = JsonObject() - event["hashCode"] = "" event["unique_id"] = uniqueId event["event_type"] = eventType event["event_id"] = eventId @@ -94,8 +93,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel { event["app_title"] = systemInfo.appTitle event["user"] = userAttributes event["attributes"] = getAttributeObject(from: attributes) - let eventJson = getJsonStringFromObject(jsonObject: event) - event["hashCode"] = eventJson.hashCode() return getJsonStringFromObject(jsonObject: event) } diff --git a/Sources/Clickstream/Network/NetRequest.swift b/Sources/Clickstream/Network/NetRequest.swift index 48ac73c..4647d73 100644 --- a/Sources/Clickstream/Network/NetRequest.swift +++ b/Sources/Clickstream/Network/NetRequest.swift @@ -32,7 +32,8 @@ enum NetRequest { URLQueryItem(name: "platform", value: "iOS"), URLQueryItem(name: "appId", value: configuration.appId), URLQueryItem(name: "compression", value: compression), - URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId)) + URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId)), + URLQueryItem(name: "hashCode", value: requestData.hashCode()) ] var request = URLRequest(url: urlComponts.url!, timeoutInterval: 15.0) diff --git a/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift b/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift index f21c575..ca779e1 100644 --- a/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift +++ b/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift @@ -104,7 +104,7 @@ class AnalyticsClientTest: XCTestCase { for i in 0 ..< 501 { analyticsClient.addGlobalAttribute("value", forKey: "name\(i)") } - Thread.sleep(forTimeInterval: 0.1) + Thread.sleep(forTimeInterval: 0.2) XCTAssertEqual(Event.PresetEvent.CLICKSTREAM_ERROR, eventRecorder.lastSavedEvent?.eventType) XCTAssertEqual(Event.ErrorCode.ATTRIBUTE_SIZE_EXCEED, eventRecorder.lastSavedEvent?.attribute(forKey: Event.ReservedAttribute.ERROR_CODE) as! Int) let errorValue = eventRecorder.lastSavedEvent?.attribute(forKey: Event.ReservedAttribute.ERROR_MESSAGE) as! String diff --git a/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift b/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift index c2bd9ea..721d7aa 100644 --- a/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift +++ b/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift @@ -139,7 +139,7 @@ class AutoRecordEventClientTest: XCTestCase { window.makeKeyAndVisible() autoRecordEventClient.updateLastScreenStartTimestamp(Date().millisecondsSince1970 - 1_100) - + Thread.sleep(forTimeInterval: 0.02) window.rootViewController = viewControllerB window.makeKeyAndVisible() XCTAssertTrue(viewControllerA.viewDidAppearCalled) diff --git a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift index 5ea37d9..645e7c2 100644 --- a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift +++ b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift @@ -15,6 +15,7 @@ class EventRecorderTest: XCTestCase { let testSuccessEndpoint = "http://localhost:8080/collect" let testFailEndpoint = "http://localhost:8080/collect/fail" let testSuccessWithDelayEndpoint = "http://localhost:8080/collect/success/delay" + let testHashCodeEndpoint = "http://localhost:8080/collect/hashcode" var dbUtil: ClickstreamDBProtocol! var clickstreamEvent: ClickstreamEvent! var eventRecorder: EventRecorder! @@ -107,7 +108,7 @@ class EventRecorderTest: XCTestCase { func testGetEventWithAllAttribute() throws { try eventRecorder.save(clickstreamEvent) let event = try eventRecorder.getBatchEvent().eventsJson.jsonArray()[0] - XCTAssertNotNil(event["hashCode"]) + XCTAssertNil(event["hashCode"]) XCTAssertEqual(clickstream.userUniqueId, event["unique_id"] as! String) XCTAssertEqual("testEvent", event["event_type"] as! String) XCTAssertNotNil(event["event_id"]) @@ -413,34 +414,21 @@ class EventRecorderTest: XCTestCase { XCTAssertTrue(eventRecorder.queue.operationCount > 0) } - func testGetEventHashCodeTwice() { - let eventJson = clickstreamEvent.toJson() - let hashCode1 = eventJson.hashCode() - let hashCode2 = eventJson.hashCode() - XCTAssertEqual(hashCode1, hashCode2) - } - - func testEventModified() throws { - let originJson = clickstreamEvent.toJson() - let originJsonData = originJson.data(using: .utf8)! - var jsonObject = try JSONSerialization.jsonObject(with: originJsonData, options: []) as! [String: Any] - let originHashCode = jsonObject["hashCode"] as! String - jsonObject["hashCode"] = "" - jsonObject["event_type"] = "testEvent1" - let jsonWithoutHashCode = clickstreamEvent.getJsonStringFromObject(jsonObject: jsonObject) - let computedHashCode = jsonWithoutHashCode.hashCode() - XCTAssertNotEqual(originHashCode, computedHashCode) - } - - func testEventNotModified() throws { - let originJson = clickstreamEvent.toJson() - let originJsonData = originJson.data(using: .utf8)! - var jsonObject = try JSONSerialization.jsonObject(with: originJsonData, options: []) as! [String: Any] - let originHashCode = jsonObject["hashCode"] as! String - jsonObject["hashCode"] = "" - let jsonWithoutHashCode = clickstreamEvent.getJsonStringFromObject(jsonObject: jsonObject) - let computedHashCode = jsonWithoutHashCode.hashCode() - XCTAssertEqual(originHashCode, computedHashCode) + func testVerifyHashCodeInRequestParameter() { + clickstream.configuration.endpoint = testHashCodeEndpoint + let eventJson = "[" + clickstreamEvent.toJson() + "]" + let eventJsonHashCode = eventJson.hashCode() + server["/collect/hashcode"] = { request in + let queryParams = request.queryParams + // swift lambda for get the hashCode value in queryParams dictionary + let hashCodeValue = queryParams.first(where: { $0.0 == "hashCode" })?.1 + if hashCodeValue == eventJsonHashCode { + return .ok(.text("Success")) + } + return .badRequest(.text("Fail")) + } + let result = NetRequest.uploadEventWithURLSession(eventsJson: eventJson, configuration: clickstream.configuration, bundleSequenceId: 1) + XCTAssertTrue(result) } }