Skip to content

Commit 459f25b

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add hash code to request query parameter (#35)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent d559234 commit 459f25b

File tree

5 files changed

+21
-35
lines changed

5 files changed

+21
-35
lines changed

Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
6666

6767
func toJson() -> String {
6868
var event = JsonObject()
69-
event["hashCode"] = ""
7069
event["unique_id"] = uniqueId
7170
event["event_type"] = eventType
7271
event["event_id"] = eventId
@@ -94,8 +93,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
9493
event["app_title"] = systemInfo.appTitle
9594
event["user"] = userAttributes
9695
event["attributes"] = getAttributeObject(from: attributes)
97-
let eventJson = getJsonStringFromObject(jsonObject: event)
98-
event["hashCode"] = eventJson.hashCode()
9996
return getJsonStringFromObject(jsonObject: event)
10097
}
10198

Sources/Clickstream/Network/NetRequest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ enum NetRequest {
3232
URLQueryItem(name: "platform", value: "iOS"),
3333
URLQueryItem(name: "appId", value: configuration.appId),
3434
URLQueryItem(name: "compression", value: compression),
35-
URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId))
35+
URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId)),
36+
URLQueryItem(name: "hashCode", value: requestData.hashCode())
3637
]
3738

3839
var request = URLRequest(url: urlComponts.url!, timeoutInterval: 15.0)

Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class AnalyticsClientTest: XCTestCase {
104104
for i in 0 ..< 501 {
105105
analyticsClient.addGlobalAttribute("value", forKey: "name\(i)")
106106
}
107-
Thread.sleep(forTimeInterval: 0.1)
107+
Thread.sleep(forTimeInterval: 0.2)
108108
XCTAssertEqual(Event.PresetEvent.CLICKSTREAM_ERROR, eventRecorder.lastSavedEvent?.eventType)
109109
XCTAssertEqual(Event.ErrorCode.ATTRIBUTE_SIZE_EXCEED, eventRecorder.lastSavedEvent?.attribute(forKey: Event.ReservedAttribute.ERROR_CODE) as! Int)
110110
let errorValue = eventRecorder.lastSavedEvent?.attribute(forKey: Event.ReservedAttribute.ERROR_MESSAGE) as! String

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class AutoRecordEventClientTest: XCTestCase {
139139
window.makeKeyAndVisible()
140140

141141
autoRecordEventClient.updateLastScreenStartTimestamp(Date().millisecondsSince1970 - 1_100)
142-
142+
Thread.sleep(forTimeInterval: 0.02)
143143
window.rootViewController = viewControllerB
144144
window.makeKeyAndVisible()
145145
XCTAssertTrue(viewControllerA.viewDidAppearCalled)

Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)