From a85079575694fc98d64848050cab6aad7424e9f0 Mon Sep 17 00:00:00 2001 From: Xiaowei Zhu <33129495+zhu-xiaowei@users.noreply.github.com> Date: Tue, 12 Mar 2024 21:56:56 +0800 Subject: [PATCH] feat: support calibrate local timestamp (#57) Co-authored-by: xiaoweii --- Sources/Clickstream/Network/NetRequest.swift | 1 + .../Clickstream/EventRecorderTest.swift | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Sources/Clickstream/Network/NetRequest.swift b/Sources/Clickstream/Network/NetRequest.swift index 4647d73..1183b57 100644 --- a/Sources/Clickstream/Network/NetRequest.swift +++ b/Sources/Clickstream/Network/NetRequest.swift @@ -33,6 +33,7 @@ enum NetRequest { URLQueryItem(name: "appId", value: configuration.appId), URLQueryItem(name: "compression", value: compression), URLQueryItem(name: "event_bundle_sequence_id", value: String(describing: bundleSequenceId)), + URLQueryItem(name: "upload_timestamp", value: String(describing: Date().millisecondsSince1970)), URLQueryItem(name: "hashCode", value: requestData.hashCode()) ] diff --git a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift index 7840c2f..f1ebdfd 100644 --- a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift +++ b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift @@ -16,6 +16,7 @@ class EventRecorderTest: XCTestCase { let testFailEndpoint = "http://localhost:8080/collect/fail" let testSuccessWithDelayEndpoint = "http://localhost:8080/collect/success/delay" let testHashCodeEndpoint = "http://localhost:8080/collect/hashcode" + let testUploadTimestampEndpoint = "http://localhost:8080/collect/timestamp" var dbUtil: ClickstreamDBProtocol! var clickstreamEvent: ClickstreamEvent! var eventRecorder: EventRecorder! @@ -426,6 +427,22 @@ class EventRecorderTest: XCTestCase { let result = NetRequest.uploadEventWithURLSession(eventsJson: eventJson, configuration: clickstream.configuration, bundleSequenceId: 1) XCTAssertTrue(result) } + + func testVerifyUploadTimestampInRequestParameter() { + clickstream.configuration.endpoint = testUploadTimestampEndpoint + server["/collect/timestamp"] = { request in + let queryParams = request.queryParams + // swift lambda for get the upload_timestamp value in queryParams dictionary + let uploadTimestamp = queryParams.first(where: { $0.0 == "upload_timestamp" })?.1 + let requestUploadTimestamp = Int64(uploadTimestamp!)! + if Date().millisecondsSince1970 - requestUploadTimestamp < 5_000 { + return .ok(.text("Success")) + } + return .badRequest(.text("Fail")) + } + let result = NetRequest.uploadEventWithURLSession(eventsJson: "[]", configuration: clickstream.configuration, bundleSequenceId: 1) + XCTAssertTrue(result) + } } extension String {