diff --git a/regression_tests/bug_0_14_1_test.rb b/regression_tests/bug_0_14_1_test.rb new file mode 100644 index 00000000..3587a881 --- /dev/null +++ b/regression_tests/bug_0_14_1_test.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "test_helper" +require "support/http_helpers" + +class Bug_0_14_1_Test < Minitest::Test + include HTTPHelpers + + def test_multipart_can_have_arbitrary_conntent_type + uri = "https://#{httpbin}/post" + + response = HTTPX.plugin(:multipart) + .post(uri, form: { + image: { + content_type: "image/png", + body: File.new(fixture_file_path), + }, + }) + verify_status(response, 200) + body = json_body(response) + verify_header(body["headers"], "Content-Type", "multipart/form-data") + # can't really test the filename, but if it's in the files field, + # then it was a file upload + verify_uploaded_image(body, "image", "image/png") + end + + private + + def origin(orig = httpbin) + "http://#{orig}" + end +end diff --git a/regression_tests/bug_0_14_2_test.rb b/regression_tests/bug_0_14_2_test.rb new file mode 100644 index 00000000..83788f14 --- /dev/null +++ b/regression_tests/bug_0_14_2_test.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "test_helper" +require "support/http_helpers" + +class Bug_0_14_2_Test < Minitest::Test + include HTTPHelpers + + def test_multipart_can_have_arbitrary_filename + uri = "https://#{httpbin}/post" + + response = HTTPX.plugin(:multipart) + .post(uri, form: { + image: { + filename: "weird-al-jankovic", + body: File.new(fixture_file_path), + }, + }) + verify_status(response, 200) + body = json_body(response) + verify_header(body["headers"], "Content-Type", "multipart/form-data") + # can't really test the filename, but if it's in the files field, + # then it was a file upload + verify_uploaded_image(body, "image", "image/jpeg") + end + + private + + def origin(orig = httpbin) + "http://#{orig}" + end +end diff --git a/test/support/assertion_helpers.rb b/test/support/assertion_helpers.rb index fe0ca6e6..491eede8 100644 --- a/test/support/assertion_helpers.rb +++ b/test/support/assertion_helpers.rb @@ -64,4 +64,31 @@ module ResponseHelpers raise "unexpected expectation (#{expectation})" end end + + # test files + + def verify_uploaded_image(body, key, mime_type, skip_verify_data: false) + assert body.key?("files"), "there were no files uploaded" + assert body["files"].key?(key), "there is no image in the file" + # checking mime-type is a bit leaky, as httpbin displays the base64-encoded data + return if skip_verify_data + + assert body["files"][key].start_with?("data:#{mime_type}"), "data was wrongly encoded (#{body["files"][key][0..64]})" + end + + def fixture + File.read(fixture_file_path, encoding: Encoding::BINARY) + end + + def fixture_name + File.basename(fixture_file_path) + end + + def fixture_file_name + "image.jpg" + end + + def fixture_file_path + File.join("test", "support", "fixtures", fixture_file_name) + end end diff --git a/test/support/requests/plugins/multipart.rb b/test/support/requests/plugins/multipart.rb index 30631543..1c14e780 100644 --- a/test/support/requests/plugins/multipart.rb +++ b/test/support/requests/plugins/multipart.rb @@ -219,31 +219,6 @@ module Requests assert check_error[retries_response], "expected #{retries_response} to be an error response" assert retries_session.calls == 1, "expect request to be retried 1 time (was #{retries_session.calls})" end - - def fixture - File.read(fixture_file_path, encoding: Encoding::BINARY) - end - - def fixture_name - File.basename(fixture_file_path) - end - - def fixture_file_name - "image.jpg" - end - - def fixture_file_path - File.join("test", "support", "fixtures", fixture_file_name) - end - - def verify_uploaded_image(body, key, mime_type, skip_verify_data: false) - assert body.key?("files"), "there were no files uploaded" - assert body["files"].key?(key), "there is no image in the file" - # checking mime-type is a bit leaky, as httpbin displays the base64-encoded data - return if skip_verify_data - - assert body["files"][key].start_with?("data:#{mime_type}"), "data was wrongly encoded (#{body["files"][key][0..64]})" - end end end end