Skip to content

Commit

Permalink
KTOR-3566 Fixed sending DELETE with body in Curl engine
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov committed Sep 19, 2022
1 parent d300441 commit 685ae4b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions ktor-client/ktor-client-curl/build.gradle.kts
Expand Up @@ -57,6 +57,7 @@ kotlin {
dependencies {
api(project(":ktor-client:ktor-client-plugins:ktor-client-logging"))
api(project(":ktor-client:ktor-client-plugins:ktor-client-json"))
api(project(":ktor-server:ktor-server-cio"))
}
}
}
Expand Down
Expand Up @@ -155,15 +155,16 @@ internal class CurlMultiApiHandler : Closeable {
easyHandle.apply {
when (method) {
"GET" -> option(CURLOPT_HTTPGET, 1L)
"PUT" -> {
option(CURLOPT_PUT, 1L)
}
"PUT" -> option(CURLOPT_PUT, 1L)
"POST" -> {
option(CURLOPT_POST, 1L)
option(CURLOPT_POSTFIELDSIZE, size)
}
"HEAD" -> option(CURLOPT_NOBODY, 1L)
else -> option(CURLOPT_CUSTOMREQUEST, method)
else -> {
if (size > 0) option(CURLOPT_POST, 1L)
option(CURLOPT_CUSTOMREQUEST, method)
}
}
}
}
Expand Down
Expand Up @@ -8,6 +8,14 @@ import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.curl.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.server.application.*
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.native.concurrent.*
import kotlin.test.*
Expand All @@ -21,18 +29,42 @@ class CurlNativeTests {
fun testDownloadInBackground() {
backgroundWorker.execute(TransferMode.SAFE, { Unit }) {
runBlocking {
val client = HttpClient(Curl)
client.get("http://google.com").body<String>()
HttpClient(Curl).use {
it.get("http://google.com").body<String>()
}
}
}.consume { assert(it.isNotEmpty()) }
}

@Test
fun testDownload() {
runBlocking {
val client = HttpClient(Curl)
val res = client.get("http://google.com").body<String>()
fun testDownload() = runBlocking {
HttpClient(Curl).use {
val res = it.get("http://google.com").body<String>()
assert(res.isNotEmpty())
}
}

@Test
fun testDelete(): Unit = runBlocking {
val server = embeddedServer(CIO, 0) {
routing {
delete("/delete") {
call.respondText("OK ${call.receiveText()}")
}
}
}.start()
val port = server.resolvedConnectors().first().port

HttpClient(Curl).use {
val response = it.delete("http://localhost:$port/delete")
assertEquals("OK ", response.bodyAsText())

val responseWithBody = it.delete("http://localhost:$port/delete") {
setBody("1")
}
assertEquals("OK 1", responseWithBody.bodyAsText())
}

server.stop(0, 500)
}
}

0 comments on commit 685ae4b

Please sign in to comment.