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

Allow dynamic choice of HTTP method #94

Merged
merged 2 commits into from
Dec 10, 2021
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
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ val r = requests.delete("http://httpbin.org/delete")
val r = requests.head("http://httpbin.org/head")

val r = requests.options("http://httpbin.org/get")

// dynamically choose what HTTP method to use
val r = requests.send("put")("http://httpbin.org/put", data = Map("key" -> "value"))

```

### Passing in Parameters
Expand Down
2 changes: 2 additions & 0 deletions requests/src/requests/Requester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ trait BaseSession{
lazy val options = Requester("OPTIONS", this)
// unofficial
lazy val patch = Requester("PATCH", this)

def send(method: String) = Requester(method, this)
}

object BaseSession{
Expand Down
44 changes: 44 additions & 0 deletions requests/test/src-2/requests/Scala2RequestTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package requests

import utest._
import ujson._

object Scala2RequestTests extends TestSuite{
val tests = Tests{

test("params"){

test("post"){
for(chunkedUpload <- Seq(true, false)) {
val res1 = requests.post(
"https://httpbin.org/post",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("put") {
for (chunkedUpload <- Seq(true, false)) {
val res1 = requests.put(
"https://httpbin.org/put",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("send"){
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz")

val res1 = requests.send("put")(
"https://httpbin.org/put",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = true
).text

assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
}
}
20 changes: 0 additions & 20 deletions requests/test/src/requests/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,6 @@ object RequestTests extends TestSuite{
)
assert(read(res4).obj("args") == Obj("++-- lol" -> " !@#$%", "hello" -> "world"))
}
test("post"){
for(chunkedUpload <- Seq(true, false)) {
val res1 = requests.post(
"https://httpbin.org/post",
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("put") {
for (chunkedUpload <- Seq(true, false)) {
val res1 = requests.put(
"https://httpbin.org/put",
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
}
test("multipart"){
for(chunkedUpload <- Seq(true, false)) {
Expand Down