Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PUT method in CurlBackend #1423

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ abstract class AbstractCurlBackend[F[_]](monad: MonadError[F], verbose: Boolean)
case Method.GET => handle.option(HttpGet, true)
case Method.HEAD => handle.option(Head, true)
case Method.POST => handle.option(Post, true)
case Method.PUT => handle.option(Put, true)
case Method.PUT => handle.option(CustomRequest, "PUT")
case Method.DELETE => handle.option(Post, true)
case Method.OPTIONS => handle.option(RtspRequest, true)
case Method.PATCH => handle.option(CustomRequest, "PATCH")
Expand Down
49 changes: 49 additions & 0 deletions core/src/test/scalanative/sttp/client3/testing/SyncHttpTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ trait SyncHttpTest
val backend: SttpBackend[Identity, Any]

protected def postEcho = basicRequest.post(uri"$endpoint/echo")
protected def putEcho = basicRequest.put(uri"$endpoint/echo")
protected val testBody = "this is the body"
protected val testBodyBytes = testBody.getBytes("UTF-8")
protected val expectedPostEchoResponse = "POST /echo this is the body"
protected val expectedPutEchoResponse = "PUT /echo this is the body"

protected val sttpIgnore = sttp.client3.ignore

Expand Down Expand Up @@ -180,6 +182,53 @@ trait SyncHttpTest
val response = postEcho.send(backend)
response.body should be(Right("POST /echo"))
}

"put a string" in {
val response = putEcho
.body(testBody)
.send(backend)
response.body should be(Right(expectedPutEchoResponse))
}

"put a byte array" in {
val response = putEcho.body(testBodyBytes).send(backend)
response.body should be(Right(expectedPutEchoResponse))
}

"put an input stream" in {
val response = putEcho
.body(new ByteArrayInputStream(testBodyBytes))
.send(backend)
response.body should be(Right(expectedPutEchoResponse))
}

"put a byte buffer" in {
val response = putEcho
.body(ByteBuffer.wrap(testBodyBytes))
.send(backend)
response.body should be(Right(expectedPutEchoResponse))
}

"put form data" in {
val response = basicRequest
.put(uri"$endpoint/echo/form_params/as_string")
.body("a" -> "b", "c" -> "d")
.send(backend)
response.body should be(Right("a=b c=d"))
}

"put form data with special characters" in {
val response = basicRequest
.put(uri"$endpoint/echo/form_params/as_string")
.body("a=" -> "/b", "c:" -> "/d")
.send(backend)
response.body should be(Right("a==/b c:=/d"))
}

"put without a body" in {
val response = putEcho.send(backend)
response.body should be(Right("PUT /echo"))
}
}

protected def cacheControlHeaders = Set("no-cache", "max-age=1000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ private class HttpServer(port: Int, info: String => Unit) extends AutoCloseable
)
}
}
} ~ put {
parameterMap { params =>
entity(as[String]) { (body: String) =>
complete(
List("PUT", "/echo", paramsToString(params), body)
.filter(_.nonEmpty)
.mkString(" ")
)
}
}
}
} ~ pathPrefix("streaming") {
path("echo") {
Expand Down