From 7f4676d63ef54435cb00ed5c7fc3576daf597167 Mon Sep 17 00:00:00 2001 From: Farshid Roohi Date: Fri, 9 Feb 2024 16:29:03 +0330 Subject: [PATCH 1/5] Add Host IP address in the overview and sharable --- .../internal/data/entity/HttpTransaction.kt | 3 +++ .../internal/data/room/ChuckerDatabase.kt | 2 +- .../chucker/internal/support/ResponseExt.kt | 17 +++++++++++++ .../internal/support/ResponseProcessor.kt | 1 + .../support/TransactionDetailsSharable.kt | 1 + .../TransactionOverviewFragment.kt | 1 + .../chucker_fragment_transaction_overview.xml | 25 +++++++++++++++++-- library/src/main/res/values/strings.xml | 1 + 8 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/entity/HttpTransaction.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/entity/HttpTransaction.kt index 5d5921d4f..742a81c45 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/entity/HttpTransaction.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/entity/HttpTransaction.kt @@ -38,6 +38,7 @@ internal class HttpTransaction( @ColumnInfo(name = "protocol") var protocol: String?, @ColumnInfo(name = "method") var method: String?, @ColumnInfo(name = "url") var url: String?, + @ColumnInfo(name = "hostIp") var hostIp: String?, @ColumnInfo(name = "host") var host: String?, @ColumnInfo(name = "path") var path: String?, @ColumnInfo(name = "scheme") var scheme: String?, @@ -70,6 +71,7 @@ internal class HttpTransaction( protocol = null, method = null, url = null, + hostIp = null, host = null, path = null, scheme = null, @@ -317,6 +319,7 @@ internal class HttpTransaction( (protocol == other.protocol) && (method == other.method) && (url == other.url) && + (hostIp == other.hostIp) && (host == other.host) && (path == other.path) && (scheme == other.scheme) && diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt index 43f5f29c7..0e0c8d735 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt @@ -6,7 +6,7 @@ import androidx.room.Room import androidx.room.RoomDatabase import com.chuckerteam.chucker.internal.data.entity.HttpTransaction -@Database(entities = [HttpTransaction::class], version = 9, exportSchema = false) +@Database(entities = [HttpTransaction::class], version = 10, exportSchema = false) internal abstract class ChuckerDatabase : RoomDatabase() { abstract fun transactionDao(): HttpTransactionDao diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt new file mode 100644 index 000000000..f77c8378e --- /dev/null +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt @@ -0,0 +1,17 @@ +package com.chuckerteam.chucker.internal.support + +import okhttp3.Response +import java.util.regex.Matcher +import java.util.regex.Pattern + +private const val IP_REGEX = "(?:\\d{1,3}\\.){3}\\d{1,3}" + +public fun Response.getHostIp(): String? { + val body = body?.source()?.readUtf8() + val pattern: Pattern = Pattern.compile(IP_REGEX) + val matcher: Matcher? = body?.let { pattern.matcher(it) } + if (matcher?.find() == true) { + return matcher.group() + } + return null +} diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt index 7a2c4cd9b..83fd989f0 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt @@ -43,6 +43,7 @@ internal class ResponseProcessor( requestDate = response.sentRequestAtMillis responseDate = response.receivedResponseAtMillis protocol = response.protocol.toString() + hostIp = response.getHostIp() responseCode = response.code responseMessage = response.message diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/TransactionDetailsSharable.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/TransactionDetailsSharable.kt index 388b45272..158f9cbbd 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/TransactionDetailsSharable.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/TransactionDetailsSharable.kt @@ -13,6 +13,7 @@ internal class TransactionDetailsSharable( override fun toSharableContent(context: Context): Source = Buffer().apply { writeUtf8("${context.getString(R.string.chucker_url)}: ${transaction.getFormattedUrl(encodeUrls)}\n") + writeUtf8("${context.getString(R.string.chucker_host_ip)}: ${transaction.hostIp}\n") writeUtf8("${context.getString(R.string.chucker_method)}: ${transaction.method}\n") writeUtf8("${context.getString(R.string.chucker_protocol)}: ${transaction.protocol}\n") writeUtf8("${context.getString(R.string.chucker_status)}: ${transaction.status}\n") diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/ui/transaction/TransactionOverviewFragment.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/ui/transaction/TransactionOverviewFragment.kt index ae961c2b9..f8619e11a 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/ui/transaction/TransactionOverviewFragment.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/ui/transaction/TransactionOverviewFragment.kt @@ -64,6 +64,7 @@ internal class TransactionOverviewFragment : Fragment() { ) { with(overviewBinding) { url.text = transaction?.getFormattedUrl(encodeUrl) + hostIp.text = transaction?.hostIp method.text = transaction?.method protocol.text = transaction?.protocol status.text = transaction?.status.toString() diff --git a/library/src/main/res/layout/chucker_fragment_transaction_overview.xml b/library/src/main/res/layout/chucker_fragment_transaction_overview.xml index b56440617..99c223c5e 100644 --- a/library/src/main/res/layout/chucker_fragment_transaction_overview.xml +++ b/library/src/main/res/layout/chucker_fragment_transaction_overview.xml @@ -38,6 +38,27 @@ app:layout_constraintTop_toTopOf="parent" tools:text="https://example.com/path/to/resource?here=might_be_really_long" /> + + + + + app:layout_constraintTop_toBottomOf="@id/host_ip" /> Request Response URL + Host IP Method Protocol Status From 99e5f45f8a0a9259c72b673b261222ade8ca85f7 Mon Sep 17 00:00:00 2001 From: Farshid Roohi Date: Fri, 9 Feb 2024 16:55:06 +0330 Subject: [PATCH 2/5] Add hostIp to the test utils and http transaction Dao test --- .../chucker/internal/data/entity/TransactionTestUtils.kt | 1 + .../chucker/internal/data/room/HttpTransactionDaoTest.kt | 1 + .../src/test/kotlin/com/chuckerteam/chucker/util/HarTestUtils.kt | 1 + .../com/chuckerteam/chucker/util/TestTransactionFactory.kt | 1 + 4 files changed, 4 insertions(+) diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/entity/TransactionTestUtils.kt b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/entity/TransactionTestUtils.kt index c4fe1a61a..eeae2eb80 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/entity/TransactionTestUtils.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/entity/TransactionTestUtils.kt @@ -23,6 +23,7 @@ internal fun HttpTransaction.withResponseData(): HttpTransaction = responseCode = 418 // I'm a teapot responseDate = 321L tookMs = 21L + hostIp = "192.168.1.1" responseTlsVersion = randomString() responseCipherSuite = randomString() responsePayloadSize = 0L diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/room/HttpTransactionDaoTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/room/HttpTransactionDaoTest.kt index 662c4fd08..1e04ea705 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/room/HttpTransactionDaoTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/room/HttpTransactionDaoTest.kt @@ -60,6 +60,7 @@ internal class HttpTransactionDaoTest { assertThat(stringValue("responseHeaders")).isEqualTo(data.responseHeaders) assertThat(stringValue("method")).isEqualTo(data.method) assertThat(stringValue("url")).isEqualTo(data.url) + assertThat(stringValue("hostIp")).isEqualTo(data.hostIp) assertThat(stringValue("host")).isEqualTo(data.host) assertThat(stringValue("path")).isEqualTo(data.path) assertThat(stringValue("scheme")).isEqualTo(data.scheme) diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/util/HarTestUtils.kt b/library/src/test/kotlin/com/chuckerteam/chucker/util/HarTestUtils.kt index d98f93606..af03984a5 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/util/HarTestUtils.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/util/HarTestUtils.kt @@ -29,6 +29,7 @@ internal object HarTestUtils { protocol = "HTTP", method = method, url = "http://localhost:80/getUsers", + hostIp = "192.168.1.1", host = "localhost", path = "/getUsers", scheme = "", diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/util/TestTransactionFactory.kt b/library/src/test/kotlin/com/chuckerteam/chucker/util/TestTransactionFactory.kt index 6caa417cd..f417d0900 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/util/TestTransactionFactory.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/util/TestTransactionFactory.kt @@ -13,6 +13,7 @@ internal object TestTransactionFactory { protocol = "HTTP", method = method, url = "http://localhost:80/getUsers", + hostIp = "192.168.1.1", host = "localhost", path = "/getUsers", scheme = "", From d62681bc5944e9d3447c71d9869ed62833377531 Mon Sep 17 00:00:00 2001 From: Farshid Roohi Date: Fri, 9 Feb 2024 22:31:58 +0330 Subject: [PATCH 3/5] Write unit tests to get the host IP address extension function --- .../chucker/internal/support/ResponseExt.kt | 8 +++---- .../internal/support/ResponseProcessor.kt | 2 +- .../chucker/internal/data/har/ResponseTest.kt | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt index f77c8378e..41d6471be 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt @@ -1,15 +1,15 @@ package com.chuckerteam.chucker.internal.support -import okhttp3.Response +import okio.BufferedSource import java.util.regex.Matcher import java.util.regex.Pattern private const val IP_REGEX = "(?:\\d{1,3}\\.){3}\\d{1,3}" -public fun Response.getHostIp(): String? { - val body = body?.source()?.readUtf8() +public fun BufferedSource.getHostIp(): String? { + val body = readUtf8() val pattern: Pattern = Pattern.compile(IP_REGEX) - val matcher: Matcher? = body?.let { pattern.matcher(it) } + val matcher: Matcher? = body.let { pattern.matcher(it) } if (matcher?.find() == true) { return matcher.group() } diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt index 83fd989f0..72fead15f 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt @@ -43,7 +43,7 @@ internal class ResponseProcessor( requestDate = response.sentRequestAtMillis responseDate = response.receivedResponseAtMillis protocol = response.protocol.toString() - hostIp = response.getHostIp() + hostIp = response.body?.source()?.getHostIp() responseCode = response.code responseMessage = response.message diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt index 003b9111c..8476c3ea2 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt @@ -1,8 +1,10 @@ package com.chuckerteam.chucker.internal.data.har import com.chuckerteam.chucker.internal.data.har.log.entry.response.Content +import com.chuckerteam.chucker.internal.support.getHostIp import com.chuckerteam.chucker.util.HarTestUtils import com.google.common.truth.Truth.assertThat +import okhttp3.mockwebserver.MockResponse import org.junit.Test internal class ResponseTest { @@ -48,4 +50,23 @@ internal class ResponseTest { assertThat(response?.bodySize).isEqualTo(1000) } + + @Test + fun `host ip address from response successfully`() { + val body = + "{\"args\": {},\"origin\": \"192.168.1.1\", \"url\": \"https://httpbin.org/get\"}" + val response = MockResponse().setBody(body).setResponseCode(200) + val buffer = response.getBody() + val hostIp = buffer?.getHostIp() + assertThat(hostIp).isEqualTo("192.168.1.1") + } + @Test + fun `host ip address from response doesn't exist`() { + val body = + "{\"args\": {}, \"url\": \"https://httpbin.org/get\"}" + val response = MockResponse().setBody(body).setResponseCode(200) + val buffer = response.getBody() + val hostIp = buffer?.getHostIp() + assertThat(hostIp).isEqualTo(null) + } } From 0450ef69e4d2fe9efe5ce91e6954585bb2cb7b9b Mon Sep 17 00:00:00 2001 From: Farshid Roohi Date: Fri, 9 Feb 2024 22:32:12 +0330 Subject: [PATCH 4/5] Write unit tests to get the host IP address extension function --- .../com/chuckerteam/chucker/internal/data/har/ResponseTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt index 8476c3ea2..b824c70b5 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt @@ -60,6 +60,7 @@ internal class ResponseTest { val hostIp = buffer?.getHostIp() assertThat(hostIp).isEqualTo("192.168.1.1") } + @Test fun `host ip address from response doesn't exist`() { val body = From 48047b8668e5be083e9afeeaf490cd6f04cefdd1 Mon Sep 17 00:00:00 2001 From: Farshid Date: Sun, 13 Sep 2026 10:03:29 +0330 Subject: [PATCH 5/5] feat: display connected peer IP in transaction details --- CHANGELOG.md | 1 + README.md | 2 +- .../chucker/api/ChuckerInterceptor.kt | 7 ++++++ .../internal/data/room/ChuckerDatabase.kt | 2 +- .../chucker/internal/support/ResponseExt.kt | 17 -------------- .../internal/support/ResponseProcessor.kt | 1 - .../api/ChuckerInterceptorSkipRequestTest.kt | 1 + .../chucker/api/ChuckerInterceptorTest.kt | 17 ++++++++++++++ .../chucker/internal/data/har/ResponseTest.kt | 22 ------------------- 9 files changed, 28 insertions(+), 42 deletions(-) delete mode 100644 library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f21e57f4..fbda47a5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Please add your entries according to this format. ### Added - Long-press the payload copy button to choose between copying the raw body or the formatted body [#1613] +- Display the connected peer IP in transaction details when Chucker is used as a network interceptor [#1180] ### Fixed diff --git a/README.md b/README.md index 80334b3fe..d5d653a7a 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ val client = OkHttpClient.Builder() **That's it!** 🎉 Chucker will now record all HTTP interactions made by your OkHttp client. -> **Tip:** Use `addNetworkInterceptor(chuckerInterceptor)` instead of `addInterceptor(chuckerInterceptor)` if you want Chucker to display everything OkHttp sends on the wire, including headers added by other network interceptors (e.g. `Content-Length`, `Accept-Encoding`, cookies from `CookieJar`). Application interceptors only observe the request as your app builds it. See OkHttp's [Interceptors](https://square.github.io/okhttp/features/interceptors/) docs for the full comparison. +> **Tip:** Use `addNetworkInterceptor(chuckerInterceptor)` instead of `addInterceptor(chuckerInterceptor)` if you want Chucker to display the connected peer IP and everything OkHttp sends on the wire, including headers added by other network interceptors (e.g. `Content-Length`, `Accept-Encoding`, cookies from `CookieJar`). The connected peer may be a proxy, VPN, CDN edge, or load balancer rather than the origin server. Application interceptors do not have access to the connection and only observe the request as your app builds it. Cached responses do not pass through network interceptors. See OkHttp's [Interceptors](https://square.github.io/okhttp/features/interceptors/) docs for the full comparison. Historically, Chucker was distributed through JitPack. You can find older version of Chucker here: [![JitPack](https://jitpack.io/v/ChuckerTeam/chucker.svg)](https://jitpack.io/#ChuckerTeam/chucker). diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt index 3c56d08e9..c25cbf85b 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt @@ -96,6 +96,13 @@ public class ChuckerInterceptor private constructor( throw e } return if (shouldProcessTheRequest) { + transaction.hostIp = + chain + .connection() + ?.route() + ?.socketAddress + ?.address + ?.hostAddress responseProcessor.process(response, transaction) } else { response diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt index 653231229..aff7ff116 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/data/room/ChuckerDatabase.kt @@ -6,7 +6,7 @@ import androidx.room.Room import androidx.room.RoomDatabase import com.chuckerteam.chucker.internal.data.entity.HttpTransaction -@Database(entities = [HttpTransaction::class], version = 10, exportSchema = false) +@Database(entities = [HttpTransaction::class], version = 11, exportSchema = false) internal abstract class ChuckerDatabase : RoomDatabase() { abstract fun transactionDao(): HttpTransactionDao diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt deleted file mode 100644 index 41d6471be..000000000 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseExt.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.chuckerteam.chucker.internal.support - -import okio.BufferedSource -import java.util.regex.Matcher -import java.util.regex.Pattern - -private const val IP_REGEX = "(?:\\d{1,3}\\.){3}\\d{1,3}" - -public fun BufferedSource.getHostIp(): String? { - val body = readUtf8() - val pattern: Pattern = Pattern.compile(IP_REGEX) - val matcher: Matcher? = body.let { pattern.matcher(it) } - if (matcher?.find() == true) { - return matcher.group() - } - return null -} diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt index 99086e0f1..0925d19cb 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/internal/support/ResponseProcessor.kt @@ -58,7 +58,6 @@ internal class ResponseProcessor( } protocol = response.protocol.toString() - hostIp = response.body?.source()?.getHostIp() responseCode = response.code responseMessage = response.message diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorSkipRequestTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorSkipRequestTest.kt index daced874b..c28032030 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorSkipRequestTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorSkipRequestTest.kt @@ -378,6 +378,7 @@ internal class ChuckerInterceptorSkipRequestTest { every { code } returns 204 // No Content every { body } returns ResponseBody.EMPTY } + every { connection() } returns null } private fun executeRequestForPath( diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorTest.kt index fbdac9044..7a510bbd2 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorTest.kt @@ -48,6 +48,23 @@ internal class ChuckerInterceptorTest { private val chuckerInterceptor = ChuckerInterceptorDelegate(cacheDirectoryProvider = { tempDir }) + @ParameterizedTest + @EnumSource(value = ClientFactory::class) + fun `connected peer IP is available to network interceptors only`(factory: ClientFactory) { + server.enqueue(MockResponse()) + val request = Request.Builder().url(serverUrl).build() + + val client = factory.create(chuckerInterceptor) + client.newCall(request).execute().readByteStringBody() + val transaction = chuckerInterceptor.expectTransaction() + + when (factory) { + ClientFactory.APPLICATION -> assertThat(transaction.hostIp).isNull() + ClientFactory.NETWORK -> + assertThat(transaction.hostIp).isEqualTo(server.delegate.socketAddress.address.hostAddress) + } + } + @ParameterizedTest @EnumSource(value = ClientFactory::class) fun `image response body is available to Chucker`(factory: ClientFactory) { diff --git a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt index b824c70b5..003b9111c 100644 --- a/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt +++ b/library/src/test/kotlin/com/chuckerteam/chucker/internal/data/har/ResponseTest.kt @@ -1,10 +1,8 @@ package com.chuckerteam.chucker.internal.data.har import com.chuckerteam.chucker.internal.data.har.log.entry.response.Content -import com.chuckerteam.chucker.internal.support.getHostIp import com.chuckerteam.chucker.util.HarTestUtils import com.google.common.truth.Truth.assertThat -import okhttp3.mockwebserver.MockResponse import org.junit.Test internal class ResponseTest { @@ -50,24 +48,4 @@ internal class ResponseTest { assertThat(response?.bodySize).isEqualTo(1000) } - - @Test - fun `host ip address from response successfully`() { - val body = - "{\"args\": {},\"origin\": \"192.168.1.1\", \"url\": \"https://httpbin.org/get\"}" - val response = MockResponse().setBody(body).setResponseCode(200) - val buffer = response.getBody() - val hostIp = buffer?.getHostIp() - assertThat(hostIp).isEqualTo("192.168.1.1") - } - - @Test - fun `host ip address from response doesn't exist`() { - val body = - "{\"args\": {}, \"url\": \"https://httpbin.org/get\"}" - val response = MockResponse().setBody(body).setResponseCode(200) - val buffer = response.getBody() - val hostIp = buffer?.getHostIp() - assertThat(hostIp).isEqualTo(null) - } }