Skip to content

Commit

Permalink
feat: add hash code to request query parameter (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii committed Oct 19, 2023
1 parent 1fc4d70 commit cd10a7b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/Clickstream/Network/NetRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 17 additions & 29 deletions Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit cd10a7b

Please sign in to comment.