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 client upload time #94

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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())
falconandy marked this conversation as resolved.
Show resolved Hide resolved
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()
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved
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