verify_status receives response and raises when not a proper response

This commit is contained in:
HoneyryderChuck 2018-03-20 20:08:24 +00:00
parent d78230899b
commit 4be12daa00
15 changed files with 50 additions and 49 deletions

View File

@ -3,8 +3,9 @@
module ResponseHelpers module ResponseHelpers
private private
def verify_status(value, expect) def verify_status(response, expect)
assert value == expect, "status assertion failed: #{value} (expected: #{expect})" raise response.status if response.is_a?(HTTPX::ErrorResponse)
assert response.status == expect, "status assertion failed: #{response.status} (expected: #{expect})"
end end
%w[header param].each do |meth| %w[header param].each do |meth|

View File

@ -5,7 +5,7 @@ module Requests
def test_http_chunked_get def test_http_chunked_get
uri = build_uri("/stream-bytes/30?chunk_size=5") uri = build_uri("/stream-bytes/30?chunk_size=5")
response = HTTPX.get(uri) response = HTTPX.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_header(response.headers, "transfer-encoding", "chunked") verify_header(response.headers, "transfer-encoding", "chunked")
verify_body_length(response, 30) verify_body_length(response, 30)
end end

View File

@ -5,7 +5,7 @@ module Requests
def test_http_get def test_http_get
uri = build_uri("/") uri = build_uri("/")
response = HTTPX.get(uri) response = HTTPX.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
end end
end end

View File

@ -5,7 +5,7 @@ module Requests
def test_http_head def test_http_head
uri = build_uri("/") uri = build_uri("/")
response = HTTPX.head(uri) response = HTTPX.head(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response, 0) verify_body_length(response, 0)
end end
end end

View File

@ -6,7 +6,7 @@ module Requests
io = origin_io io = origin_io
uri = build_uri("/") uri = build_uri("/")
response = HTTPX.get(uri, io: io) response = HTTPX.get(uri, io: io)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
assert !io.closed?, "io should have been left open" assert !io.closed?, "io should have been left open"
ensure ensure

View File

@ -5,24 +5,24 @@ module Requests
module Authentication module Authentication
def test_plugin_basic_authentication def test_plugin_basic_authentication
no_auth_response = HTTPX.get(basic_auth_uri) no_auth_response = HTTPX.get(basic_auth_uri)
verify_status(no_auth_response.status, 401) verify_status(no_auth_response, 401)
verify_header(no_auth_response.headers, "www-authenticate", "Basic realm=\"Fake Realm\"") verify_header(no_auth_response.headers, "www-authenticate", "Basic realm=\"Fake Realm\"")
client = HTTPX.plugin(:basic_authentication) client = HTTPX.plugin(:basic_authentication)
response = client.basic_authentication(user, pass).get(basic_auth_uri) response = client.basic_authentication(user, pass).get(basic_auth_uri)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body, "authenticated", true) verify_header(body, "authenticated", true)
verify_header(body, "user", user) verify_header(body, "user", user)
invalid_response = client.basic_authentication(user, "fake").get(basic_auth_uri) invalid_response = client.basic_authentication(user, "fake").get(basic_auth_uri)
verify_status(invalid_response.status, 401) verify_status(invalid_response, 401)
end end
def test_plugin_digest_authentication def test_plugin_digest_authentication
client = HTTPX.plugin(:digest_authentication).headers("cookie" => "fake=fake_value") client = HTTPX.plugin(:digest_authentication).headers("cookie" => "fake=fake_value")
response = client.digest_authentication(user, pass).get(digest_auth_uri) response = client.digest_authentication(user, pass).get(digest_auth_uri)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body, "authenticated", true) verify_header(body, "authenticated", true)
verify_header(body, "user", user) verify_header(body, "user", user)

View File

@ -7,13 +7,13 @@ module Requests
url = "https://github.com" url = "https://github.com"
response1 = HTTPX.get(url) response1 = HTTPX.get(url)
skip if response1.status == 429 skip if response1.status == 429
verify_status(response1.status, 200) verify_status(response1, 200)
assert !response1.headers.key?("content-encoding"), "response should come in plain text" assert !response1.headers.key?("content-encoding"), "response should come in plain text"
client = HTTPX.plugin(:compression) client = HTTPX.plugin(:compression)
response = client.get(url) response = client.get(url)
skip if response.status == 429 skip if response == 429
verify_status(response.status, 200) verify_status(response, 200)
verify_header(response.headers, "content-encoding", "gzip") verify_header(response.headers, "content-encoding", "gzip")
end end
@ -21,7 +21,7 @@ module Requests
client = HTTPX.plugin(:compression) client = HTTPX.plugin(:compression)
uri = build_uri("/gzip") uri = build_uri("/gzip")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
assert body["gzipped"], "response should be gzipped" assert body["gzipped"], "response should be gzipped"
end end
@ -31,7 +31,7 @@ module Requests
uri = build_uri("/post") uri = build_uri("/post")
response = client.headers("content-encoding" => "gzip") response = client.headers("content-encoding" => "gzip")
.post(uri, body: "a" * 8012) .post(uri, body: "a" * 8012)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
compressed_data = body["data"] compressed_data = body["data"]
@ -42,7 +42,7 @@ module Requests
client = HTTPX.plugin(:compression) client = HTTPX.plugin(:compression)
uri = build_uri("/deflate") uri = build_uri("/deflate")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
assert body["deflated"], "response should be deflated" assert body["deflated"], "response should be deflated"
end end
@ -52,7 +52,7 @@ module Requests
uri = build_uri("/post") uri = build_uri("/post")
response = client.headers("content-encoding" => "deflate") response = client.headers("content-encoding" => "deflate")
.post(uri, body: "a" * 8012) .post(uri, body: "a" * 8012)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
compressed_data = body["data"] compressed_data = body["data"]
@ -63,7 +63,7 @@ module Requests
def test_plugin_compression_brotli def test_plugin_compression_brotli
client = HTTPX.plugin(:"compression/brotli") client = HTTPX.plugin(:"compression/brotli")
response = client.get("http://httpbin.org/brotli") response = client.get("http://httpbin.org/brotli")
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
assert body["brotli"], "response should be deflated" assert body["brotli"], "response should be deflated"
end end
@ -73,7 +73,7 @@ module Requests
uri = build_uri("/post") uri = build_uri("/post")
response = client.headers("content-encoding" => "br") response = client.headers("content-encoding" => "br")
.post(uri, body: "a" * 8012) .post(uri, body: "a" * 8012)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
compressed_data = body["data"] compressed_data = body["data"]

View File

@ -23,7 +23,7 @@ module Requests
session_cookies = { "a" => "b", "c" => "d" } session_cookies = { "a" => "b", "c" => "d" }
session_uri = cookies_set_uri(session_cookies) session_uri = cookies_set_uri(session_cookies)
session_response = client.get(cookies_set_uri(session_cookies)) session_response = client.get(cookies_set_uri(session_cookies))
assert session_response.status == 302, "response should redirect" assert session_response == 302, "response should redirect"
assert !session_response.cookies.nil?, "there should be cookies in the response" assert !session_response.cookies.nil?, "there should be cookies in the response"
response_cookies = session_response.cookie_jar response_cookies = session_response.cookie_jar

View File

@ -7,12 +7,12 @@ module Requests
module FollowRedirects module FollowRedirects
def test_plugin_follow_redirects def test_plugin_follow_redirects
no_redirect_response = HTTPX.get(redirect_uri) no_redirect_response = HTTPX.get(redirect_uri)
verify_status(no_redirect_response.status, 302) verify_status(no_redirect_response, 302)
verify_header(no_redirect_response.headers, "location", redirect_location) verify_header(no_redirect_response.headers, "location", redirect_location)
client = HTTPX.plugin(:follow_redirects) client = HTTPX.plugin(:follow_redirects)
redirect_response = client.get(redirect_uri) redirect_response = client.get(redirect_uri)
verify_status(redirect_response.status, 200) verify_status(redirect_response, 200)
body = json_body(redirect_response) body = json_body(redirect_response)
assert body.key?("url"), "url should be set" assert body.key?("url"), "url should be set"
assert body["url"] == redirect_location, "url should have been the given redirection url" assert body["url"] == redirect_location, "url should have been the given redirection url"
@ -22,20 +22,20 @@ module Requests
client = HTTPX.plugin(:follow_redirects) client = HTTPX.plugin(:follow_redirects)
response = client.get(max_redirect_uri(3)) response = client.get(max_redirect_uri(3))
verify_status(response.status, 200) verify_status(response, 200)
response = client.get(max_redirect_uri(4)) response = client.get(max_redirect_uri(4))
verify_status(response.status, 302) verify_status(response, 302)
end end
def test_plugin_follow_redirects_max_redirects def test_plugin_follow_redirects_max_redirects
client = HTTPX.plugin(:follow_redirects) client = HTTPX.plugin(:follow_redirects)
response = client.max_redirects(1).get(max_redirect_uri(1)) response = client.max_redirects(1).get(max_redirect_uri(1))
verify_status(response.status, 200) verify_status(response, 200)
response = client.max_redirects(1).get(max_redirect_uri(2)) response = client.max_redirects(1).get(max_redirect_uri(2))
verify_status(response.status, 302) verify_status(response, 302)
end end
private private

View File

@ -6,7 +6,7 @@ module Requests
def test_plugin_h2c_disabled def test_plugin_h2c_disabled
uri = build_uri("/get") uri = build_uri("/get")
response = HTTPX.get(uri) response = HTTPX.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
assert response.version == "1.1", "http requests should be by default in HTTP/1.1" assert response.version == "1.1", "http requests should be by default in HTTP/1.1"
end end
@ -14,7 +14,7 @@ module Requests
client = HTTPX.plugin(:h2c) client = HTTPX.plugin(:h2c)
uri = build_uri("/get") uri = build_uri("/get")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
assert response.version == "2.0", "http h2c requests should be in HTTP/2" assert response.version == "2.0", "http h2c requests should be in HTTP/2"
end end
end end

View File

@ -9,7 +9,7 @@ module Requests
client = HTTPX.plugin(:proxy).with_proxy(uri: http_proxy) client = HTTPX.plugin(:proxy).with_proxy(uri: http_proxy)
uri = build_uri("/get") uri = build_uri("/get")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
end end
@ -17,7 +17,7 @@ module Requests
client = HTTPX.plugin(:proxy).with_proxy(uri: socks4_proxy) client = HTTPX.plugin(:proxy).with_proxy(uri: socks4_proxy)
uri = build_uri("/get") uri = build_uri("/get")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
end end
@ -25,7 +25,7 @@ module Requests
client = HTTPX.plugin(:proxy).with_proxy(uri: socks4a_proxy) client = HTTPX.plugin(:proxy).with_proxy(uri: socks4a_proxy)
uri = build_uri("/get") uri = build_uri("/get")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
end end
@ -33,7 +33,7 @@ module Requests
client = HTTPX.plugin(:proxy).with_proxy(uri: socks5_proxy) client = HTTPX.plugin(:proxy).with_proxy(uri: socks5_proxy)
uri = build_uri("/get") uri = build_uri("/get")
response = client.get(uri) response = client.get(uri)
verify_status(response.status, 200) verify_status(response, 200)
verify_body_length(response) verify_body_length(response)
end end
end end

View File

@ -6,8 +6,8 @@ module Requests
def test_plugin_push_promise_get def test_plugin_push_promise_get
client = HTTPX.plugin(:push_promise) client = HTTPX.plugin(:push_promise)
html, css = client.get(push_html_uri, push_css_uri) html, css = client.get(push_html_uri, push_css_uri)
verify_status(html.status, 200) verify_status(html, 200)
verify_status(css.status, 200) verify_status(css, 200)
verify_header(css.headers, "x-http2-push", "1") verify_header(css.headers, "x-http2-push", "1")
end end
@ -15,8 +15,8 @@ module Requests
client = HTTPX.plugin(:push_promise) client = HTTPX.plugin(:push_promise)
.with(max_concurrent_requests: 100) .with(max_concurrent_requests: 100)
html, css = client.get(push_html_uri, push_css_uri) html, css = client.get(push_html_uri, push_css_uri)
verify_status(html.status, 200) verify_status(html, 200)
verify_status(css.status, 200) verify_status(css, 200)
verify_no_header(css.headers, "x-http2-push") verify_no_header(css.headers, "x-http2-push")
end end

View File

@ -6,7 +6,7 @@ module Requests
file = Tempfile.new(%w[cat .jpeg]) file = Tempfile.new(%w[cat .jpeg])
uri = build_uri("/image") uri = build_uri("/image")
response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" }) response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" })
verify_status(response.status, 200) verify_status(response, 200)
verify_header(response.headers, "content-type", "image/jpeg") verify_header(response.headers, "content-type", "image/jpeg")
response.copy_to(file) response.copy_to(file)
verify_body_length(response) verify_body_length(response)
@ -23,7 +23,7 @@ module Requests
io = StringIO.new io = StringIO.new
uri = build_uri("/image") uri = build_uri("/image")
response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" }) response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" })
verify_status(response.status, 200) verify_status(response, 200)
verify_header(response.headers, "content-type", "image/jpeg") verify_header(response.headers, "content-type", "image/jpeg")
response.copy_to(io) response.copy_to(io)
content_length = response.headers["content-length"].to_i content_length = response.headers["content-length"].to_i
@ -53,7 +53,7 @@ module Requests
end end
response = HTTPX.with(response_body_class: custom_body).get(uri) response = HTTPX.with(response_body_class: custom_body).get(uri)
verify_status(response.status, 200) verify_status(response, 200)
assert response.body.is_a?(custom_body), "body should be from custom type" assert response.body.is_a?(custom_body), "body should be from custom type"
file = response.body.file file = response.body.file
file.rewind file.rewind

View File

@ -6,7 +6,7 @@ module Requests
define_method :"test_#{meth}_query_params" do define_method :"test_#{meth}_query_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, params: { "q" => "this is a test" }) response = HTTPX.send(meth, uri, params: { "q" => "this is a test" })
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_uploaded(body, "args", "q" => "this is a test") verify_uploaded(body, "args", "q" => "this is a test")
verify_uploaded(body, "url", build_uri("/#{meth}?q=this+is+a+test")) verify_uploaded(body, "url", build_uri("/#{meth}?q=this+is+a+test"))
@ -15,7 +15,7 @@ module Requests
define_method :"test_#{meth}_form_params" do define_method :"test_#{meth}_form_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: { "foo" => "bar" }) response = HTTPX.send(meth, uri, form: { "foo" => "bar" })
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded") verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded")
verify_uploaded(body, "form", "foo" => "bar") verify_uploaded(body, "form", "foo" => "bar")
@ -24,7 +24,7 @@ module Requests
define_method :"test_#{meth}_json_params" do define_method :"test_#{meth}_json_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, json: { "foo" => "bar" }) response = HTTPX.send(meth, uri, json: { "foo" => "bar" })
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/json") verify_header(body["headers"], "Content-Type", "application/json")
verify_uploaded(body, "json", "foo" => "bar") verify_uploaded(body, "json", "foo" => "bar")
@ -33,7 +33,7 @@ module Requests
define_method :"test_#{meth}_body_params" do define_method :"test_#{meth}_body_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, body: "data") response = HTTPX.send(meth, uri, body: "data")
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data") verify_uploaded(body, "data", "data")
@ -42,7 +42,7 @@ module Requests
define_method :"test_#{meth}_body_ary_params" do define_method :"test_#{meth}_body_ary_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, body: %w[d a t a]) response = HTTPX.send(meth, uri, body: %w[d a t a])
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data") verify_uploaded(body, "data", "data")
@ -58,7 +58,7 @@ module Requests
# y << "a" # y << "a"
# end # end
# response = HTTPX.send(meth, uri, body: body) # response = HTTPX.send(meth, uri, body: body)
# verify_status(response.status, 200) # verify_status(response, 200)
# body = json_body(response) # body = json_body(response)
# verify_header(body["headers"], "Content-Type", "application/octet-stream") # verify_header(body["headers"], "Content-Type", "application/octet-stream")
# verify_uploaded(body, "data", "data") # verify_uploaded(body, "data", "data")
@ -68,7 +68,7 @@ module Requests
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
body = StringIO.new("data") body = StringIO.new("data")
response = HTTPX.send(meth, uri, body: body) response = HTTPX.send(meth, uri, body: body)
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "application/octet-stream") verify_header(body["headers"], "Content-Type", "application/octet-stream")
verify_uploaded(body, "data", "data") verify_uploaded(body, "data", "data")
@ -77,7 +77,7 @@ module Requests
define_method :"test_#{meth}_form_file_params" do define_method :"test_#{meth}_form_file_params" do
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.send(meth, uri, form: { image: HTTP::FormData::File.new(fixture_file_path) }) response = HTTPX.send(meth, uri, form: { image: HTTP::FormData::File.new(fixture_file_path) })
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "multipart/form-data") verify_header(body["headers"], "Content-Type", "multipart/form-data")
verify_uploaded_image(body) verify_uploaded_image(body)
@ -87,7 +87,7 @@ module Requests
uri = build_uri("/#{meth}") uri = build_uri("/#{meth}")
response = HTTPX.headers("expect" => "100-continue") response = HTTPX.headers("expect" => "100-continue")
.send(meth, uri, form: { image: HTTP::FormData::File.new(fixture_file_path) }) .send(meth, uri, form: { image: HTTP::FormData::File.new(fixture_file_path) })
verify_status(response.status, 200) verify_status(response, 200)
body = json_body(response) body = json_body(response)
verify_header(body["headers"], "Content-Type", "multipart/form-data") verify_header(body["headers"], "Content-Type", "multipart/form-data")
verify_header(body["headers"], "Expect", "100-continue") verify_header(body["headers"], "Expect", "100-continue")

View File

@ -7,7 +7,7 @@ module Requests
# uri = build_uri("/#{meth}") # uri = build_uri("/#{meth}")
# response = HTTPX.headers("transfer-encoding" => "chunked") # response = HTTPX.headers("transfer-encoding" => "chunked")
# .send(meth, uri, body: %w[this is a chunked response]) # .send(meth, uri, body: %w[this is a chunked response])
# verify_status(response.status, 200) # verify_status(response, 200)
# body = json_body(response) # body = json_body(response)
# verify_header(body["headers"], "Transfer-Encoding", "chunked") # verify_header(body["headers"], "Transfer-Encoding", "chunked")
# assert body.key?("data") # assert body.key?("data")