Skip to content

Commit

Permalink
Merge pull request #1423 from aartigao/scala-native-fix-put-method
Browse files Browse the repository at this point in the history
Fix PUT method in CurlBackend
  • Loading branch information
Pask423 authored Apr 29, 2022
2 parents 99ab636 + d9afc6b commit 30b3b36
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
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

0 comments on commit 30b3b36

Please sign in to comment.