[前][次][番号順一覧][スレッド一覧]

ruby-changes:71806

From: Benoit <ko1@a...>
Date: Thu, 12 May 2022 18:19:30 +0900 (JST)
Subject: [ruby-changes:71806] 40ca208a6d (master): [ruby/uri] Improve URI.register_scheme tests and automatically upcase the given scheme

https://git.ruby-lang.org/ruby.git/commit/?id=40ca208a6d

From 40ca208a6db8d3a53cc016caab2aa8301bafdac2 Mon Sep 17 00:00:00 2001
From: Benoit Daloze <eregontp@g...>
Date: Wed, 28 Jul 2021 12:00:33 +0200
Subject: [ruby/uri] Improve URI.register_scheme tests and automatically upcase
 the given scheme

* Also add docs and mention current limitations.
* For reference, https://stackoverflow.com/a/3641782/388803 mentions the
  valid characters in schemes.

https://github.com/ruby/uri/commit/4346daac75
---
 lib/uri/common.rb       |  7 ++++++-
 test/uri/test_common.rb | 40 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index a6d08aa26f..ca38bec7ec 100644
--- a/lib/uri/common.rb
+++ b/lib/uri/common.rb
@@ -68,8 +68,13 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/common.rb#L68
   end
   private_constant :Schemes
 
+  #
+  # Register the given +klass+ to be instantiated when parsing URLs with the given +scheme+.
+  # Note that currently only schemes which after .upcase are valid constant names
+  # can be registered (no -/+/. allowed).
+  #
   def self.register_scheme(scheme, klass)
-    Schemes.const_set(scheme, klass)
+    Schemes.const_set(scheme.to_s.upcase, klass)
   end
 
   # Returns a Hash of the defined schemes.
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb
index 8cb23fe167..038f483a83 100644
--- a/test/uri/test_common.rb
+++ b/test/uri/test_common.rb
@@ -41,18 +41,52 @@ class TestCommon < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_common.rb#L41
     RUBY
   end
 
+  DEFAULT_SCHEMES = ["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort.freeze
+
   def test_register_scheme
-    assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
+    assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
 
     foobar = Class.new(URI::Generic)
     URI.register_scheme 'FOOBAR', foobar
     begin
-      assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
+      assert_include(URI.scheme_list.keys, "FOOBAR")
+      assert_equal foobar, URI.parse('foobar://localhost').class
     ensure
       URI.const_get(:Schemes).send(:remove_const, :FOOBAR)
     end
 
-    assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
+    assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
+  end
+
+  def test_register_scheme_lowercase
+    assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
+
+    foobar = Class.new(URI::Generic)
+    URI.register_scheme 'foobarlower', foobar
+    begin
+      assert_include(URI.scheme_list.keys, "FOOBARLOWER")
+      assert_equal foobar, URI.parse('foobarlower://localhost').class
+    ensure
+      URI.const_get(:Schemes).send(:remove_const, :FOOBARLOWER)
+    end
+
+    assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
+  end
+
+  def test_register_scheme_with_symbols
+    # Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
+    some_uri_class = Class.new(URI::Generic)
+    assert_raise(NameError) { URI.register_scheme 'ms-search', some_uri_class }
+    assert_raise(NameError) { URI.register_scheme 'microsoft.windows.camera', some_uri_class }
+    assert_raise(NameError) { URI.register_scheme 'coaps+ws', some_uri_class }
+
+    ms_search_class = Class.new(URI::Generic)
+    URI.register_scheme 'MS_SEARCH', ms_search_class
+    begin
+      assert_equal URI::Generic, URI.parse('ms-search://localhost').class
+    ensure
+      URI.const_get(:Schemes).send(:remove_const, :MS_SEARCH)
+    end
   end
 
   def test_regexp
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]