Skip to content

Commit

Permalink
feat: add client upload time
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Oct 18, 2023
1 parent 746d433 commit 22127d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/Amplitude/Utilities/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ class HttpClient {

func getRequestData(events: String) -> Data? {
let apiKey = configuration.apiKey
let clientUploadTime: String = ISO8601DateFormatter().string(from: Date())
var requestPayload = """
{"api_key":"\(apiKey)","events":\(events)
{"api_key":"\(apiKey)","client_upload_time":"\(clientUploadTime)","events":\(events)
"""
if let minIdLength = configuration.minIdLength {
requestPayload += """
Expand Down
17 changes: 17 additions & 0 deletions Tests/AmplitudeTests/Utilities/HttpClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ final class HttpClientTests: XCTestCase {
}
}

func testGetRequestData() {
let httpClient = HttpClient(configuration: configuration)
let event = BaseEvent(userId: "unit-test user", eventType: "unit-test event")

let dateFormatter = ISO8601DateFormatter()
let currentDate = Date()
let expectedClientUploadTime = dateFormatter.string(from: currentDate)

let expectedRequestPayload = """
{"api_key":"testApiKey","client_upload_time":"\(expectedClientUploadTime)","events":[\(event.toString())]}
""".data(using: .utf8)

let result = httpClient.getRequestData(events: "[\(event.toString())]")

XCTAssertEqual(result, expectedRequestPayload)
}

func testUploadWithInvalidApiKey() {
// TODO: currently this test is sending request to real Amplitude host, update to mock for better stability
let httpClient = HttpClient(configuration: configuration)
Expand Down

0 comments on commit 22127d6

Please sign in to comment.