mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-11-28 00:02:22 -05:00
verify_status receives response and raises when not a proper response
This commit is contained in:
parent
d78230899b
commit
4be12daa00
@ -3,8 +3,9 @@
|
||||
module ResponseHelpers
|
||||
private
|
||||
|
||||
def verify_status(value, expect)
|
||||
assert value == expect, "status assertion failed: #{value} (expected: #{expect})"
|
||||
def verify_status(response, expect)
|
||||
raise response.status if response.is_a?(HTTPX::ErrorResponse)
|
||||
assert response.status == expect, "status assertion failed: #{response.status} (expected: #{expect})"
|
||||
end
|
||||
|
||||
%w[header param].each do |meth|
|
||||
|
||||
@ -5,7 +5,7 @@ module Requests
|
||||
def test_http_chunked_get
|
||||
uri = build_uri("/stream-bytes/30?chunk_size=5")
|
||||
response = HTTPX.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_header(response.headers, "transfer-encoding", "chunked")
|
||||
verify_body_length(response, 30)
|
||||
end
|
||||
|
||||
@ -5,7 +5,7 @@ module Requests
|
||||
def test_http_get
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
end
|
||||
|
||||
@ -5,7 +5,7 @@ module Requests
|
||||
def test_http_head
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.head(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response, 0)
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,7 +6,7 @@ module Requests
|
||||
io = origin_io
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.get(uri, io: io)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
assert !io.closed?, "io should have been left open"
|
||||
ensure
|
||||
|
||||
@ -5,24 +5,24 @@ module Requests
|
||||
module Authentication
|
||||
def test_plugin_basic_authentication
|
||||
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\"")
|
||||
|
||||
client = HTTPX.plugin(:basic_authentication)
|
||||
response = client.basic_authentication(user, pass).get(basic_auth_uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body, "authenticated", true)
|
||||
verify_header(body, "user", user)
|
||||
|
||||
invalid_response = client.basic_authentication(user, "fake").get(basic_auth_uri)
|
||||
verify_status(invalid_response.status, 401)
|
||||
verify_status(invalid_response, 401)
|
||||
end
|
||||
|
||||
def test_plugin_digest_authentication
|
||||
client = HTTPX.plugin(:digest_authentication).headers("cookie" => "fake=fake_value")
|
||||
response = client.digest_authentication(user, pass).get(digest_auth_uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body, "authenticated", true)
|
||||
verify_header(body, "user", user)
|
||||
|
||||
@ -7,13 +7,13 @@ module Requests
|
||||
url = "https://github.com"
|
||||
response1 = HTTPX.get(url)
|
||||
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"
|
||||
|
||||
client = HTTPX.plugin(:compression)
|
||||
response = client.get(url)
|
||||
skip if response.status == 429
|
||||
verify_status(response.status, 200)
|
||||
skip if response == 429
|
||||
verify_status(response, 200)
|
||||
verify_header(response.headers, "content-encoding", "gzip")
|
||||
end
|
||||
|
||||
@ -21,7 +21,7 @@ module Requests
|
||||
client = HTTPX.plugin(:compression)
|
||||
uri = build_uri("/gzip")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
assert body["gzipped"], "response should be gzipped"
|
||||
end
|
||||
@ -31,7 +31,7 @@ module Requests
|
||||
uri = build_uri("/post")
|
||||
response = client.headers("content-encoding" => "gzip")
|
||||
.post(uri, body: "a" * 8012)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
@ -42,7 +42,7 @@ module Requests
|
||||
client = HTTPX.plugin(:compression)
|
||||
uri = build_uri("/deflate")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
assert body["deflated"], "response should be deflated"
|
||||
end
|
||||
@ -52,7 +52,7 @@ module Requests
|
||||
uri = build_uri("/post")
|
||||
response = client.headers("content-encoding" => "deflate")
|
||||
.post(uri, body: "a" * 8012)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
@ -63,7 +63,7 @@ module Requests
|
||||
def test_plugin_compression_brotli
|
||||
client = HTTPX.plugin(:"compression/brotli")
|
||||
response = client.get("http://httpbin.org/brotli")
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
assert body["brotli"], "response should be deflated"
|
||||
end
|
||||
@ -73,7 +73,7 @@ module Requests
|
||||
uri = build_uri("/post")
|
||||
response = client.headers("content-encoding" => "br")
|
||||
.post(uri, body: "a" * 8012)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
|
||||
@ -23,7 +23,7 @@ module Requests
|
||||
session_cookies = { "a" => "b", "c" => "d" }
|
||||
session_uri = 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"
|
||||
response_cookies = session_response.cookie_jar
|
||||
|
||||
@ -7,12 +7,12 @@ module Requests
|
||||
module FollowRedirects
|
||||
def test_plugin_follow_redirects
|
||||
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)
|
||||
|
||||
client = HTTPX.plugin(:follow_redirects)
|
||||
redirect_response = client.get(redirect_uri)
|
||||
verify_status(redirect_response.status, 200)
|
||||
verify_status(redirect_response, 200)
|
||||
body = json_body(redirect_response)
|
||||
assert body.key?("url"), "url should be set"
|
||||
assert body["url"] == redirect_location, "url should have been the given redirection url"
|
||||
@ -22,20 +22,20 @@ module Requests
|
||||
client = HTTPX.plugin(:follow_redirects)
|
||||
|
||||
response = client.get(max_redirect_uri(3))
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
|
||||
response = client.get(max_redirect_uri(4))
|
||||
verify_status(response.status, 302)
|
||||
verify_status(response, 302)
|
||||
end
|
||||
|
||||
def test_plugin_follow_redirects_max_redirects
|
||||
client = HTTPX.plugin(:follow_redirects)
|
||||
|
||||
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))
|
||||
verify_status(response.status, 302)
|
||||
verify_status(response, 302)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@ -6,7 +6,7 @@ module Requests
|
||||
def test_plugin_h2c_disabled
|
||||
uri = build_uri("/get")
|
||||
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"
|
||||
end
|
||||
|
||||
@ -14,7 +14,7 @@ module Requests
|
||||
client = HTTPX.plugin(:h2c)
|
||||
uri = build_uri("/get")
|
||||
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"
|
||||
end
|
||||
end
|
||||
|
||||
@ -9,7 +9,7 @@ module Requests
|
||||
client = HTTPX.plugin(:proxy).with_proxy(uri: http_proxy)
|
||||
uri = build_uri("/get")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
|
||||
@ -17,7 +17,7 @@ module Requests
|
||||
client = HTTPX.plugin(:proxy).with_proxy(uri: socks4_proxy)
|
||||
uri = build_uri("/get")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
|
||||
@ -25,7 +25,7 @@ module Requests
|
||||
client = HTTPX.plugin(:proxy).with_proxy(uri: socks4a_proxy)
|
||||
uri = build_uri("/get")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
|
||||
@ -33,7 +33,7 @@ module Requests
|
||||
client = HTTPX.plugin(:proxy).with_proxy(uri: socks5_proxy)
|
||||
uri = build_uri("/get")
|
||||
response = client.get(uri)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,8 +6,8 @@ module Requests
|
||||
def test_plugin_push_promise_get
|
||||
client = HTTPX.plugin(:push_promise)
|
||||
html, css = client.get(push_html_uri, push_css_uri)
|
||||
verify_status(html.status, 200)
|
||||
verify_status(css.status, 200)
|
||||
verify_status(html, 200)
|
||||
verify_status(css, 200)
|
||||
verify_header(css.headers, "x-http2-push", "1")
|
||||
end
|
||||
|
||||
@ -15,8 +15,8 @@ module Requests
|
||||
client = HTTPX.plugin(:push_promise)
|
||||
.with(max_concurrent_requests: 100)
|
||||
html, css = client.get(push_html_uri, push_css_uri)
|
||||
verify_status(html.status, 200)
|
||||
verify_status(css.status, 200)
|
||||
verify_status(html, 200)
|
||||
verify_status(css, 200)
|
||||
verify_no_header(css.headers, "x-http2-push")
|
||||
end
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ module Requests
|
||||
file = Tempfile.new(%w[cat .jpeg])
|
||||
uri = build_uri("/image")
|
||||
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")
|
||||
response.copy_to(file)
|
||||
verify_body_length(response)
|
||||
@ -23,7 +23,7 @@ module Requests
|
||||
io = StringIO.new
|
||||
uri = build_uri("/image")
|
||||
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")
|
||||
response.copy_to(io)
|
||||
content_length = response.headers["content-length"].to_i
|
||||
@ -53,7 +53,7 @@ module Requests
|
||||
end
|
||||
|
||||
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"
|
||||
file = response.body.file
|
||||
file.rewind
|
||||
|
||||
@ -6,7 +6,7 @@ module Requests
|
||||
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.status, 200)
|
||||
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"))
|
||||
@ -15,7 +15,7 @@ module Requests
|
||||
define_method :"test_#{meth}_form_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, form: { "foo" => "bar" })
|
||||
verify_status(response.status, 200)
|
||||
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")
|
||||
@ -24,7 +24,7 @@ module Requests
|
||||
define_method :"test_#{meth}_json_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, json: { "foo" => "bar" })
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/json")
|
||||
verify_uploaded(body, "json", "foo" => "bar")
|
||||
@ -33,7 +33,7 @@ module Requests
|
||||
define_method :"test_#{meth}_body_params" do
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.send(meth, uri, body: "data")
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
verify_uploaded(body, "data", "data")
|
||||
@ -42,7 +42,7 @@ module Requests
|
||||
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.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
verify_uploaded(body, "data", "data")
|
||||
@ -58,7 +58,7 @@ module Requests
|
||||
# y << "a"
|
||||
# end
|
||||
# response = HTTPX.send(meth, uri, body: body)
|
||||
# verify_status(response.status, 200)
|
||||
# verify_status(response, 200)
|
||||
# body = json_body(response)
|
||||
# verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
# verify_uploaded(body, "data", "data")
|
||||
@ -68,7 +68,7 @@ module Requests
|
||||
uri = build_uri("/#{meth}")
|
||||
body = StringIO.new("data")
|
||||
response = HTTPX.send(meth, uri, body: body)
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
verify_uploaded(body, "data", "data")
|
||||
@ -77,7 +77,7 @@ module Requests
|
||||
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) })
|
||||
verify_status(response.status, 200)
|
||||
verify_status(response, 200)
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "multipart/form-data")
|
||||
verify_uploaded_image(body)
|
||||
@ -87,7 +87,7 @@ module Requests
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.headers("expect" => "100-continue")
|
||||
.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)
|
||||
verify_header(body["headers"], "Content-Type", "multipart/form-data")
|
||||
verify_header(body["headers"], "Expect", "100-continue")
|
||||
|
||||
@ -7,7 +7,7 @@ module Requests
|
||||
# uri = build_uri("/#{meth}")
|
||||
# response = HTTPX.headers("transfer-encoding" => "chunked")
|
||||
# .send(meth, uri, body: %w[this is a chunked response])
|
||||
# verify_status(response.status, 200)
|
||||
# verify_status(response, 200)
|
||||
# body = json_body(response)
|
||||
# verify_header(body["headers"], "Transfer-Encoding", "chunked")
|
||||
# assert body.key?("data")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user