mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
added tests for json and form (also multipart) decoders
This commit is contained in:
parent
c9de63d4cd
commit
e5a120111c
@ -125,6 +125,21 @@ class ResponseTest < Minitest::Test
|
||||
assert body.buffer.is_a?(Tempfile), "body should buffer to file after going over threshold"
|
||||
end
|
||||
|
||||
def test_response_decoders
|
||||
json_response = Response.new(request, 200, "2.0", { "content-type" => "application/json" })
|
||||
json_response << %({"a": "b"})
|
||||
assert json_response.json == { "a" => "b" }
|
||||
assert json_response.json(symbolize_names: true) == { :a => "b" }
|
||||
|
||||
form_response = Response.new(request, 200, "2.0", { "content-type" => "application/x-www-form-urlencoded" })
|
||||
form_response << "a=b&c=d"
|
||||
assert form_response.form == { "a" => "b", "c" => "d" }
|
||||
|
||||
form2_response = Response.new(request, 200, "2.0", { "content-type" => "application/x-www-form-urlencoded" })
|
||||
form2_response << "a[]=b&a[]=c&d[e]=f"
|
||||
assert form2_response.form == { "a" => %w[b c], "d" => { "e" => "f" } }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request(verb = :get, uri = "http://google.com")
|
||||
|
@ -219,6 +219,42 @@ module Requests
|
||||
assert check_error[retries_response], "expected #{retries_response} to be an error response"
|
||||
assert retries_session.calls == 1, "expect request to be retried 1 time (was #{retries_session.calls})"
|
||||
end
|
||||
|
||||
def test_plugin_multipart_response_decoder
|
||||
form_response = HTTPX.plugin(:multipart)
|
||||
.class.default_options
|
||||
.response_class
|
||||
.new(
|
||||
HTTPX::Request.new(:get, "http://example.com"),
|
||||
200,
|
||||
"2.0",
|
||||
{ "content-type" => "Content-Type: multipart/form-data; boundary=90" }
|
||||
)
|
||||
form_response << [
|
||||
"--90\r\n",
|
||||
"Content-Disposition: form-data; name=\"text\"\r\n\r\n",
|
||||
"text default\r\n",
|
||||
"--90\r\n",
|
||||
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n",
|
||||
"Content-Type: text/plain\r\n\r\n",
|
||||
"Content of a.txt.\r\n\r\n",
|
||||
"--90\r\n",
|
||||
"Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n",
|
||||
"Content-Type: text/html\r\n\r\n",
|
||||
"<!DOCTYPE html><title>Content of a.html.</title>\r\n\r\n",
|
||||
"--90--",
|
||||
].join
|
||||
form = form_response.form
|
||||
|
||||
assert form["text"] == "text_default"
|
||||
assert form["file1"].original_filename == "a.txt"
|
||||
assert form["file1"].content_type == "text/plain"
|
||||
assert form["file1"].read == "Content of a.txt."
|
||||
|
||||
assert form["file2"].original_filename == "a.html"
|
||||
assert form["file2"].content_type == "text/html"
|
||||
assert form["file2"].read == "<!DOCTYPE html><title>Content of a.html.</title>"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user