Skip to content

Commit 1773bed

Browse files
committed
Move URI to rely less on shared examples
1 parent 997d09f commit 1773bed

6 files changed

Lines changed: 350 additions & 351 deletions

File tree

library/uri/parser/extract_spec.rb

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,90 @@
11
require_relative '../../../spec_helper'
2-
require_relative '../shared/extract'
32
require 'uri'
43

54
describe "URI::Parser#extract" do
6-
it_behaves_like :uri_extract, :extract, URI::Parser.new
5+
before :all do
6+
@parser = URI::Parser.new
7+
end
8+
9+
it "behaves according to its documentation" do
10+
@parser.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.").should == ["http://foo.example.org/bla", "mailto:test@example.com"]
11+
end
12+
13+
it "treats contiguous URIs as a single URI" do
14+
@parser.extract('http://example.jphttp://example.jp').should == ['http://example.jphttp://example.jp']
15+
end
16+
17+
it "treats pretty much anything with a colon as a URI" do
18+
@parser.extract('From: XXX [mailto:xxx@xxx.xxx.xxx]').should == ['From:', 'mailto:xxx@xxx.xxx.xxx]']
19+
end
20+
21+
it "wraps a URI string in an array" do
22+
@parser.extract("http://github.com/brixen/rubyspec/tree/master").should == ["http://github.com/brixen/rubyspec/tree/master"]
23+
end
24+
25+
it "pulls a variety of protocol URIs from a string" do
26+
@parser.extract("this is a string, it has http://rubini.us/ in it").should == ["http://rubini.us/"]
27+
@parser.extract("mailto:spambait@example.com").should == ["mailto:spambait@example.com"]
28+
@parser.extract("ftp://ruby-lang.org/").should == ["ftp://ruby-lang.org/"]
29+
@parser.extract("https://mail.google.com").should == ["https://mail.google.com"]
30+
@parser.extract("anything://example.com/").should == ["anything://example.com/"]
31+
end
32+
33+
it "pulls all URIs within a string in order into an array when a block is not given" do
34+
@parser.extract("1.3. Example URI
35+
36+
The following examples illustrate URI that are in common use.
37+
38+
ftp://ftp.is.co.za/rfc/rfc1808.txt
39+
-- ftp scheme for File Transfer Protocol services
40+
41+
gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles
42+
-- gopher scheme for Gopher and Gopher+ Protocol services
43+
44+
http://www.math.uio.no/faq/compression-faq/part1.html
45+
-- http scheme for Hypertext Transfer Protocol services
46+
47+
mailto:mduerst@ifi.unizh.ch
48+
-- mailto scheme for electronic mail addresses
49+
50+
news:comp.infosystems.www.servers.unix
51+
-- news scheme for USENET news groups and articles
52+
53+
telnet://melvyl.ucop.edu/
54+
-- telnet scheme for interactive services via the TELNET Protocol
55+
").should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch","news:comp.infosystems.www.servers.unix","telnet://melvyl.ucop.edu/"]
56+
end
57+
58+
it "yields each URI in the given string in order to a block, if given, and returns nil" do
59+
results = ["http://foo.example.org/bla", "mailto:test@example.com"]
60+
@parser.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.") {|uri|
61+
uri.should == results.shift
62+
}.should == nil
63+
results.should == []
64+
end
65+
66+
it "allows the user to specify a list of acceptable protocols of URIs to scan for" do
67+
@parser.extract("1.3. Example URI
68+
69+
The following examples illustrate URI that are in common use.
70+
71+
ftp://ftp.is.co.za/rfc/rfc1808.txt
72+
-- ftp scheme for File Transfer Protocol services
73+
74+
gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles
75+
-- gopher scheme for Gopher and Gopher+ Protocol services
76+
77+
http://www.math.uio.no/faq/compression-faq/part1.html
78+
-- http scheme for Hypertext Transfer Protocol services
79+
80+
mailto:mduerst@ifi.unizh.ch
81+
-- mailto scheme for electronic mail addresses
82+
83+
news:comp.infosystems.www.servers.unix
84+
-- news scheme for USENET news groups and articles
85+
86+
telnet://melvyl.ucop.edu/
87+
-- telnet scheme for interactive services via the TELNET Protocol
88+
", ["http","ftp","mailto"]).should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch"]
89+
end
790
end

library/uri/parser/join_spec.rb

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,62 @@
11
require_relative '../../../spec_helper'
2-
require_relative '../shared/join'
32
require 'uri'
43

54
describe "URI::Parser#join" do
6-
it_behaves_like :uri_join, :join, URI::Parser.new
5+
before :all do
6+
@parser = URI::Parser.new
7+
end
8+
9+
it "returns a URI object of the concatenation of a protocol and domain, and a path" do
10+
@parser.join("http://localhost/","main.rbx").should == URI.parse("http://localhost/main.rbx")
11+
end
12+
13+
it "accepts URI objects" do
14+
@parser.join(URI("http://localhost/"),"main.rbx").should == URI.parse("http://localhost/main.rbx")
15+
@parser.join("http://localhost/",URI("main.rbx")).should == URI.parse("http://localhost/main.rbx")
16+
@parser.join(URI("http://localhost/"),URI("main.rbx")).should == URI.parse("http://localhost/main.rbx")
17+
end
18+
19+
it "accepts string-like arguments with to_str" do
20+
str = mock('string-like')
21+
str.should_receive(:to_str).and_return("http://ruby-lang.org")
22+
str2 = mock('string-like also')
23+
str2.should_receive(:to_str).and_return("foo/bar")
24+
@parser.join(str, str2).should == URI.parse("http://ruby-lang.org/foo/bar")
25+
end
26+
27+
it "raises an error if given no argument" do
28+
-> {
29+
@parser.join
30+
}.should.raise(ArgumentError)
31+
end
32+
33+
it "doesn't create redundant '/'s" do
34+
@parser.join("http://localhost/", "/main.rbx").should == URI.parse("http://localhost/main.rbx")
35+
end
36+
37+
it "discards arguments given before an absolute uri" do
38+
@parser.join("http://localhost/a/b/c/d", "http://ruby-lang.com/foo", "bar").should == URI.parse("http://ruby-lang.com/bar")
39+
end
40+
41+
it "resolves .. in paths" do
42+
@parser.join("http://localhost/a/b/c/d", "../../e/f", "g/h/../i").to_s.should == "http://localhost/a/e/g/i"
43+
end
744
end
45+
46+
# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar'))
47+
# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar'))
48+
# assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/'))
49+
#
50+
# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz'))
51+
# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz'))
52+
# assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/'))
53+
# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz'))
54+
# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge'))
55+
#
56+
# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz'))
57+
# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
58+
# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
59+
# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))
60+
# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
61+
# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
62+
# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))

library/uri/parser/parse_spec.rb

Lines changed: 208 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,213 @@
11
require_relative '../../../spec_helper'
22
require_relative '../fixtures/classes'
3-
require_relative '../shared/parse'
43

54
describe "URI::Parser#parse" do
6-
it_behaves_like :uri_parse, :parse, URI::Parser.new
5+
before :all do
6+
@parser = URI::Parser.new
7+
end
8+
9+
it "returns a URI::HTTP object when parsing an HTTP URI" do
10+
@parser.parse("http://www.example.com/").should.is_a?(URI::HTTP)
11+
end
12+
13+
it "populates the components of a parsed URI::HTTP, setting the port to 80 by default" do
14+
# general case
15+
URISpec.components(@parser.parse("http://user:pass@example.com/path/?query=val&q2=val2#fragment")).should == {
16+
scheme: "http",
17+
userinfo: "user:pass",
18+
host: "example.com",
19+
port: 80,
20+
path: "/path/",
21+
query: "query=val&q2=val2",
22+
fragment: "fragment"
23+
}
24+
25+
# multiple paths
26+
URISpec.components(@parser.parse("http://a/b/c/d;p?q")).should == {
27+
scheme: "http",
28+
userinfo: nil,
29+
host: "a",
30+
port: 80,
31+
path: "/b/c/d;p",
32+
query: "q",
33+
fragment: nil
34+
}
35+
36+
# multi-level domain
37+
URISpec.components(@parser.parse('http://www.math.uio.no/faq/compression-faq/part1.html')).should == {
38+
scheme: "http",
39+
userinfo: nil,
40+
host: "www.math.uio.no",
41+
port: 80,
42+
path: "/faq/compression-faq/part1.html",
43+
query: nil,
44+
fragment: nil
45+
}
46+
end
47+
48+
it "parses out the port number of a URI, when given" do
49+
@parser.parse("http://example.com:8080/").port.should == 8080
50+
end
51+
52+
it "returns a URI::HTTPS object when parsing an HTTPS URI" do
53+
@parser.parse("https://important-intern-net.net").should.is_a?(URI::HTTPS)
54+
end
55+
56+
it "sets the port of a parsed https URI to 443 by default" do
57+
@parser.parse("https://example.com/").port.should == 443
58+
end
59+
60+
it "populates the components of a parsed URI::FTP object" do
61+
# generic, empty password.
62+
url = @parser.parse("ftp://anonymous@ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.bz2;type=i")
63+
url.should.is_a?(URI::FTP)
64+
URISpec.components(url).should == {
65+
scheme: "ftp",
66+
userinfo: "anonymous",
67+
host: "ruby-lang.org",
68+
port: 21,
69+
path: "pub/ruby/1.8/ruby-1.8.6.tar.bz2",
70+
typecode: "i"
71+
}
72+
73+
# multidomain, no user or password
74+
url = @parser.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt')
75+
url.should.is_a?(URI::FTP)
76+
URISpec.components(url).should == {
77+
scheme: "ftp",
78+
userinfo: nil,
79+
host: "ftp.is.co.za",
80+
port: 21,
81+
path: "rfc/rfc1808.txt",
82+
typecode: nil
83+
}
84+
85+
# empty user
86+
url = @parser.parse('ftp://:pass@localhost/')
87+
url.should.is_a?(URI::FTP)
88+
URISpec.components(url).should == {
89+
scheme: "ftp",
90+
userinfo: ":pass",
91+
host: "localhost",
92+
port: 21,
93+
path: "",
94+
typecode: nil
95+
}
96+
url.password.should == "pass"
97+
end
98+
99+
it "returns a URI::LDAP object when parsing an LDAP URI" do
100+
#taken from http://www.faqs.org/rfcs/rfc2255.html 'cause I don't really know what an LDAP url looks like
101+
ldap_uris = %w{ ldap:///o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen) ldap://ldap.itd.umich.edu/c=GB?objectClass?one ldap://ldap.question.com/o=Question%3f,c=US?mail ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04) ldap:///??sub??bindname=cn=Manager%2co=Foo ldap:///??sub??!bindname=cn=Manager%2co=Foo }
102+
ldap_uris.each do |ldap_uri|
103+
@parser.parse(ldap_uri).should.is_a?(URI::LDAP)
104+
end
105+
end
106+
107+
it "populates the components of a parsed URI::LDAP object" do
108+
URISpec.components(@parser.parse("ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress?scope?filter?extensions")).should == {
109+
scheme: "ldap",
110+
host: "ldap.itd.umich.edu",
111+
port: 389,
112+
dn: "o=University%20of%20Michigan,c=US",
113+
attributes: "postalAddress",
114+
scope: "scope",
115+
filter: "filter",
116+
extensions: "extensions"
117+
}
118+
end
119+
120+
it "returns a URI::MailTo object when passed a mailto URI" do
121+
@parser.parse("mailto:spam@mailinator.com").should.is_a?(URI::MailTo)
122+
end
123+
124+
it "populates the components of a parsed URI::MailTo object" do
125+
URISpec.components(@parser.parse("mailto:spam@mailinator.com?subject=Discounts%20On%20Imported%20methods!!!&body=Exciting%20offer")).should == {
126+
scheme: "mailto",
127+
to: "spam@mailinator.com",
128+
headers: [["subject","Discounts%20On%20Imported%20methods!!!"],
129+
["body", "Exciting%20offer"]]
130+
}
131+
end
132+
133+
# TODO
134+
# Test registry
135+
it "does its best to extract components from URI::Generic objects" do
136+
# generic
137+
URISpec.components(URI("scheme://userinfo@host/path?query#fragment")).should == {
138+
scheme: "scheme",
139+
userinfo: "userinfo",
140+
host: "host",
141+
port: nil,
142+
path: "/path",
143+
query: "query",
144+
fragment: "fragment",
145+
registry: nil,
146+
opaque: nil
147+
}
148+
149+
# gopher
150+
gopher = @parser.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles')
151+
gopher.should.is_a?(URI::Generic)
152+
153+
URISpec.components(gopher).should == {
154+
scheme: "gopher",
155+
userinfo: nil,
156+
host: "spinaltap.micro.umn.edu",
157+
port: nil,
158+
path: "/00/Weather/California/Los%20Angeles",
159+
query: nil,
160+
fragment: nil,
161+
registry: nil,
162+
opaque: nil
163+
}
164+
165+
# news
166+
news = @parser.parse('news:comp.infosystems.www.servers.unix')
167+
news.should.is_a?(URI::Generic)
168+
URISpec.components(news).should == {
169+
scheme: "news",
170+
userinfo: nil,
171+
host: nil,
172+
port: nil,
173+
path: nil,
174+
query: nil,
175+
fragment: nil,
176+
registry: nil,
177+
opaque: "comp.infosystems.www.servers.unix"
178+
}
179+
180+
# telnet
181+
telnet = @parser.parse('telnet://melvyl.ucop.edu/')
182+
telnet.should.is_a?(URI::Generic)
183+
URISpec.components(telnet).should == {
184+
scheme: "telnet",
185+
userinfo: nil,
186+
host: "melvyl.ucop.edu",
187+
port: nil,
188+
path: "/",
189+
query: nil,
190+
fragment: nil,
191+
registry: nil,
192+
opaque: nil
193+
}
194+
195+
# files
196+
file_l = @parser.parse('file:///foo/bar.txt')
197+
file_l.should.is_a?(URI::Generic)
198+
file = @parser.parse('file:/foo/bar.txt')
199+
file.should.is_a?(URI::Generic)
200+
end
201+
202+
if URI::DEFAULT_PARSER == URI::RFC2396_Parser
203+
it "raises errors on malformed URIs" do
204+
-> { @parser.parse('http://a_b:80/') }.should.raise(URI::InvalidURIError)
205+
-> { @parser.parse('http://a_b/') }.should.raise(URI::InvalidURIError)
206+
end
207+
elsif URI::DEFAULT_PARSER == URI::RFC3986_Parser
208+
it "does not raise errors on URIs contained underscore" do
209+
-> { @parser.parse('http://a_b:80/') }.should_not.raise(URI::InvalidURIError)
210+
-> { @parser.parse('http://a_b/') }.should_not.raise(URI::InvalidURIError)
211+
end
212+
end
7213
end

0 commit comments

Comments
 (0)