mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
added helper assertion helpers
This commit is contained in:
parent
808c351d72
commit
51cdf95aad
25
test/support/assertion_helpers.rb
Normal file
25
test/support/assertion_helpers.rb
Normal file
@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ResponseHelpers
|
||||
private
|
||||
|
||||
def verify_status(value, expect)
|
||||
assert value == expect, "status assertion failed: #{value} (expected: #{expect})"
|
||||
end
|
||||
|
||||
%w(header param).each do |meth|
|
||||
class_eval <<-DEFINE, __FILE__, __LINE__ + 1
|
||||
def verify_#{meth}(#{meth}s, key, expect)
|
||||
assert #{meth}s.key?(key), "#{meth}s don't contain the given key (" + key + ")"
|
||||
value = #{meth}s[key]
|
||||
assert value.start_with?(expect), "#{meth} assertion failed: " + key + "=" + value + " (expected: " + expect + ")"
|
||||
end
|
||||
DEFINE
|
||||
end
|
||||
|
||||
def verify_body_length(response, expect=response.headers["content-length"].to_i)
|
||||
len = response.body.to_s.bytesize
|
||||
assert len == expect, "length assertion failed: #{len} (expected: #{expect})"
|
||||
end
|
||||
|
||||
end
|
@ -3,6 +3,7 @@
|
||||
require_relative "../test_helper"
|
||||
|
||||
class HTTPTest < Minitest::Spec
|
||||
include ResponseHelpers
|
||||
|
||||
private
|
||||
|
||||
|
@ -5,9 +5,9 @@ module Requests
|
||||
def test_http_chunked_get
|
||||
uri = build_uri("/stream-bytes/30?chunk_size=5")
|
||||
response = HTTPX.get(uri)
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.headers["transfer-encoding"] == "chunked", "response hasn't been chunked"
|
||||
assert response.body.to_s.bytesize == 30, "didn't load the whole body"
|
||||
verify_status(response.status, 200)
|
||||
verify_header(response.headers, "transfer-encoding", "chunked")
|
||||
verify_body_length(response, 30)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -5,8 +5,8 @@ module Requests
|
||||
def test_http_get
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.get(uri)
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.body.to_s.bytesize == response.headers["content-length"].to_i, "didn't load the whole body"
|
||||
verify_status(response.status, 200)
|
||||
verify_body_length(response)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -5,9 +5,8 @@ module Requests
|
||||
def test_http_head
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.head(uri)
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.body.to_s.bytesize == 0, "there should be no body"
|
||||
verify_status(response.status, 200)
|
||||
verify_body_length(response, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -11,15 +11,14 @@ module Requests
|
||||
|
||||
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"
|
||||
verify_header(body["headers"], "Accept", "text/css")
|
||||
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"
|
||||
verify_header(body, "user-agent", "httpx.rb/#{HTTPX::VERSION}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -6,8 +6,8 @@ module Requests
|
||||
io = origin_io
|
||||
uri = build_uri("/")
|
||||
response = HTTPX.get(uri, io: io)
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.body.to_s.bytesize == response.headers["content-length"].to_i, "didn't load the whole body"
|
||||
verify_status(response.status, 200)
|
||||
verify_body_length(response)
|
||||
assert !io.closed?, "io should have been left open"
|
||||
ensure
|
||||
io.close if io
|
||||
|
@ -6,10 +6,12 @@ module Requests
|
||||
file = Tempfile.new(%w[cat .jpeg])
|
||||
uri = build_uri("/image")
|
||||
response = HTTPX.get(uri, headers: {"accept" => "image/jpeg"})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.headers["content-type"]== "image/jpeg", "content is not an image"
|
||||
verify_status(response.status, 200)
|
||||
verify_header(response.headers, "content-type", "image/jpeg")
|
||||
response.copy_to(file)
|
||||
assert file.size == response.headers["content-length"].to_i, "file should contain the content of response"
|
||||
verify_body_length(response)
|
||||
content_length = response.headers["content-length"].to_i
|
||||
assert file.size == content_length, "file should contain the content of response"
|
||||
ensure
|
||||
if file
|
||||
file.close
|
||||
@ -21,10 +23,11 @@ module Requests
|
||||
io = StringIO.new
|
||||
uri = build_uri("/image")
|
||||
response = HTTPX.get(uri, headers: {"accept" => "image/jpeg"})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
assert response.headers["content-type"]== "image/jpeg", "content is not an image"
|
||||
verify_status(response.status, 200)
|
||||
verify_header(response.headers, "content-type", "image/jpeg")
|
||||
response.copy_to(io)
|
||||
assert io.size == response.headers["content-length"].to_i, "file should contain the content of response"
|
||||
content_length = response.headers["content-length"].to_i
|
||||
assert io.size == content_length, "file should contain the content of response"
|
||||
ensure
|
||||
io.close if io
|
||||
end
|
||||
@ -50,11 +53,12 @@ module Requests
|
||||
end
|
||||
|
||||
response = HTTPX.get(uri, response_body_class: custom_body)
|
||||
assert response.status == 200, "status is unexpected"
|
||||
verify_status(response.status, 200)
|
||||
assert response.body.is_a?(custom_body), "body should be from custom type"
|
||||
file = response.body.file
|
||||
file.rewind
|
||||
assert file.size == response.headers["content-length"].to_i, "didn't buffer the whole body"
|
||||
content_length = response.headers["content-length"].to_i
|
||||
assert file.size == content_length, "didn't buffer the whole body"
|
||||
ensure
|
||||
response.close if response
|
||||
end
|
||||
|
@ -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"})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body.key?("args")
|
||||
assert body["args"].key?("q")
|
||||
@ -17,9 +17,9 @@ module Requests
|
||||
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"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"] == "application/x-www-form-urlencoded"
|
||||
verify_header(body["headers"], "Content-Type", "application/x-www-form-urlencoded")
|
||||
assert body.key?("form")
|
||||
assert body["form"].key?("foo")
|
||||
assert body["form"]["foo"] == "bar"
|
||||
@ -28,9 +28,9 @@ module Requests
|
||||
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"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("application/json")
|
||||
verify_header(body["headers"], "Content-Type", "application/json")
|
||||
assert body.key?("json")
|
||||
assert body["json"].key?("foo")
|
||||
assert body["json"]["foo"] == "bar"
|
||||
@ -39,9 +39,9 @@ module Requests
|
||||
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"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"] == "application/octet-stream"
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
assert body.key?("data")
|
||||
assert body["data"] == "data"
|
||||
end
|
||||
@ -49,9 +49,9 @@ 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)})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
|
||||
verify_header(body["headers"], "Content-Type", "multipart/form-data")
|
||||
assert body.key?("files")
|
||||
assert body["files"].key?("image")
|
||||
end
|
||||
@ -60,10 +60,10 @@ module Requests
|
||||
uri = build_uri("/#{meth}")
|
||||
response = HTTPX.headers("expect" => "100-continue")
|
||||
.send(meth, uri, form: {image: HTTP::FormData::File.new(fixture_file_path)})
|
||||
assert response.status == 200, "status is unexpected"
|
||||
verify_status(response.status, 200)
|
||||
body = json_body(response)
|
||||
assert body["headers"]["Content-Type"].start_with?("multipart/form-data")
|
||||
assert body["headers"]["Expect"].start_with?("100-continue")
|
||||
verify_header(body["headers"], "Content-Type", "multipart/form-data")
|
||||
verify_header(body["headers"], "Expect", "100-continue")
|
||||
assert body.key?("files")
|
||||
assert body["files"].key?("image")
|
||||
end
|
||||
|
@ -7,9 +7,9 @@ module Requests
|
||||
# uri = build_uri("/#{meth}")
|
||||
# response = HTTPX.headers("transfer-encoding" => "chunked")
|
||||
# .send(meth, uri, body: %w[this is a chunked response])
|
||||
# assert response.status == 200, "status is unexpected"
|
||||
# verify_status(response.status, 200)
|
||||
# body = json_body(response)
|
||||
# assert body["headers"]["Transfer-Encoding"] == "chunked"
|
||||
# verify_header(body["headers"], "Transfer-Encoding", "chunked")
|
||||
# assert body.key?("data")
|
||||
# assert body["data"] == "thisisachunkedresponse"
|
||||
# end
|
||||
|
Loading…
x
Reference in New Issue
Block a user