|
| 1 | +require_relative '../../spec_helper' |
| 2 | + |
| 3 | +ruby_version_is ""..."3.5" do |
| 4 | + require 'cgi' |
| 5 | +end |
| 6 | +ruby_version_is "3.5" do |
| 7 | + require 'cgi/escape' |
| 8 | +end |
| 9 | + |
| 10 | +describe "CGI.unescapeURIComponent" do |
| 11 | + it "unescapes whitespace" do |
| 12 | + string = "%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93" |
| 13 | + CGI.unescapeURIComponent(string).should == "&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93" |
| 14 | + end |
| 15 | + |
| 16 | + it "does not unescape with unreserved characters" do |
| 17 | + string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" |
| 18 | + CGI.unescapeURIComponent(string).should == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" |
| 19 | + end |
| 20 | + |
| 21 | + it "can produce String with invalid encoding" do |
| 22 | + string = CGI.unescapeURIComponent("%C0%3C%3C") |
| 23 | + string.should == "\xC0<<" |
| 24 | + string.encoding.should == Encoding::UTF_8 |
| 25 | + end |
| 26 | + |
| 27 | + it "processes String bytes one by one, not characters" do |
| 28 | + CGI.unescapeURIComponent("%CE%B2").should == "β" # "β" bytes representation is CE B2 |
| 29 | + end |
| 30 | + |
| 31 | + it "raises a TypeError with nil" do |
| 32 | + -> { |
| 33 | + CGI.unescapeURIComponent(nil) |
| 34 | + }.should raise_error(TypeError, 'no implicit conversion of nil into String') |
| 35 | + end |
| 36 | + |
| 37 | + it "unencodes empty string" do |
| 38 | + CGI.unescapeURIComponent("").should == "" |
| 39 | + end |
| 40 | + |
| 41 | + it "unencodes single whitespace" do |
| 42 | + CGI.unescapeURIComponent("%20").should == " " |
| 43 | + end |
| 44 | + |
| 45 | + it "unencodes double whitespace" do |
| 46 | + CGI.unescapeURIComponent("%20%20").should == " " |
| 47 | + end |
| 48 | + |
| 49 | + it "does not preserve encoding" do |
| 50 | + string = "whatever".encode("ASCII-8BIT") |
| 51 | + CGI.unescapeURIComponent(string).encoding.should == Encoding::UTF_8 |
| 52 | + end |
| 53 | + |
| 54 | + it "decodes using a specified Encoding" do |
| 55 | + string = CGI.unescapeURIComponent("%D2%3C%3C", "ISO-8859-1") |
| 56 | + string.encoding.should == Encoding::ISO_8859_1 |
| 57 | + string.should == "Ò<<".encode("ISO-8859-1") |
| 58 | + end |
| 59 | + |
| 60 | + it "uses implicit type conversion to String" do |
| 61 | + object = Object.new |
| 62 | + def object.to_str |
| 63 | + "a%20b" |
| 64 | + end |
| 65 | + |
| 66 | + CGI.unescapeURIComponent(object).should == "a b" |
| 67 | + end |
| 68 | +end |
0 commit comments