Skip to content

Commit

Permalink
Add base64 encoding to body string and update header
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitozgumus committed Jul 11, 2024
1 parent 275263d commit cfb7c70
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mockingstar/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ publishing {
create<MavenPublication>("release") {
groupId = "com.trendyol.mockingstar"
artifactId = "mockingstar"
version = "1.0.1"
version = "1.0.2"
afterEvaluate {
from(components["java"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import okhttp3.Response
import okio.Buffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi

class MockingStarInterceptor(
private val params: MockUrlParams = MockUrlParams(),
private val header: Map<String, String> = emptyMap(),
) : Interceptor {

@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, ExperimentalEncodingApi::class)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Expand All @@ -26,13 +28,18 @@ class MockingStarInterceptor(
originalRequest.body?.writeTo(buffer)

val contentType = originalRequest.body?.contentType()
val charset: Charset = contentType?.charset(StandardCharsets.UTF_8) ?: StandardCharsets.UTF_8
val charset: Charset =
contentType?.charset(StandardCharsets.UTF_8) ?: StandardCharsets.UTF_8

val bodyString = buffer.readString(charset)
.takeIf { it.isNotEmpty() }
?.let { Base64.UrlSafe.encode(it.toByteArray()) }

val requestBody = MockingStarRequestBody(
method = originalRequest.method,
url = originalRequest.url.toString(),
headers = originalRequest.headers.toMap(),
body = buffer.readString(charset).takeIf { it.isNotEmpty() },
header = originalRequest.headers.toMap(),
body = bodyString
)
val json = Json { explicitNulls = false }
val requestString = json.encodeToString(requestBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import kotlinx.serialization.Serializable
internal class MockingStarRequestBody(
val method: String,
val url: String,
val headers: Map<String, String>,
val header: Map<String, String>,
val body: String?,
)

0 comments on commit cfb7c70

Please sign in to comment.