httpx/test/support/requests/with_body.rb
HoneyryderChuck 092e594a4b Request.verb is now an upcased string (ex: "GET")
The reference for a request verb is now the string which is used
everywhere else, instead of the symbol corresponding to it. This was an
artifact from the import from httprb, and there is no advantage in it,
since these strings are frozen in most use cases, and the
transformations from symbol to strings being performed everywhere are
prooof that keeping the atom isn't really bringing any benefit.
2023-04-17 16:54:31 +03:00

138 lines
5.7 KiB
Ruby

# frozen_string_literal: true
module Requests
module WithBody
%w[post put patch delete].each do |meth|
define_method :"test_#{meth}_query_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, params: { "q" => "this is a test" })
verify_status(response, 200)
body = json_body(response)
verify_uploaded(body, "args", "q" => "this is a test")
verify_uploaded(body, "url", build_uri("/#{meth}?q=this+is+a+test"))
end
define_method :"test_#{meth}_query_nested_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, params: { "q" => { "a" => "z" }, "a" => %w[1 2], "b" => [] })
verify_status(response, 200)
body = json_body(response)
verify_uploaded(body, "args", "q[a]" => "z", "a[]" => %w[1 2], "b[]" => "")
verify_uploaded(body, "url", build_uri("/#{meth}?q[a]=z&a[]=1&a[]=2&b[]"))
end
define_method :"test_#{meth}_form_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: { "foo" => "bar" })
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded")
verify_uploaded(body, "form", "foo" => "bar")
end
define_method :"test_#{meth}_form_nested_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: { "q" => { "a" => "z" }, "a" => %w[1 2], "b" => [] })
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded")
verify_uploaded(body, "form", "q[a]" => "z", "a[]" => %w[1 2], "b[]" => "")
end
define_method :"test_#{meth}_expect_100_form_params" do
uri = build_uri("/#{meth}")
response = HTTPX.with_headers("expect" => "100-continue")
.send(meth, uri, form: { "foo" => "bar" })
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded")
verify_header(body["headers"], "Expect", "100-continue")
verify_uploaded(body, "form", "foo" => "bar")
end
define_method :"test_#{meth}_json_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, json: { "foo" => "bar" })
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/json")
verify_uploaded(body, "json", "foo" => "bar")
end
define_method :"test_#{meth}_body_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, body: "data")
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data")
end
define_method :"test_#{meth}_body_ary_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, body: %w[d a t a])
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data")
end
define_method :"test_#{meth}_body_each_params" do
uri = build_uri("/#{meth}")
body = Class.new do
def each(&blk)
%w[d a t a].each(&blk)
end
end.new
response = HTTPX.send(meth, uri, body: body)
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_header(body["headers"], "Transfer-Encoding", "chunked")
# TODO: nghttp not receiving chunked requests, investigate
# verify_uploaded(body, "data", "data")
end
define_method :"test_#{meth}_body_io_params" do
uri = build_uri("/#{meth}")
body = StringIO.new("data")
response = HTTPX.send(meth, uri, body: body)
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data")
end
define_method :"test_#{meth}_multiple_params" do
uri = build_uri("/#{meth}")
response1, response2 = HTTPX.request([
[meth.upcase, uri, { body: "data" }],
[meth.upcase, uri, { form: { "foo" => "bar" } }],
], max_concurrent_requests: 1) # because httpbin sucks and can't handle pipeline requests
verify_status(response1, 200)
body1 = json_body(response1)
verify_header(body1["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body1, "data", "data")
verify_status(response2, 200)
body2 = json_body(response2)
verify_header(body2["headers"], "Content-Type", "application/x-www-form-urlencoded")
verify_uploaded(body2, "form", "foo" => "bar")
end
define_method :"test_#{meth}_build_request_body_params" do
uri = build_uri("/#{meth}")
HTTPX.wrap do |http|
request = http.build_request(meth.upcase, uri, body: "data")
response = http.request(request)
verify_status(response, 200)
body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data")
end
end
end
end
end