|
| 1 | +require_relative '../../spec_helper' |
| 2 | + |
| 3 | +ruby_version_is ""..."4.0" do |
| 4 | + require 'cgi' |
| 5 | +end |
| 6 | +ruby_version_is "4.0" do |
| 7 | + require 'cgi/escape' |
| 8 | +end |
| 9 | + |
| 10 | +describe "CGI.unescapeURIComponent" do |
| 11 | + it "decodes any percent-encoded octets to their corresponding bytes according to RFC 3986" do |
| 12 | + string = (0x00..0xff).map { |i| "%%%02x" % i }.join |
| 13 | + expected = (0x00..0xff).map { |i| i.chr }.join.force_encoding(Encoding::UTF_8) |
| 14 | + CGI.unescapeURIComponent(string).should == expected |
| 15 | + end |
| 16 | + |
| 17 | + it "disregards case of characters in a percent-encoding triplet" do |
| 18 | + CGI.unescapeURIComponent("%CE%B2abc").should == "βabc" |
| 19 | + CGI.unescapeURIComponent("%ce%b2ABC").should == "βABC" |
| 20 | + end |
| 21 | + |
| 22 | + it "leaves any non-percent-encoded characters as-is" do |
| 23 | + string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ:/?#[]@!$&'()*+,;=\t\x0D\xFFβᛉ▒90%" |
| 24 | + decoded = CGI.unescapeURIComponent(string) |
| 25 | + decoded.should == string |
| 26 | + string.should_not.equal?(decoded) |
| 27 | + end |
| 28 | + |
| 29 | + it "leaves sequences which can't be a percent-encoded octet as-is" do |
| 30 | + string = "%AZ%B" |
| 31 | + decoded = CGI.unescapeURIComponent(string) |
| 32 | + decoded.should == string |
| 33 | + string.should_not.equal?(decoded) |
| 34 | + end |
| 35 | + |
| 36 | + it "creates a String with the specified target Encoding" do |
| 37 | + string = CGI.unescapeURIComponent("%D2%3C%3CABC", Encoding::ISO_8859_1) |
| 38 | + string.encoding.should == Encoding::ISO_8859_1 |
| 39 | + string.should == "Ò<<ABC".encode("ISO-8859-1") |
| 40 | + end |
| 41 | + |
| 42 | + it "accepts a string name of an Encoding" do |
| 43 | + CGI.unescapeURIComponent("%D2%3C%3CABC", "ISO-8859-1").should == "Ò<<ABC".encode("ISO-8859-1") |
| 44 | + end |
| 45 | + |
| 46 | + it "raises ArgumentError if specified encoding is unknown" do |
| 47 | + -> { CGI.unescapeURIComponent("ABC", "ISO-JOKE-1") }.should raise_error(ArgumentError, "unknown encoding name - ISO-JOKE-1") |
| 48 | + end |
| 49 | + |
| 50 | + ruby_version_is ""..."4.0" do |
| 51 | + it "uses CGI.accept_charset as the default target encoding" do |
| 52 | + original_charset = CGI.accept_charset |
| 53 | + CGI.accept_charset = "ISO-8859-1" |
| 54 | + decoded = CGI.unescapeURIComponent("%D2%3C%3CABC") |
| 55 | + decoded.should == "Ò<<ABC".encode("ISO-8859-1") |
| 56 | + decoded.encoding.should == Encoding::ISO_8859_1 |
| 57 | + ensure |
| 58 | + CGI.accept_charset = original_charset |
| 59 | + end |
| 60 | + |
| 61 | + it "has CGI.accept_charset as UTF-8 by default" do |
| 62 | + decoded = CGI.unescapeURIComponent("%CE%B2ABC") |
| 63 | + decoded.should == "βABC" |
| 64 | + decoded.encoding.should == Encoding::UTF_8 |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + ruby_version_is "4.0" do |
| 69 | + # "cgi/escape" does not have methods to access @@accept_charset. |
| 70 | + # Full "cgi" gem provides them, allowing to possibly change it. |
| 71 | + it "uses CGI's @@accept_charset as the default target encoding" do |
| 72 | + original_charset = CGI.class_variable_get(:@@accept_charset) |
| 73 | + CGI.class_variable_set(:@@accept_charset, "ISO-8859-1") |
| 74 | + decoded = CGI.unescapeURIComponent("%D2%3C%3CABC") |
| 75 | + decoded.should == "Ò<<ABC".encode("ISO-8859-1") |
| 76 | + decoded.encoding.should == Encoding::ISO_8859_1 |
| 77 | + ensure |
| 78 | + CGI.class_variable_set(:@@accept_charset, original_charset) |
| 79 | + end |
| 80 | + |
| 81 | + it "has CGI's @@accept_charset as UTF-8 by default" do |
| 82 | + decoded = CGI.unescapeURIComponent("%CE%B2ABC") |
| 83 | + decoded.should == "βABC" |
| 84 | + decoded.encoding.should == Encoding::UTF_8 |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "when source string specifies octets invalid in target encoding" do |
| 89 | + it "uses source string's encoding" do |
| 90 | + string = "%A2%A6%A3".encode(Encoding::SHIFT_JIS) |
| 91 | + decoded = CGI.unescapeURIComponent(string, Encoding::US_ASCII) |
| 92 | + decoded.encoding.should == Encoding::SHIFT_JIS |
| 93 | + decoded.should == "「ヲ」".encode(Encoding::SHIFT_JIS) |
| 94 | + decoded.valid_encoding?.should be_true |
| 95 | + end |
| 96 | + |
| 97 | + it "uses source string's encoding even if it's also invalid" do |
| 98 | + string = "%FF".encode(Encoding::US_ASCII) |
| 99 | + decoded = CGI.unescapeURIComponent(string, Encoding::SHIFT_JIS) |
| 100 | + decoded.encoding.should == Encoding::US_ASCII |
| 101 | + decoded.should == "\xFF".dup.force_encoding(Encoding::US_ASCII) |
| 102 | + decoded.valid_encoding?.should be_false |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + it "decodes an empty string as an empty string with target encoding" do |
| 107 | + string = "".encode(Encoding::BINARY) |
| 108 | + decoded = CGI.unescapeURIComponent(string, "UTF-8") |
| 109 | + decoded.should == "" |
| 110 | + decoded.encoding.should == Encoding::UTF_8 |
| 111 | + string.should_not.equal?(decoded) |
| 112 | + end |
| 113 | + |
| 114 | + it "raises a TypeError with nil" do |
| 115 | + -> { |
| 116 | + CGI.unescapeURIComponent(nil) |
| 117 | + }.should raise_error(TypeError, "no implicit conversion of nil into String") |
| 118 | + end |
| 119 | + |
| 120 | + it "uses implicit type conversion to String" do |
| 121 | + object = Object.new |
| 122 | + def object.to_str |
| 123 | + "a%20b" |
| 124 | + end |
| 125 | + |
| 126 | + CGI.unescapeURIComponent(object).should == "a b" |
| 127 | + end |
| 128 | +end |
0 commit comments