summaryrefslogtreecommitdiff
path: root/test/uri/test_common.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-03-04 14:05:18 -0800
committergit <svn-admin@ruby-lang.org>2022-05-12 14:54:37 +0900
commitfbebfe1697938a684f460cd28af36cf1f056513c (patch)
tree22771001917053627d5562d7725ead5344220fb6 /test/uri/test_common.rb
parent054ae999dc5dfcb182f407bffceec5a52ae7ff6c (diff)
[ruby/uri] Add URI::Generic#decoded_#{user,password}
URI::Generic#{user,password} return the encoded values, which are not that useful if you want to do authentication with them. Automatic decoding by default would break backwards compatibility. Optional automatic decoding via a keyword to URI.parse would require threading the option through at least 3 other methods, and would make semantics confusing (user= takes encoded or unencoded password?) or require more work. Thus, adding this as a separate method seemed the simplest approach. Unfortunately, URI lacks a method for correct decoding. Unlike in www form components, + in earlier parts of the URI such as the userinfo section is treated verbatim and not as an encoded space. Add URI.#{en,de}code_uri_component methods, which are almost the same as URI.#{en,de}code_www_form_component, but without the special SP => + handling. Implements [Feature #9045] https://github.com/ruby/uri/commit/16cfc4e92f
Diffstat (limited to 'test/uri/test_common.rb')
-rw-r--r--test/uri/test_common.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb
index 0fa7e8ac70..8cb23fe167 100644
--- a/test/uri/test_common.rb
+++ b/test/uri/test_common.rb
@@ -130,6 +130,58 @@ class TestCommon < Test::Unit::TestCase
assert_nothing_raised(ArgumentError){URI.decode_www_form_component("x"*(1024*1024))}
end
+ def test_encode_uri_component
+ assert_equal("%00%20%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E",
+ URI.encode_uri_component("\x00 !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~"))
+ assert_equal("%95A", URI.encode_uri_component(
+ "\x95\x41".force_encoding(Encoding::Shift_JIS)))
+ assert_equal("0B", URI.encode_uri_component(
+ "\x30\x42".force_encoding(Encoding::UTF_16BE)))
+ assert_equal("%1B%24B%24%22%1B%28B", URI.encode_uri_component(
+ "\e$B$\"\e(B".force_encoding(Encoding::ISO_2022_JP)))
+
+ assert_equal("%E3%81%82", URI.encode_uri_component(
+ "\u3042", Encoding::ASCII_8BIT))
+ assert_equal("%82%A0", URI.encode_uri_component(
+ "\u3042", Encoding::Windows_31J))
+ assert_equal("%E3%81%82", URI.encode_uri_component(
+ "\u3042", Encoding::UTF_8))
+
+ assert_equal("%82%A0", URI.encode_uri_component(
+ "\u3042".encode("sjis"), Encoding::ASCII_8BIT))
+ assert_equal("%A4%A2", URI.encode_uri_component(
+ "\u3042".encode("sjis"), Encoding::EUC_JP))
+ assert_equal("%E3%81%82", URI.encode_uri_component(
+ "\u3042".encode("sjis"), Encoding::UTF_8))
+ assert_equal("B0", URI.encode_uri_component(
+ "\u3042".encode("sjis"), Encoding::UTF_16LE))
+ assert_equal("%26%23730%3B", URI.encode_uri_component(
+ "\u02DA", Encoding::WINDOWS_1252))
+
+ # invalid
+ assert_equal("%EF%BF%BD%EF%BF%BD", URI.encode_uri_component(
+ "\xE3\x81\xFF", "utf-8"))
+ assert_equal("%E6%9F%8A%EF%BF%BD%EF%BF%BD", URI.encode_uri_component(
+ "\x95\x41\xff\xff".force_encoding(Encoding::Shift_JIS), "utf-8"))
+ end
+
+ def test_decode_uri_component
+ assert_equal(" +!\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~",
+ URI.decode_uri_component(
+ "%20+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E"))
+ assert_equal("\xA1\xA2".force_encoding(Encoding::EUC_JP),
+ URI.decode_uri_component("%A1%A2", "EUC-JP"))
+ assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"),
+ URI.decode_uri_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8")))
+
+ assert_raise(ArgumentError){URI.decode_uri_component("%")}
+ assert_raise(ArgumentError){URI.decode_uri_component("%a")}
+ assert_raise(ArgumentError){URI.decode_uri_component("x%a_")}
+ assert_nothing_raised(ArgumentError){URI.decode_uri_component("x"*(1024*1024))}
+ end
+
def test_encode_www_form
assert_equal("a=1", URI.encode_www_form("a" => "1"))
assert_equal("a=1", URI.encode_www_form(a: 1))