require_relative '../../spec_helper' ruby_version_is ""..."4.0" do require 'cgi' end ruby_version_is "4.0" do require 'cgi/escape' end describe "CGI.unescapeURIComponent" do it "decodes any percent-encoded octets to their corresponding bytes according to RFC 3986" do string = (0x00..0xff).map { |i| "%%%02x" % i }.join expected = (0x00..0xff).map { |i| i.chr }.join.force_encoding(Encoding::UTF_8) CGI.unescapeURIComponent(string).should == expected end it "disregards case of characters in a percent-encoding triplet" do CGI.unescapeURIComponent("%CE%B2abc").should == "βabc" CGI.unescapeURIComponent("%ce%b2ABC").should == "βABC" end it "leaves any non-percent-encoded characters as-is" do string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ:/?#[]@!$&'()*+,;=\t\x0D\xFFβᛉ▒90%" decoded = CGI.unescapeURIComponent(string) decoded.should == string string.should_not.equal?(decoded) end it "leaves sequences which can't be a percent-encoded octet as-is" do string = "%AZ%B" decoded = CGI.unescapeURIComponent(string) decoded.should == string string.should_not.equal?(decoded) end it "creates a String with the specified target Encoding" do string = CGI.unescapeURIComponent("%D2%3C%3CABC", Encoding::ISO_8859_1) string.encoding.should == Encoding::ISO_8859_1 string.should == "Ò< { CGI.unescapeURIComponent("ABC", "ISO-JOKE-1") }.should raise_error(ArgumentError, "unknown encoding name - ISO-JOKE-1") end ruby_version_is ""..."4.0" do it "uses CGI.accept_charset as the default target encoding" do original_charset = CGI.accept_charset CGI.accept_charset = "ISO-8859-1" decoded = CGI.unescapeURIComponent("%D2%3C%3CABC") decoded.should == "Ò< { CGI.unescapeURIComponent(nil) }.should raise_error(TypeError, "no implicit conversion of nil into String") end it "uses implicit type conversion to String" do object = Object.new def object.to_str "a%20b" end CGI.unescapeURIComponent(object).should == "a b" end end