Skip to content

Commit

Permalink
Remove query params from network logging (#4155)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored Oct 24, 2024
1 parent e80585f commit 53d4c4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class ResultCall<T>(

private fun Throwable.toFailure(): Result<T> =
this
.also { Timber.w(it, "Network Error: ${backingCall.request().url}") }
.also {
// We rebuild the URL without query params, we do not want to log those
val url = backingCall.request().url.toUrl().run { "$protocol://$authority$path" }
Timber.w(it, "Network Error: $url")
}
.asFailure()

private fun Response<T>.toResult(): Result<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.network.util
import com.x8bit.bitwarden.data.platform.util.asSuccess
import io.mockk.every
import io.mockk.mockk
import okhttp3.HttpUrl
import okhttp3.Request
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.jupiter.api.Assertions.assertEquals
Expand All @@ -11,14 +12,13 @@ import org.junit.jupiter.api.Test
import retrofit2.Call
import retrofit2.Response
import java.io.IOException
import java.net.URL

class CallExtensionsTest {

@Test
fun `executeForResult returns failure when execute throws IOException`() {
val request = mockk<Request> {
every { url } returns mockk()
}
val request = createMockkRequest()
val call = mockk<Call<Unit>> {
every { request() } returns request
every { execute() } throws IOException("Fail")
Expand All @@ -31,9 +31,7 @@ class CallExtensionsTest {

@Test
fun `executeForResult returns failure when execute throws RuntimeException`() {
val request = mockk<Request> {
every { url } returns mockk()
}
val request = createMockkRequest()
val call = mockk<Call<Unit>> {
every { request() } returns request
every { execute() } throws RuntimeException("Fail")
Expand All @@ -46,9 +44,7 @@ class CallExtensionsTest {

@Test
fun `executeForResult returns failure when response is failure`() {
val request = mockk<Request> {
every { url } returns mockk()
}
val request = createMockkRequest()
val call = mockk<Call<Unit>> {
every { request() } returns request
every { execute() } returns Response.error(400, "".toResponseBody())
Expand All @@ -69,4 +65,18 @@ class CallExtensionsTest {

assertEquals(Unit.asSuccess(), result)
}

private fun createMockkRequest(): Request {
val mockkUrl = mockk<URL> {
every { protocol } returns "http"
every { authority } returns "bitwarden.com"
every { path } returns "/example/path"
}
val mockkHttpUrl = mockk<HttpUrl> {
every { toUrl() } returns mockkUrl
}
return mockk<Request> {
every { url } returns mockkHttpUrl
}
}
}

0 comments on commit 53d4c4c

Please sign in to comment.