Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions library/src/androidTest/java/com/paypal/messages/io/ApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,29 @@ class ApiTest {
}

@Test
fun testCreateLoggerRequest() {
val request = Api.createLoggerRequest("{}")
fun testCreateLoggerRequestWithClientId() {
val request = Api.createLoggerRequest("""{"data": {"client_id": "mockClientId"}}""")
val expectedPath = "v1/credit/upstream-messaging-events"

assertTrue(!request.header("Authorization").isNullOrEmpty())
assertTrue(request.url.toString().contains(expectedPath))
}

@Test
fun testCreateLoggerRequestWithNoClientId() {
val request = Api.createLoggerRequest("""{"data": {}}""")
val expectedPath = "v1/credit/upstream-messaging-events"

assertTrue(request.header("Authorization")!!.contains("Basic null"))
assertTrue(request.url.toString().contains(expectedPath))
}

@Test
fun testCreateLoggerRequestWithNoData() {
val request = Api.createLoggerRequest("""{}""")
val expectedPath = "v1/credit/upstream-messaging-events"

assertTrue(request.header("Authorization")!!.contains("Basic null"))
assertTrue(request.url.toString().contains(expectedPath))
}

Expand Down
16 changes: 15 additions & 1 deletion library/src/main/java/com/paypal/messages/io/Api.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.paypal.messages.io

import android.content.Context
import android.util.Base64
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.paypal.messages.BuildConfig
Expand Down Expand Up @@ -230,12 +231,25 @@ object Api {
}

internal fun createLoggerRequest(json: String): Request {
val jsonObject = JSONObject(json)

// Create authorization header for logs: Basic <base64_client_id>
val encodedClientId = runCatching {
jsonObject.getJSONObject("data")
.getString("client_id")
.toByteArray(charset = Charsets.UTF_8)
.let { Base64.encodeToString(it, Base64.NO_WRAP) }
}.onFailure { error ->
LogCat.error(TAG, "Error processing client_id: ${error.message}")
}.getOrNull()

val request = Request.Builder().apply {
header("Authorization", "Basic $encodedClientId")
url(env.url(Env.Endpoints.LOGGER))
post(json.toRequestBody("application/json".toMediaType()))
}.build()

val jsonNoFdata = JSONObject(json).toString(2)
val jsonNoFdata = jsonObject.toString(2)
.replace(""""fdata":.*?",""".toRegex(), "")
LogCat.debug(TAG, "createLoggerRequest: $request\npayloadJson: $jsonNoFdata")
return request
Expand Down