Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hash code to request query parameter #35

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
zhu-xiaowei marked this conversation as resolved.
Show resolved Hide resolved
]

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