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

fix: correctly format client_upload_time in HTTP request when minIdLength is set #160

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions core/src/main/java/com/amplitude/core/utilities/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal class HttpClient(
}

internal fun getClientUploadTime(): String {
val currentTimeMillis = System.currentTimeMillis()
val currentTimeMillis = getCurrentTimeMillis()
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.timeZone = TimeZone.getTimeZone("UTC")
return sdf.format(Date(currentTimeMillis))
Expand All @@ -95,6 +95,10 @@ internal class HttpClient(
return configuration.minIdLength
}

internal fun getCurrentTimeMillis(): Long {
return System.currentTimeMillis()
}

fun getInputStream(connection: HttpURLConnection): InputStream {
return try {
connection.inputStream
Expand Down Expand Up @@ -149,7 +153,7 @@ abstract class Connection(
if (minIdLength == null) {
return "{\"api_key\":\"$apiKey\",\"client_upload_time\":\"$clientUploadTime\",\"events\":$events}"
}
return "{\"api_key\":\"$apiKey\",\"client_upload_time\":$clientUploadTime,\"events\":$events,\"options\":{\"min_id_length\":$minIdLength}}"
return "{\"api_key\":\"$apiKey\",\"client_upload_time\":\"$clientUploadTime\",\"events\":$events,\"options\":{\"min_id_length\":$minIdLength}}"
}
}

Expand Down
108 changes: 108 additions & 0 deletions core/src/test/kotlin/com/amplitude/core/utilities/HttpClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.amplitude.core.utilities

import com.amplitude.core.Configuration
import com.amplitude.core.events.BaseEvent
import io.mockk.every
import io.mockk.spyk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.json.JSONObject
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.util.concurrent.TimeUnit

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class HttpClientTest {
private lateinit var server: MockWebServer
val apiKey = "API_KEY"
val clientUploadTimeMillis = 1699905773000L
val clientUploadTimeString = "2023-11-13T20:02:53.000Z"

@ExperimentalCoroutinesApi
@BeforeEach
fun setup() {
server = MockWebServer()
server.start()
}

@AfterEach
fun shutdown() {
server.shutdown()
}

@Test
fun `test client_upload_time is set on the request`() {
server.enqueue(MockResponse().setBody("{\"code\": \"success\"}"))

val config = Configuration(
apiKey = apiKey,
serverUrl = server.url("/").toString()
)
val event = BaseEvent()
event.eventType = "test"

val httpClient = spyk(HttpClient(config))
every { httpClient.getCurrentTimeMillis() } returns clientUploadTimeMillis

val connection = httpClient.upload()
System.currentTimeMillis()
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved

connection.outputStream?.let {
connection.setEvents(JSONUtil.eventsToString(listOf(event)))
// Upload the payloads.
connection.close()
}

val request: RecordedRequest? = runRequest()
val body = request?.body?.readUtf8()
val result = JSONObject(body)

assertEquals(apiKey, result.getString("api_key"))
assertEquals(clientUploadTimeString, result.getString("client_upload_time"))
}

@Test
fun `test client_upload_time is set correctly when minIdLength is set`() {
server.enqueue(MockResponse().setBody("{\"code\": \"success\"}"))

val config = Configuration(
apiKey = apiKey,
serverUrl = server.url("/").toString(),
minIdLength = 3,
)
val event = BaseEvent()
event.eventType = "test"

val httpClient = spyk(HttpClient(config))
every { httpClient.getCurrentTimeMillis() } returns clientUploadTimeMillis

val connection = httpClient.upload()
System.currentTimeMillis()

connection.outputStream?.let {
connection.setEvents(JSONUtil.eventsToString(listOf(event)))
// Upload the payloads.
connection.close()
}

val request: RecordedRequest? = runRequest()
val body = request?.body?.readUtf8()
val result = JSONObject(body)

assertEquals(apiKey, result.getString("api_key"))
assertEquals(clientUploadTimeString, result.getString("client_upload_time"))
}

private fun runRequest(): RecordedRequest? {
return try {
server.takeRequest(5, TimeUnit.SECONDS)
} catch (e: InterruptedException) {
null
}
}
}
Loading