Skip to content

Commit 6031c4c

Browse files
Ridjei.kazantsevi
and
i.kazantsevi
authored
Solution: method skipPaths on builder does not work as expected, issue #1237 (#1290)
Co-authored-by: i.kazantsevi <[email protected]>
1 parent c84fcd5 commit 6031c4c

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Please add your entries according to this format.
1111
* Kotlin to 2.0.20
1212
* AGP to 8.5.2
1313
* Fix Toolbar is not accessible on api level 35 [#1280]
14+
* Fixed the `skipPaths` method unexpectedly modified the passed arguments [#1237]
1415

1516
### Added
1617
* Added _save as text_ and _save as .har file_ options to save all transactions [#1214]

library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.chuckerteam.chucker.internal.support.CacheDirectoryProvider
77
import com.chuckerteam.chucker.internal.support.PlainTextDecoder
88
import com.chuckerteam.chucker.internal.support.RequestProcessor
99
import com.chuckerteam.chucker.internal.support.ResponseProcessor
10+
import com.chuckerteam.chucker.internal.support.addNonBlankPathSegments
1011
import okhttp3.HttpUrl
1112
import okhttp3.Interceptor
1213
import okhttp3.Response
@@ -199,6 +200,9 @@ public class ChuckerInterceptor private constructor(
199200
/**
200201
* Sets a list of [String] to skip paths. When any of the [String] matches
201202
* a request path, the request will be skipped.
203+
*
204+
* **Note:** An empty path will be treated as the '/'.
205+
* '//' will also be treated as the '/' path.
202206
*/
203207
public fun skipPaths(vararg paths: String): Builder =
204208
apply {
@@ -207,7 +211,8 @@ public class ChuckerInterceptor private constructor(
207211
HttpUrl.Builder()
208212
.scheme("https")
209213
.host("example.com")
210-
.addPathSegment(candidatePath).build()
214+
.addNonBlankPathSegments(candidatePath)
215+
.build()
211216
this@Builder.skipPaths.add(httpUrl.encodedPath)
212217
}
213218
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.chuckerteam.chucker.internal.support
2+
3+
import okhttp3.HttpUrl
4+
5+
private const val PATH_SEGMENTS_DELIMITER = "/"
6+
7+
public fun HttpUrl.Builder.addNonBlankPathSegments(candidatePath: String): HttpUrl.Builder =
8+
apply {
9+
candidatePath.split(PATH_SEGMENTS_DELIMITER).filter { it.isNotBlank() }
10+
.forEach { item -> addPathSegment(item) }
11+
}

library/src/test/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptorSkipRequestTest.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.chuckerteam.chucker.api
22

3+
import com.chuckerteam.chucker.internal.support.addNonBlankPathSegments
34
import com.chuckerteam.chucker.util.ChuckerInterceptorDelegate
45
import com.chuckerteam.chucker.util.ClientFactory
56
import com.chuckerteam.chucker.util.NoLoggerRule
@@ -379,7 +380,11 @@ internal class ChuckerInterceptorSkipRequestTest {
379380
responseBody: String,
380381
) {
381382
val httpUrl =
382-
HttpUrl.Builder().scheme("https").host("testexample.com").addPathSegment(path).build()
383+
HttpUrl.Builder()
384+
.scheme("https")
385+
.host("testexample.com")
386+
.addNonBlankPathSegments(path)
387+
.build()
383388

384389
val request = Request.Builder().url(server.url(httpUrl.encodedPath)).build()
385390
server.enqueue(MockResponse().setBody(responseBody))

0 commit comments

Comments
 (0)