From c61007ba0f81759f6cf7e5e7c94c70961e8c1a50 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Thu, 27 May 2021 17:44:28 +0100 Subject: [PATCH] do not forget the filename in those multipart spoofs --- lib/httpx/plugins/multipart/part.rb | 2 +- sig/plugins/multipart.rbs | 2 +- test/support/requests/plugins/multipart.rb | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/httpx/plugins/multipart/part.rb b/lib/httpx/plugins/multipart/part.rb index 737b1a59..575b125d 100644 --- a/lib/httpx/plugins/multipart/part.rb +++ b/lib/httpx/plugins/multipart/part.rb @@ -26,7 +26,7 @@ module HTTPX content_type ||= MimeTypeDetector.call(value, filename) || "application/octet-stream" [value, content_type, filename] else - [StringIO.new(value.to_s), content_type || "text/plain"] + [StringIO.new(value.to_s), content_type || "text/plain", filename] end end end diff --git a/sig/plugins/multipart.rbs b/sig/plugins/multipart.rbs index 62399621..0aa2903a 100644 --- a/sig/plugins/multipart.rbs +++ b/sig/plugins/multipart.rbs @@ -33,7 +33,7 @@ module HTTPX end module Part - def self?.call: (multipart_nested_value) -> ([_Reader, String, String?] | [_Reader, String]) + def self?.call: (multipart_nested_value) -> ([_Reader, String, String?]) end module MimeTypeDetector diff --git a/test/support/requests/plugins/multipart.rb b/test/support/requests/plugins/multipart.rb index 2fe72eb1..8c5ba4bd 100644 --- a/test/support/requests/plugins/multipart.rb +++ b/test/support/requests/plugins/multipart.rb @@ -174,7 +174,7 @@ module Requests body = json_body(response) verify_header(body["headers"], "Content-Type", "multipart/form-data") # httpbin accepts the spoofed part, but it wipes our the content-type header - verify_uploaded(body, "form", "image" => "spoofpeg") + verify_uploaded_image(body, "image", "spoofpeg", skip_verify_data: true) end end @@ -209,10 +209,12 @@ module Requests File.join("test", "support", "fixtures", fixture_file_name) end - def verify_uploaded_image(body, key, mime_type) + 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