Debian Codegraph @

ruby/tests/test_uri.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 require 'test_tools'
20 require 'qpid_proton'
21
22 class TestURI < MiniTest::Test
23
24 PARTS=[:scheme, :userinfo, :host, :port, :path] # Interesting URI components
25 def uri(u) Qpid::Proton::uri(u); end
26 def uri_parts(u) uri(u).select(*PARTS); end
27
28 # Extension to standard URI parser
29 def test_standard
30 u = URI("amqp://u:p@h/x")
31 assert_equal URI::AMQP, u.class
32 assert_equal ['amqp', 'u:p', 'h', 5672, '/x'], u.select(*PARTS)
33
34 u = URI("amqps://u:p@h/x")
35 assert_equal URI::AMQPS, u.class
36 assert_equal ['amqps', 'u:p', 'h', 5671, '/x'], u.select(*PARTS)
37
38 assert_equal ['amqp', nil, '[::1:2:3]', 5672, ""], URI('amqp://[::1:2:3]').select(*PARTS)
39 end
40
41 # Proton::uri on valid URIs
42 def test_valid
43 u = uri("amqp://u:p@h:1/x")
44 assert_equal URI::AMQP, u.class
45 assert_equal ['amqp', 'u:p', 'h', 1, '/x'], u.select(*PARTS)
46
47 u = uri("amqps://u:p@h:1/x")
48 assert_equal URI::AMQPS, u.class
49 assert_equal ['amqps', 'u:p', 'h', 1, '/x'], u.select(*PARTS)
50
51 # Schemeless string -> amqp
52 assert_equal ["amqp", nil, "h", 1, "/x"], uri_parts("//h:1/x")
53 assert_equal ["amqp", nil, "", 5672, "/x"], uri_parts("/x")
54 assert_equal ["amqp", nil, "[::1]", 5672, ""], uri_parts("//[::1]")
55
56 # Schemeless URI gets amqp: scheme
57 assert_equal ["amqp", nil, nil, 5672, "/x"], uri_parts(URI("/x"))
58
59 # Pass-through
60 u = uri('')
61 assert_same u, uri(u)
62 end
63
64 # Proton::uri non-standard shortcuts
65 def test_shortcut
66 assert_equal URI("amqp://u:p@h:1/x"), uri("u:p@h:1/x")
67 assert_equal URI("amqp://h:1"), uri("h:1")
68 assert_equal URI("amqp://h"), uri("h")
69 assert_equal URI("amqp://h"), uri("h:")
70 assert_equal ["amqp", nil, "", 5672, ""], uri_parts("")
71 assert_equal ["amqp", nil, "", 5672, ""], uri_parts(":")
72 assert_equal ["amqp", nil, "", 1, ""], uri_parts(":1")
73 assert_equal ["amqp", nil, "", 1, ""], uri_parts("amqp://:1")
74 assert_equal URI("amqp://[::1:2]:1"), uri("[::1:2]:1")
75 assert_equal URI("amqp://[::1:2]"), uri("[::1:2]")
76 end
77
78 def test_error
79 assert_raises(::ArgumentError) { uri(nil) }
80 assert_raises(URI::BadURIError) { uri(URI("http://x")) } # Don't re-parse a URI with wrong scheme
81 assert_raises(URI::InvalidURIError) { uri("x:y:z") } # Nonsense
82 assert_raises(URI::InvalidURIError) { uri("amqp://[foobar]") } # Bad host
83 end
84
85 end