Skip to content

Commit 8ea4c21

Browse files
committed
feat: add curl() method test cases to RequestTest for generating cURL commands
1 parent dfbbc8a commit 8ea4c21

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

okhttp/src/commonJvmAndroid/kotlin/okhttp3/Request.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class Request internal constructor(
423423

424424
open fun build(): Request = Request(this)
425425
}
426-
426+
427427
/**
428428
* Returns a cURL command equivalent to this request, useful for debugging and reproducing requests.
429429
*
@@ -442,20 +442,28 @@ class Request internal constructor(
442442
if (method != "GET") {
443443
curl.append(" -X ").append(method)
444444
}
445-
445+
446446
for ((name, value) in headers) {
447-
curl.append(" -H ")
448-
.append("\"").append(name).append(": ").append(value).append("\"")
447+
curl
448+
.append(" -H ")
449+
.append("\"")
450+
.append(name)
451+
.append(": ")
452+
.append(value)
453+
.append("\"")
449454
}
450-
455+
451456
body?.let { requestBody ->
452457
val buffer = okio.Buffer()
453458
requestBody.writeTo(buffer)
454459
val bodyString = buffer.readUtf8()
455-
curl.append(" --data ")
456-
.append("\"").append(bodyString.replace("\"", "\\\"")).append("\"")
460+
curl
461+
.append(" --data ")
462+
.append("\"")
463+
.append(bodyString.replace("\"", "\\\""))
464+
.append("\"")
457465
}
458-
466+
459467
curl.append(" \"").append(url).append("\"")
460468
return curl.toString()
461469
}

okhttp/src/jvmTest/kotlin/okhttp3/RequestTest.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,72 @@ class RequestTest {
643643
}
644644
}
645645

646+
@Test
647+
fun curlGet() {
648+
val request =
649+
Request
650+
.Builder()
651+
.url("https://example.com")
652+
.header("Authorization", "Bearer abc123")
653+
.build()
654+
val curl = request.curl()
655+
assert(curl.contains("curl"))
656+
assert(curl.contains("https://example.com"))
657+
assert(curl.contains("-H \"Authorization: Bearer abc123\""))
658+
}
659+
660+
@Test
661+
fun curlPostWithBody() {
662+
val mediaType = "application/json".toMediaType()
663+
val body = "{\"key\":\"value\"}".toRequestBody(mediaType)
664+
val request =
665+
Request
666+
.Builder()
667+
.url("https://api.example.com/data")
668+
.post(body)
669+
.addHeader("Content-Type", "application/json")
670+
.addHeader("Authorization", "Bearer abc123")
671+
.build()
672+
val curl = request.curl()
673+
assert(curl.contains("-X POST"))
674+
assert(curl.contains("--data"))
675+
assert(curl.contains("{\\\"key\\\":\\\"value\\\"}"))
676+
assert(curl.contains("-H \"Content-Type: application/json\""))
677+
assert(curl.contains("-H \"Authorization: Bearer abc123\""))
678+
}
679+
680+
@Test
681+
fun curlPostWithComplexBody() {
682+
val mediaType = "application/json".toMediaType()
683+
val jsonBody =
684+
"""
685+
{
686+
"user": {
687+
"id": 123,
688+
"name": "John Doe"
689+
},
690+
"roles": ["admin", "editor"],
691+
"active": true
692+
}
693+
""".trimIndent()
694+
val body = jsonBody.toRequestBody(mediaType)
695+
val request =
696+
Request
697+
.Builder()
698+
.url("https://api.example.com/users")
699+
.post(body)
700+
.addHeader("Content-Type", "application/json")
701+
.addHeader("Authorization", "Bearer xyz789")
702+
.build()
703+
val curl = request.curl()
704+
assert(curl.contains("-X POST"))
705+
assert(curl.contains("--data"))
706+
assert(curl.contains("user")) // The JSON should contain "user"
707+
assert(curl.contains("roles")) // The JSON should contain "roles"
708+
assert(curl.contains("-H \"Content-Type: application/json\""))
709+
assert(curl.contains("-H \"Authorization: Bearer xyz789\""))
710+
}
711+
646712
private fun bodyToHex(body: RequestBody): String {
647713
val buffer = Buffer()
648714
body.writeTo(buffer)

0 commit comments

Comments
 (0)