Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/context_dev/file_part.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "pathname"

module ContextDev
class FilePart
# @return [Pathname, StringIO, IO, String]
Expand Down Expand Up @@ -49,7 +51,7 @@ def initialize(content, filename: nil, content_type: nil)
in [nil, Pathname]
content.basename.to_path
in [nil, IO]
content.to_path
content.to_path&.then { ::File.basename(_1) }
else
filename
end
Expand Down
5 changes: 4 additions & 1 deletion lib/context_dev/internal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@ def encode_query_params(query)
in ContextDev::FilePart unless val.filename.nil?
filename = encode_path(val.filename)
y << "; filename=\"#{filename}\""
in Pathname | IO
in Pathname
filename = encode_path(::File.basename(val.to_path))
y << "; filename=\"#{filename}\""
in IO unless val.to_path.nil?
filename = encode_path(::File.basename(val.to_path))
y << "; filename=\"#{filename}\""
else
Expand Down
15 changes: 15 additions & 0 deletions test/context_dev/file_part_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@ def test_to_json
assert_equal(text.to_json, filepart.to_json)
assert_equal(text.to_yaml, filepart.to_yaml)
end

def test_filename_basename_for_io
file = File.open(__FILE__)
filepart = ContextDev::FilePart.new(file)
assert_equal(File.basename(__FILE__), filepart.filename)
file.close
end

def test_pathless_io
r, w = IO.pipe
filepart = ContextDev::FilePart.new(r)
assert_nil(filepart.filename)
r.close
w.close
end
end
15 changes: 15 additions & 0 deletions test/context_dev/internal/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ def test_file_encode
end
end

def test_pathless_io_encode
r, w = IO.pipe
w.write("streamed data")
w.close
headers = {"content-type" => "multipart/form-data"}
encoded = ContextDev::Internal::Util.encode_content(headers, r)
cgi = FakeCGI.new(*encoded)
io = cgi[""]
assert_pattern do
io.original_filename => ""
io.read => "streamed data"
end
r.close
end

def test_hash_encode
headers = {"content-type" => "multipart/form-data"}
cases = {
Expand Down