Skip to content

Commit

Permalink
fix: Fix OOM error when a bad api key is passed (#210)
Browse files Browse the repository at this point in the history
* fix: Fix OOM error when a bad api key is passed
  • Loading branch information
izaaz authored Aug 8, 2024
1 parent f4842e7 commit 8f1c047
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FileResponseHandler(
removeCallbackByInsertId(eventsString)
throw e
}
if (eventsList.size == 1) {
if (eventsList.size == 1 || badRequestResponse.isInvalidApiKeyResponse()) {
triggerEventsCallback(eventsList, HttpStatus.BAD_REQUEST.code, badRequestResponse.error)
storage.removeFile(eventFilePath)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class InMemoryResponseHandler(

override fun handleBadRequestResponse(badRequestResponse: BadRequestResponse, events: Any, eventsString: String) {
val eventsList = events as List<BaseEvent>
if (eventsList.size == 1) {
if (eventsList.size == 1 || badRequestResponse.isInvalidApiKeyResponse()) {
triggerEventsCallback(eventsList, HttpStatus.BAD_REQUEST.code, badRequestResponse.error)
return
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/amplitude/core/utilities/Response.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class BadRequestResponse(response: JSONObject) : Response {
return false
}
}

fun isInvalidApiKeyResponse(): Boolean {
return error.lowercase().contains("invalid api key")
}
}

class PayloadTooLargeResponse(response: JSONObject) : Response {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.amplitude.core.utilities

import com.amplitude.core.Configuration
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.EventPipeline
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.json.JSONObject
import org.junit.jupiter.api.Test

class FileResponseHandlerTest {
@Test
fun testBadResponseHandlerForInvalidApiKey() {
val response = BadRequestResponse(
JSONObject("{\"error\":\"Invalid API key\"}")
)
val storage = mockk<EventsFileStorage>()
val pipeline = mockk<EventPipeline>()
val handler =
FileResponseHandler(storage, pipeline, Configuration("test"), mockk(), mockk(), null)

every {
storage.removeFile("file_path")
} returns true

handler.handleBadRequestResponse(
response,
"file_path",
JSONUtil.eventsToString(
listOf(generateBaseEvent("test1"), generateBaseEvent("test2"))
)
)

verify(exactly = 1) {
storage.removeFile("file_path")
}
}

private fun generateBaseEvent(eventType: String): BaseEvent {
val baseEvent = BaseEvent()
baseEvent.eventType = eventType
return baseEvent
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.amplitude.core.utilities

import com.amplitude.core.Configuration
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.EventPipeline
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.json.JSONObject
import org.junit.jupiter.api.Test

class InMemoryResponseHandlerTest {
@Test
fun testBadResponseHandlerForInvalidApiKey() {
val response = BadRequestResponse(
JSONObject("{\"error\":\"Invalid API key\"}")
)
val storage = mockk<EventsFileStorage>()
val pipeline = mockk<EventPipeline>()
val handler =
InMemoryResponseHandler(pipeline, Configuration("test"), mockk(), mockk())

every {
storage.removeFile("file_path")
} returns true

handler.handleBadRequestResponse(
response,
listOf(generateBaseEvent("test1"), generateBaseEvent("test2")),
""
)

verify(exactly = 0) {
pipeline.put(any())
}
}

private fun generateBaseEvent(eventType: String): BaseEvent {
val baseEvent = BaseEvent()
baseEvent.eventType = eventType
return baseEvent
}
}

0 comments on commit 8f1c047

Please sign in to comment.