-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle empty and missing response bodies. (#250)
* Add failing test cases. * Remove unused const. * Gzip response body if it was gunzipped. * Add test cases for regular bodies in Chucker. * Fix rule formatting. * Use proper name for application interceptor. * Return original response downstream. * Account for no content with gzip encoding. * Use Truth for Chucker tests. * Honor empty plaintext bodies. * Revert changes to HttpBinClient. * Update library/src/test/java/com/chuckerteam/chucker/ChuckerInterceptorDelegate.kt * Update library/src/main/java/com/chuckerteam/chucker/internal/support/OkHttpUtils.kt Co-authored-by: Nicola Corti <corti.nico@gmail.com> Co-authored-by: Volodymyr Buberenko <vbuberen@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
282 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 26 additions & 1 deletion
27
library/src/main/java/com/chuckerteam/chucker/internal/support/OkHttpUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
library/src/test/java/com/chuckerteam/chucker/ChuckerInterceptorDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.chuckerteam.chucker | ||
|
||
import android.content.Context | ||
import com.chuckerteam.chucker.api.ChuckerCollector | ||
import com.chuckerteam.chucker.api.ChuckerInterceptor | ||
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction | ||
import com.chuckerteam.chucker.internal.support.FileFactory | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import java.util.concurrent.CopyOnWriteArrayList | ||
import java.util.concurrent.atomic.AtomicLong | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
internal class ChuckerInterceptorDelegate( | ||
fileFactory: FileFactory, | ||
maxContentLength: Long = 250000L, | ||
headersToRedact: Set<String> = emptySet() | ||
) : Interceptor { | ||
private val idGenerator = AtomicLong() | ||
private val transactions = CopyOnWriteArrayList<HttpTransaction>() | ||
|
||
private val mockContext = mockk<Context> { | ||
every { getString(any()) } returns "" | ||
} | ||
private val mockCollector = mockk<ChuckerCollector> { | ||
every { onRequestSent(any()) } returns Unit | ||
every { onResponseReceived(any()) } answers { | ||
val transaction = (args[0] as HttpTransaction) | ||
transaction.id = idGenerator.getAndIncrement() | ||
transactions.add(transaction) | ||
} | ||
} | ||
|
||
private val chucker = ChuckerInterceptor( | ||
context = mockContext, | ||
collector = mockCollector, | ||
maxContentLength = maxContentLength, | ||
headersToRedact = headersToRedact, | ||
fileFactory = fileFactory | ||
) | ||
|
||
internal fun expectTransaction(): HttpTransaction { | ||
if (transactions.isEmpty()) { | ||
throw AssertionError("Expected transaction but was empty.") | ||
} | ||
return transactions.removeAt(0) | ||
} | ||
|
||
internal fun expectNoTransactions() { | ||
if (transactions.isNotEmpty()) { | ||
throw AssertionError("Expected no transactions but found ${transactions.size}") | ||
} | ||
} | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
return chucker.intercept(chain) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.