|
1 | 1 | require_relative '../../../spec_helper' |
2 | 2 | require_relative '../fixtures/classes' |
3 | | -require_relative '../shared/parse' |
4 | 3 |
|
5 | 4 | 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 |
7 | 213 | end |
0 commit comments