added way more httpbin tests, all possible verbs, all possible types of bodies (file,form,json,etc...)

This commit is contained in:
HoneyryderChuck 2017-12-08 20:24:51 +00:00
parent fcd97c1351
commit 24eea892bd
2 changed files with 80 additions and 0 deletions

View File

@ -18,8 +18,76 @@ class HTTP1Test < Minitest::Spec
assert response.body.to_s.bytesize == response.headers["content-length"].to_i, "didn't load the whole body"
end
%w[post put patch delete].each do |meth|
define_method :"test_#{meth}_form_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: {"foo" => "bar"})
assert response.status == 200, "status is unexpected"
body = json_body(response)
assert body["headers"]["Content-Type"] == "application/x-www-form-urlencoded"
assert body.key?("form")
assert body["form"].key?("foo")
assert body["form"]["foo"] == "bar"
end
define_method :"test_#{meth}_form_file_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: {image: HTTP::FormData::File.new(fixture_file_path)})
assert response.status == 200, "status is unexpected"
body = json_body(response)
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
assert body.key?("files")
assert body["files"].key?("image")
end
define_method :"test_#{meth}_json_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, json: {"foo" => "bar"})
assert response.status == 200, "status is unexpected"
body = json_body(response)
assert body["headers"]["Content-Type"].start_with?("application/json")
assert body.key?("json")
assert body["json"].key?("foo")
assert body["json"]["foo"] == "bar"
end
define_method :"test_#{meth}_body_params" do
uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, body: "data")
assert response.status == 200, "status is unexpected"
body = json_body(response)
assert body["headers"]["Content-Type"] == "application/octet-stream"
assert body.key?("data")
assert body["data"] == "data"
end
end
def test_http_headers
uri = build_uri("/headers")
response = HTTPX.get(uri)
body = json_body(response)
assert body.key?("headers"), "no headers"
assert body["headers"]["accept"] == "*/*", "unexpected accept"
response = HTTPX.headers("accept" => "text/css").get(uri)
body = json_body(response)
assert body["headers"]["accept"] == "text/css", "accept should have been set at the client"
end
def test_http_user_agent
uri = build_uri("/user-agent")
response = HTTPX.get(uri)
body = json_body(response)
assert body.key?("user-agent"), "user agent wasn't there"
assert body["user-agent"] == "httpx.rb/#{HTTPX::VERSION}", "user agent is unexpected"
end
private
def json_body(response)
JSON.parse(response.body.to_s)
end
def build_uri(suffix="/")
"#{origin}#{suffix || "/"}"
end
@ -27,4 +95,16 @@ class HTTP1Test < Minitest::Spec
def origin
"http://nghttp2.org/httpbin"
end
def fixture
File.read(fixture_file_path, encoding: Encoding::BINARY)
end
def fixture_name
File.basename(fixture_file_path)
end
def fixture_file_path
File.join("test", "support", "fixtures", "image.jpg")
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB