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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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?,
Expand Down Expand Up @@ -71,6 +72,7 @@ internal class HttpTransaction(
protocol = null,
method = null,
url = null,
hostIp = null,
host = null,
path = null,
scheme = null,
Expand Down Expand Up @@ -306,6 +308,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) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,35 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="https://example.com/path/to/resource?here=might_be_really_long" />

<TextView
style="@style/Chucker.TextAppearance.Label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/chucker_host_ip"
app:layout_constraintEnd_toStartOf="@id/overviewGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/url" />

<TextView
android:id="@+id/host_ip"
style="@style/Chucker.TextAppearance.Value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/overviewGuideline"
app:layout_constraintTop_toBottomOf="@+id/url"
tools:text="192.168.1.1" />

<TextView
style="@style/Chucker.TextAppearance.Label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/chucker_method"
app:layout_constraintEnd_toStartOf="@id/overviewGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/url" />
app:layout_constraintTop_toBottomOf="@id/host_ip" />

<TextView
android:id="@+id/method"
Expand All @@ -54,7 +75,7 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/overviewGuideline"
app:layout_constraintTop_toBottomOf="@+id/url"
app:layout_constraintTop_toBottomOf="@+id/host_ip"
tools:text="GET" />

<TextView
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="chucker_request">Request</string>
<string name="chucker_response">Response</string>
<string name="chucker_url">URL</string>
<string name="chucker_host_ip">Host IP</string>
<string name="chucker_method">Method</string>
<string name="chucker_protocol">Protocol</string>
<string name="chucker_status">Status</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
Loading