Skip to content

Commit

Permalink
Convert non-UTF-8 strings to UTF-8 if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolesUC committed Jun 1, 2023
1 parent 5a0de49 commit 22ce317
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/berkeley_library/util/uris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def uri_or_nil(url)
# replacing disallowed characters (including /) with percent-encodings as needed.
def path_escape(s)
raise ArgumentError, "Can't escape #{s.inspect}: not a string" unless s.respond_to?(:encoding)
raise ArgumentError, "Can't escape #{s.inspect}: expected #{UTF_8}, was #{s.encoding}" unless s.encoding == UTF_8

s = s.encode(UTF_8) unless s.encoding == UTF_8
''.tap do |escaped|
s.bytes.each do |b|
escaped << (should_escape?(b, :path_segment) ? '%%%02X' % b : b.chr)
Expand Down
22 changes: 8 additions & 14 deletions spec/berkeley_library/util/uris_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,14 @@ module BerkeleyLibrary::Util
expect { URIs.path_escape(str.bytes) }.to raise_error(ArgumentError)
end

it 'rejects non-UTF-8 strings' do
str = in_out.keys.last
expect { URIs.path_escape(str.encode(Encoding::Shift_JIS)) }.to raise_error(ArgumentError)
end

it 'accepts non-UTF-8 strings converted to UTF-8' do
in_str = in_out.keys.last
out_str = in_out[in_str]

# OK, we're really just testing String#encode here, but
# it's useful for documentation
in_str_sjis = in_str.encode(Encoding::Shift_JIS)
in_str_utf8 = in_str_sjis.encode(Encoding::UTF_8)
expect(URIs.path_escape(in_str_utf8)).to eq(out_str)
it 'converts non-UTF-8 strings to UTF-8' do
utf_16_be = Encoding.find('UTF-16BE')
aggregate_failures do
in_out.each do |in_str, out_str|
encoded = in_str.encode(utf_16_be)
expect(URIs.path_escape(encoded)).to eq(out_str)
end
end
end
end
end
Expand Down

0 comments on commit 22ce317

Please sign in to comment.