From cc2162cb76d9d77b8a7d05ffad9f02d19bb9c91e Mon Sep 17 00:00:00 2001 From: mario Date: Tue, 19 Sep 2023 15:32:48 +0200 Subject: [PATCH] add specs for CGI.escapeURIComponent https://github.com/ruby/spec/issues/1016 --- library/cgi/escapeURIComponent_spec.rb | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 library/cgi/escapeURIComponent_spec.rb diff --git a/library/cgi/escapeURIComponent_spec.rb b/library/cgi/escapeURIComponent_spec.rb new file mode 100644 index 0000000000..ff95f9d751 --- /dev/null +++ b/library/cgi/escapeURIComponent_spec.rb @@ -0,0 +1,41 @@ +require_relative '../../spec_helper' +require 'cgi' + +describe "CGI.escapeURIComponent" do + ruby_version_is "3.2" do + it "escapes whitespace" do + str = "&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93" + CGI.escapeURIComponent(str).should == '%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93' + end + + it "does not escape with unreserved characters" do + str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" + CGI.escapeURIComponent(str).should == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" + end + + it "with invalid bytesequence" do + str = "\xC0\<\<".force_encoding("UTF-8") + CGI.escapeURIComponent(str).should == "%C0%3C%3C" + end + + it "nil raises a TypeError" do + -> { CGI.escapeURIComponent(nil) }.should raise_error(TypeError) + end + + it "empty string" do + CGI.escapeURIComponent("").should == "" + end + + it "single whitespace" do + CGI.escapeURIComponent(" ").should == "%20" + end + + it "double whitespace" do + CGI.escapeURIComponent(" ").should == "%20%20" + end + + it "preserves encoding" do + CGI.escapeURIComponent("whatever".force_encoding("ASCII-8BIT")).encoding.should == Encoding::ASCII_8BIT + end + end +end