Skip to content

Commit

Permalink
Solution: method skipPaths on builder does not work as expected, issue
Browse files Browse the repository at this point in the history
…#1237 (#1290)

Co-authored-by: i.kazantsevi <[email protected]>
  • Loading branch information
Ridje and i.kazantsevi authored Oct 4, 2024
1 parent c84fcd5 commit 6031c4c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Please add your entries according to this format.
* Kotlin to 2.0.20
* AGP to 8.5.2
* Fix Toolbar is not accessible on api level 35 [#1280]
* Fixed the `skipPaths` method unexpectedly modified the passed arguments [#1237]

### Added
* Added _save as text_ and _save as .har file_ options to save all transactions [#1214]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.chuckerteam.chucker.internal.support.CacheDirectoryProvider
import com.chuckerteam.chucker.internal.support.PlainTextDecoder
import com.chuckerteam.chucker.internal.support.RequestProcessor
import com.chuckerteam.chucker.internal.support.ResponseProcessor
import com.chuckerteam.chucker.internal.support.addNonBlankPathSegments
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.Response
Expand Down Expand Up @@ -199,6 +200,9 @@ public class ChuckerInterceptor private constructor(
/**
* Sets a list of [String] to skip paths. When any of the [String] matches
* a request path, the request will be skipped.
*
* **Note:** An empty path will be treated as the '/'.
* '//' will also be treated as the '/' path.
*/
public fun skipPaths(vararg paths: String): Builder =
apply {
Expand All @@ -207,7 +211,8 @@ public class ChuckerInterceptor private constructor(
HttpUrl.Builder()
.scheme("https")
.host("example.com")
.addPathSegment(candidatePath).build()
.addNonBlankPathSegments(candidatePath)
.build()
this@Builder.skipPaths.add(httpUrl.encodedPath)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.chuckerteam.chucker.internal.support

import okhttp3.HttpUrl

private const val PATH_SEGMENTS_DELIMITER = "/"

public fun HttpUrl.Builder.addNonBlankPathSegments(candidatePath: String): HttpUrl.Builder =
apply {
candidatePath.split(PATH_SEGMENTS_DELIMITER).filter { it.isNotBlank() }
.forEach { item -> addPathSegment(item) }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chuckerteam.chucker.api

import com.chuckerteam.chucker.internal.support.addNonBlankPathSegments
import com.chuckerteam.chucker.util.ChuckerInterceptorDelegate
import com.chuckerteam.chucker.util.ClientFactory
import com.chuckerteam.chucker.util.NoLoggerRule
Expand Down Expand Up @@ -379,7 +380,11 @@ internal class ChuckerInterceptorSkipRequestTest {
responseBody: String,
) {
val httpUrl =
HttpUrl.Builder().scheme("https").host("testexample.com").addPathSegment(path).build()
HttpUrl.Builder()
.scheme("https")
.host("testexample.com")
.addNonBlankPathSegments(path)
.build()

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

0 comments on commit 6031c4c

Please sign in to comment.