2009-07-30 01:34:02 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'cgi'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
|
|
|
|
class CGIUtilTest < Test::Unit::TestCase
|
2013-05-03 07:23:23 -04:00
|
|
|
include CGI::Util
|
2009-07-30 01:34:02 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
ENV['REQUEST_METHOD'] = 'GET'
|
|
|
|
@str1="&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93"
|
2009-12-22 19:14:48 -05:00
|
|
|
@str1.force_encoding("UTF-8") if defined?(::Encoding)
|
2009-07-30 01:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
%W[REQUEST_METHOD SCRIPT_NAME].each do |name|
|
|
|
|
ENV.delete(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_cgi_escape
|
|
|
|
assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93', CGI::escape(@str1))
|
2009-12-22 19:14:48 -05:00
|
|
|
assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'.ascii_only?, CGI::escape(@str1).ascii_only?) if defined?(::Encoding)
|
2009-07-30 01:34:02 -04:00
|
|
|
end
|
|
|
|
|
2012-01-20 06:48:52 -05:00
|
|
|
def test_cgi_escape_with_invalid_byte_sequence
|
|
|
|
assert_nothing_raised(ArgumentError) do
|
2014-03-21 17:46:04 -04:00
|
|
|
assert_equal('%C0%3C%3C', CGI::escape("\xC0\<\<".force_encoding("UTF-8")))
|
2012-01-20 06:48:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-22 20:09:06 -05:00
|
|
|
def test_cgi_escape_preserve_encoding
|
2014-03-21 17:46:04 -04:00
|
|
|
assert_equal(Encoding::US_ASCII, CGI::escape("\xC0\<\<".force_encoding("US-ASCII")).encoding)
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, CGI::escape("\xC0\<\<".force_encoding("ASCII-8BIT")).encoding)
|
|
|
|
assert_equal(Encoding::UTF_8, CGI::escape("\xC0\<\<".force_encoding("UTF-8")).encoding)
|
2012-01-22 20:09:06 -05:00
|
|
|
end
|
|
|
|
|
2009-07-30 01:34:02 -04:00
|
|
|
def test_cgi_unescape
|
|
|
|
assert_equal(@str1, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'))
|
2009-12-22 19:14:48 -05:00
|
|
|
assert_equal(@str1.encoding, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93').encoding) if defined?(::Encoding)
|
2010-10-13 09:39:13 -04:00
|
|
|
assert_equal("\u{30E1 30E2 30EA 691C 7D22}", CGI.unescape("\u{30E1 30E2 30EA}%E6%A4%9C%E7%B4%A2"))
|
2009-07-30 01:34:02 -04:00
|
|
|
end
|
|
|
|
|
2012-01-22 20:09:06 -05:00
|
|
|
def test_cgi_unescape_preserve_encoding
|
|
|
|
assert_equal(Encoding::US_ASCII, CGI::unescape("%C0%3C%3C".force_encoding("US-ASCII")).encoding)
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, CGI::unescape("%C0%3C%3C".force_encoding("ASCII-8BIT")).encoding)
|
|
|
|
assert_equal(Encoding::UTF_8, CGI::unescape("%C0%3C%3C".force_encoding("UTF-8")).encoding)
|
|
|
|
end
|
|
|
|
|
2009-07-30 03:39:09 -04:00
|
|
|
def test_cgi_pretty
|
|
|
|
assert_equal("<HTML>\n <BODY>\n </BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>"))
|
|
|
|
assert_equal("<HTML>\n\t<BODY>\n\t</BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>","\t"))
|
|
|
|
end
|
|
|
|
|
2012-07-17 19:04:46 -04:00
|
|
|
def test_cgi_escapeHTML
|
2012-08-21 18:30:47 -04:00
|
|
|
assert_equal(CGI::escapeHTML("'&\"><"),"'&"><")
|
2012-07-17 19:04:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cgi_unescapeHTML
|
2012-08-21 18:30:47 -04:00
|
|
|
assert_equal(CGI::unescapeHTML("'&"><"),"'&\"><")
|
2012-07-17 19:04:46 -04:00
|
|
|
end
|
|
|
|
|
2013-04-08 00:06:39 -04:00
|
|
|
def test_cgi_unescapeHTML_uppercasecharacter
|
|
|
|
assert_equal(CGI::unescapeHTML("あいう"),"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86")
|
|
|
|
end
|
|
|
|
|
2013-05-03 07:23:23 -04:00
|
|
|
def test_cgi_include_escape
|
|
|
|
assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93', escape(@str1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cgi_include_escapeHTML
|
|
|
|
assert_equal(escapeHTML("'&\"><"),"'&"><")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cgi_include_h
|
|
|
|
assert_equal(h("'&\"><"),"'&"><")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cgi_include_unescape
|
|
|
|
assert_equal(@str1, unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'))
|
|
|
|
assert_equal(@str1.encoding, unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93').encoding) if defined?(::Encoding)
|
|
|
|
assert_equal("\u{30E1 30E2 30EA 691C 7D22}", unescape("\u{30E1 30E2 30EA}%E6%A4%9C%E7%B4%A2"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cgi_include_unescapeHTML
|
|
|
|
assert_equal(unescapeHTML("'&"><"),"'&\"><")
|
|
|
|
end
|
2014-03-21 17:46:17 -04:00
|
|
|
|
|
|
|
def test_cgi_escapeElement
|
|
|
|
assert_equal("<BR><A HREF="url"></A>", escapeElement('<BR><A HREF="url"></A>', "A", "IMG"))
|
|
|
|
assert_equal("<BR><A HREF="url"></A>", escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"]))
|
|
|
|
assert_equal("<BR><A HREF="url"></A>", escape_element('<BR><A HREF="url"></A>', "A", "IMG"))
|
|
|
|
assert_equal("<BR><A HREF="url"></A>", escape_element('<BR><A HREF="url"></A>', ["A", "IMG"]))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_cgi_unescapeElement
|
|
|
|
assert_equal('<BR><A HREF="url"></A>', unescapeElement(escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG"))
|
|
|
|
assert_equal('<BR><A HREF="url"></A>', unescapeElement(escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"]))
|
|
|
|
assert_equal('<BR><A HREF="url"></A>', unescape_element(escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG"))
|
|
|
|
assert_equal('<BR><A HREF="url"></A>', unescape_element(escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"]))
|
|
|
|
end
|
2009-07-30 01:34:02 -04:00
|
|
|
end
|