Skip to content

Commit ff8a103

Browse files
committed
Feat: Support WSS
There was a file for WSS so I added one line of `require_relative` to make it work. Now `URI.parse('wss://example.com')` returns `URI::WS`.
1 parent 230fd18 commit ff8a103

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

lib/uri.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@ module URI
101101
require_relative 'uri/ldaps'
102102
require_relative 'uri/mailto'
103103
require_relative 'uri/ws'
104+
require_relative 'uri/wss'

test/uri/test_common.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ def test_ractor
4242
end
4343

4444
def test_register_scheme
45-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS"].sort, URI.scheme_list.keys.sort)
45+
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
4646

4747
foobar = Class.new(URI::Generic)
4848
URI.register_scheme 'FOOBAR', foobar
4949
begin
50-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
50+
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
5151
ensure
5252
URI.const_get(:Schemes).send(:remove_const, :FOOBAR)
5353
end
5454

55-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS"].sort, URI.scheme_list.keys.sort)
55+
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
5656
end
5757

5858
def test_regexp

test/uri/test_wss.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: false
2+
require 'test/unit'
3+
require 'uri/http'
4+
require 'uri/wss'
5+
6+
module URI
7+
8+
9+
class TestWSS < Test::Unit::TestCase
10+
def setup
11+
end
12+
13+
def teardown
14+
end
15+
16+
def uri_to_ary(uri)
17+
uri.class.component.collect {|c| uri.send(c)}
18+
end
19+
20+
def test_build
21+
u = URI::WSS.build(host: 'www.example.com', path: '/foo/bar')
22+
assert_kind_of(URI::WSS, u)
23+
end
24+
25+
def test_parse
26+
u = URI.parse('wss://a')
27+
assert_kind_of(URI::WSS, u)
28+
assert_equal(['wss',
29+
nil, 'a', URI::HTTPS.default_port,
30+
'', nil], uri_to_ary(u))
31+
end
32+
33+
def test_normalize
34+
host = 'aBcD'
35+
u1 = URI.parse('wss://' + host + '/eFg?HiJ')
36+
u2 = URI.parse('wss://' + host.downcase + '/eFg?HiJ')
37+
assert(u1.normalize.host == 'abcd')
38+
assert(u1.normalize.path == u1.path)
39+
assert(u1.normalize == u2.normalize)
40+
assert(!u1.normalize.host.equal?(u1.host))
41+
assert( u2.normalize.host.equal?(u2.host))
42+
43+
assert_equal('wss://abc/', URI.parse('wss://abc').normalize.to_s)
44+
end
45+
46+
def test_equal
47+
assert(URI.parse('wss://abc') == URI.parse('wss://ABC'))
48+
assert(URI.parse('wss://abc/def') == URI.parse('wss://ABC/def'))
49+
assert(URI.parse('wss://abc/def') != URI.parse('wss://ABC/DEF'))
50+
end
51+
52+
def test_request_uri
53+
assert_equal('/', URI.parse('wss://a.b.c/').request_uri)
54+
assert_equal('/?abc=def', URI.parse('wss://a.b.c/?abc=def').request_uri)
55+
assert_equal('/', URI.parse('wss://a.b.c').request_uri)
56+
assert_equal('/?abc=def', URI.parse('wss://a.b.c?abc=def').request_uri)
57+
assert_equal(nil, URI.parse('wss:foo').request_uri)
58+
end
59+
60+
def test_select
61+
assert_equal(['wss', 'a.b.c', 443], URI.parse('wss://a.b.c/').select(:scheme, :host, :port))
62+
u = URI.parse('wss://a.b.c/')
63+
assert_equal(uri_to_ary(u), u.select(*u.component))
64+
assert_raise(ArgumentError) do
65+
u.select(:scheme, :host, :not_exist, :port)
66+
end
67+
end
68+
end
69+
70+
71+
end

0 commit comments

Comments
 (0)