From 51cdf95aadd9effb9d1763c3aaf441b2af9b76ee Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Thu, 14 Dec 2017 01:19:47 +0000 Subject: [PATCH] added helper assertion helpers --- test/support/assertion_helpers.rb | 25 ++++++++++++++++++++++ test/support/http_test.rb | 1 + test/support/requests/chunked_get.rb | 6 +++--- test/support/requests/get.rb | 4 ++-- test/support/requests/head.rb | 5 ++--- test/support/requests/headers.rb | 5 ++--- test/support/requests/io.rb | 4 ++-- test/support/requests/response_body.rb | 20 ++++++++++------- test/support/requests/with_body.rb | 24 ++++++++++----------- test/support/requests/with_chunked_body.rb | 4 ++-- 10 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 test/support/assertion_helpers.rb diff --git a/test/support/assertion_helpers.rb b/test/support/assertion_helpers.rb new file mode 100644 index 00000000..44d75cfc --- /dev/null +++ b/test/support/assertion_helpers.rb @@ -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 diff --git a/test/support/http_test.rb b/test/support/http_test.rb index fe48ad8d..cfcbaca5 100644 --- a/test/support/http_test.rb +++ b/test/support/http_test.rb @@ -3,6 +3,7 @@ require_relative "../test_helper" class HTTPTest < Minitest::Spec + include ResponseHelpers private diff --git a/test/support/requests/chunked_get.rb b/test/support/requests/chunked_get.rb index bd138d76..71b1046e 100644 --- a/test/support/requests/chunked_get.rb +++ b/test/support/requests/chunked_get.rb @@ -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 diff --git a/test/support/requests/get.rb b/test/support/requests/get.rb index 9c13d0d1..36f5ef5a 100644 --- a/test/support/requests/get.rb +++ b/test/support/requests/get.rb @@ -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 diff --git a/test/support/requests/head.rb b/test/support/requests/head.rb index acfc52c4..5b0a37b5 100644 --- a/test/support/requests/head.rb +++ b/test/support/requests/head.rb @@ -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 - diff --git a/test/support/requests/headers.rb b/test/support/requests/headers.rb index 49529625..9d579ea0 100644 --- a/test/support/requests/headers.rb +++ b/test/support/requests/headers.rb @@ -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 diff --git a/test/support/requests/io.rb b/test/support/requests/io.rb index 950c83ce..eee827d1 100644 --- a/test/support/requests/io.rb +++ b/test/support/requests/io.rb @@ -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 diff --git a/test/support/requests/response_body.rb b/test/support/requests/response_body.rb index fb148f29..37181588 100644 --- a/test/support/requests/response_body.rb +++ b/test/support/requests/response_body.rb @@ -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 diff --git a/test/support/requests/with_body.rb b/test/support/requests/with_body.rb index 374f3ee3..e679093c 100644 --- a/test/support/requests/with_body.rb +++ b/test/support/requests/with_body.rb @@ -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 diff --git a/test/support/requests/with_chunked_body.rb b/test/support/requests/with_chunked_body.rb index 44d3f19b..8bee4a57 100644 --- a/test/support/requests/with_chunked_body.rb +++ b/test/support/requests/with_chunked_body.rb @@ -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