mobed all http request tests to request helpers

This commit is contained in:
HoneyryderChuck 2017-12-11 14:59:40 +00:00
parent 15a5ffffdd
commit daa2860b60
10 changed files with 267 additions and 204 deletions

View File

@ -3,189 +3,20 @@
require_relative "test_helper"
class HTTP1Test < Minitest::Spec
include Requests
include Head
include Get
include ChunkedGet
include WithBody
include WithChunkedBody
include Headers
include ResponseBody
include IO
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"
end
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"
end
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"
end
%w[post put patch delete].each do |meth|
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"
body = json_body(response)
assert body.key?("args")
assert body["args"].key?("q")
assert body["args"]["q"] == "this is a test"
assert body["url"] == build_uri("/#{meth}?q=this+is+a+test")
end
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
#define_method :"test_#{meth}_chunked_body_params" do
# 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"
# body = json_body(response)
# assert body["headers"]["Transfer-Encoding"] == "chunked"
# assert body.key?("data")
# assert body["data"] == "thisisachunkedresponse"
#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
def test_http_io
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"
assert !io.closed?, "io should have been left open"
ensure
io.close if io
end
def test_http_copy_to_file
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"
response.copy_to(file)
assert file.size == response.headers["content-length"].to_i, "file should contain the content of response"
ensure
if file
file.close
file.unlink
end
end
def test_http_copy_to_io
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"
response.copy_to(io)
assert io.size == response.headers["content-length"].to_i, "file should contain the content of response"
ensure
io.close if io
end
def test_http_buffer_to_custom
uri = build_uri("/")
custom_body = Class.new do
attr_reader :file
def initialize(response, **)
@file = Tempfile.new
end
def <<(data)
@file << data
end
def close
return unless @file
@file.close
@file.unlink
end
end
response = HTTPX.get(uri, response_body_class: custom_body)
assert response.status == 200, "status is unexpected"
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"
ensure
response.close if response
end
private
def json_body(response)
JSON.parse(response.body.to_s)
end
def build_uri(suffix="/")
"#{origin}#{suffix || "/"}"
end
@ -193,29 +24,4 @@ class HTTP1Test < Minitest::Spec
def origin
"http://nghttp2.org/httpbin"
end
def origin_io
uri = URI(origin)
case uri.scheme
when "http"
TCPSocket.new(uri.host, uri.port)
when "https"
sock = TCPSocket.new(uri.host, uri.port)
OpenSSL::SSL::SSLSocket.new(sock)
else
raise "#{uri.scheme}: unsupported scheme"
end
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

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Requests
module ChunkedGet
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"
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Requests
module Get
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"
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Requests
module Head
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"
end
end
end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Requests
module Headers
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
end
end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Requests
module IO
def test_http_io
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"
assert !io.closed?, "io should have been left open"
ensure
io.close if io
end
end
private
def origin_io
uri = URI(origin)
case uri.scheme
when "http"
TCPSocket.new(uri.host, uri.port)
when "https"
sock = TCPSocket.new(uri.host, uri.port)
OpenSSL::SSL::SSLSocket.new(sock)
else
raise "#{uri.scheme}: unsupported scheme"
end
end
end

View File

@ -0,0 +1,62 @@
# frozen_string_literal: true
module Requests
module ResponseBody
def test_http_copy_to_file
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"
response.copy_to(file)
assert file.size == response.headers["content-length"].to_i, "file should contain the content of response"
ensure
if file
file.close
file.unlink
end
end
def test_http_copy_to_io
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"
response.copy_to(io)
assert io.size == response.headers["content-length"].to_i, "file should contain the content of response"
ensure
io.close if io
end
def test_http_buffer_to_custom
uri = build_uri("/")
custom_body = Class.new do
attr_reader :file
def initialize(response, **)
@file = Tempfile.new
end
def <<(data)
@file << data
end
def close
return unless @file
@file.close
@file.unlink
end
end
response = HTTPX.get(uri, response_body_class: custom_body)
assert response.status == 200, "status is unexpected"
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"
ensure
response.close if response
end
end
end

View File

@ -0,0 +1,78 @@
# frozen_string_literal: true
module Requests
module WithBody
%w[post put patch delete].each do |meth|
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"
body = json_body(response)
assert body.key?("args")
assert body["args"].key?("q")
assert body["args"]["q"] == "this is a test"
assert body["url"] == build_uri("/#{meth}?q=this+is+a+test")
end
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
private
def json_body(response)
JSON.parse(response.body.to_s)
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
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
module Requests
module WithChunkedBody
%w[post put patch delete].each do |meth|
# define_method :"test_#{meth}_chunked_body_params" do
# 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"
# body = json_body(response)
# assert body["headers"]["Transfer-Encoding"] == "chunked"
# assert body.key?("data")
# assert body["data"] == "thisisachunkedresponse"
# end
end
end
end

View File

@ -5,3 +5,5 @@ require "minitest/autorun"
require "httpx"
Dir[File.join(".", "test", "support", "**", "*.rb")].each { |f| require f }