diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index b7de4c1..354ad22 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -1,38 +1,38 @@ # frozen_string_literal: true require 'uri' # # uriescape.rb # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions newfunction(:uriescape, type: :rvalue, doc: <<-DOC @summary Urlencodes a string or array of strings. Requires either a single string or an array as an input. @return [String] a string that contains the converted value DOC ) do |arguments| raise(Puppet::ParseError, "uriescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'uriescape(): Requires either array or string to work with') end result = if value.is_a?(Array) # Numbers in Puppet are often string-encoded which is troublesome ... - value.map { |i| i.is_a?(String) ? URI.escape(i) : i } + value.map { |i| i.is_a?(String) ? URI::DEFAULT_PARSER.escape(i) : i } else - URI.escape(value) + URI::DEFAULT_PARSER.escape(value) end return result end end # vim: set ts=2 sw=2 et : diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb index 3ac632a..81b27c3 100644 --- a/spec/functions/uriescape_spec.rb +++ b/spec/functions/uriescape_spec.rb @@ -1,38 +1,38 @@ # frozen_string_literal: true require 'spec_helper' describe 'uriescape' do describe 'signature validation' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { pending('Current implementation ignores parameters after the first.') is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } end describe 'handling normal strings' do - it 'calls ruby\'s URI.escape function' do - expect(URI).to receive(:escape).with('uri_string').and_return('escaped_uri_string').once + it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do + expect(URI::DEFAULT_PARSER).to receive(:escape).with('uri_string').and_return('escaped_uri_string').once is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') end end describe 'handling classes derived from String' do - it 'calls ruby\'s URI.escape function' do + it 'calls ruby\'s URI::DEFAULT_PARSER.escape function' do uri_string = AlsoString.new('uri_string') - expect(URI).to receive(:escape).with(uri_string).and_return('escaped_uri_string').once + expect(URI::DEFAULT_PARSER).to receive(:escape).with(uri_string).and_return('escaped_uri_string').once is_expected.to run.with_params(uri_string).and_return('escaped_uri_string') end end describe 'strings in arrays handling' do it { is_expected.to run.with_params([]).and_return([]) } it { is_expected.to run.with_params(['one}', 'two']).and_return(['one%7D', 'two']) } it { is_expected.to run.with_params(['one}', 1, true, {}, 'two']).and_return(['one%7D', 1, true, {}, 'two']) } end end