PageRenderTime 73ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/spec/addressable/uri_spec.rb

https://github.com/bolshakov/addressable
Ruby | 5836 lines | 4843 code | 901 blank | 92 comment | 806 complexity | 5e424523a8dc28e4ad74f908ed39c151 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. # coding: utf-8
  2. # Copyright (C) 2006-2013 Bob Aman
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. require "spec_helper"
  16. require "addressable/uri"
  17. if !"".respond_to?("force_encoding")
  18. class String
  19. def force_encoding(encoding)
  20. @encoding = encoding
  21. end
  22. def encoding
  23. @encoding ||= Encoding::ASCII_8BIT
  24. end
  25. end
  26. class Encoding
  27. def initialize(name)
  28. @name = name
  29. end
  30. def to_s
  31. return @name
  32. end
  33. UTF_8 = Encoding.new("UTF-8")
  34. ASCII_8BIT = Encoding.new("US-ASCII")
  35. end
  36. end
  37. module Fake
  38. module URI
  39. class HTTP
  40. def initialize(uri)
  41. @uri = uri
  42. end
  43. def to_s
  44. return @uri.to_s
  45. end
  46. alias :to_str :to_s
  47. end
  48. end
  49. end
  50. describe Addressable::URI, "when created with a non-numeric port number" do
  51. it "should raise an error" do
  52. (lambda do
  53. Addressable::URI.new(:port => "bogus")
  54. end).should raise_error(Addressable::URI::InvalidURIError)
  55. end
  56. end
  57. describe Addressable::URI, "when created with a non-string scheme" do
  58. it "should raise an error" do
  59. (lambda do
  60. Addressable::URI.new(:scheme => :bogus)
  61. end).should raise_error(TypeError)
  62. end
  63. end
  64. describe Addressable::URI, "when created with a non-string user" do
  65. it "should raise an error" do
  66. (lambda do
  67. Addressable::URI.new(:user => :bogus)
  68. end).should raise_error(TypeError)
  69. end
  70. end
  71. describe Addressable::URI, "when created with a non-string password" do
  72. it "should raise an error" do
  73. (lambda do
  74. Addressable::URI.new(:password => :bogus)
  75. end).should raise_error(TypeError)
  76. end
  77. end
  78. describe Addressable::URI, "when created with a non-string userinfo" do
  79. it "should raise an error" do
  80. (lambda do
  81. Addressable::URI.new(:userinfo => :bogus)
  82. end).should raise_error(TypeError)
  83. end
  84. end
  85. describe Addressable::URI, "when created with a non-string host" do
  86. it "should raise an error" do
  87. (lambda do
  88. Addressable::URI.new(:host => :bogus)
  89. end).should raise_error(TypeError)
  90. end
  91. end
  92. describe Addressable::URI, "when created with a non-string authority" do
  93. it "should raise an error" do
  94. (lambda do
  95. Addressable::URI.new(:authority => :bogus)
  96. end).should raise_error(TypeError)
  97. end
  98. end
  99. describe Addressable::URI, "when created with a non-string authority" do
  100. it "should raise an error" do
  101. (lambda do
  102. Addressable::URI.new(:authority => :bogus)
  103. end).should raise_error(TypeError)
  104. end
  105. end
  106. describe Addressable::URI, "when created with a non-string path" do
  107. it "should raise an error" do
  108. (lambda do
  109. Addressable::URI.new(:path => :bogus)
  110. end).should raise_error(TypeError)
  111. end
  112. end
  113. describe Addressable::URI, "when created with a non-string query" do
  114. it "should raise an error" do
  115. (lambda do
  116. Addressable::URI.new(:query => :bogus)
  117. end).should raise_error(TypeError)
  118. end
  119. end
  120. describe Addressable::URI, "when created with a non-string fragment" do
  121. it "should raise an error" do
  122. (lambda do
  123. Addressable::URI.new(:fragment => :bogus)
  124. end).should raise_error(TypeError)
  125. end
  126. end
  127. describe Addressable::URI, "when created with a scheme but no hierarchical " +
  128. "segment" do
  129. it "should raise an error" do
  130. (lambda do
  131. Addressable::URI.parse("http:")
  132. end).should raise_error(Addressable::URI::InvalidURIError)
  133. end
  134. end
  135. describe Addressable::URI, "when created with an invalid host" do
  136. it "should raise an error" do
  137. (lambda do
  138. Addressable::URI.new(:host => "<invalid>")
  139. end).should raise_error(Addressable::URI::InvalidURIError)
  140. end
  141. end
  142. describe Addressable::URI, "when created from nil components" do
  143. before do
  144. @uri = Addressable::URI.new
  145. end
  146. it "should have a nil site value" do
  147. @uri.site.should == nil
  148. end
  149. it "should have an empty path" do
  150. @uri.path.should == ""
  151. end
  152. it "should be an empty uri" do
  153. @uri.to_s.should == ""
  154. end
  155. it "should have a nil default port" do
  156. @uri.default_port.should == nil
  157. end
  158. it "should be empty" do
  159. @uri.should be_empty
  160. end
  161. it "should raise an error if the scheme is set to whitespace" do
  162. (lambda do
  163. @uri.scheme = "\t \n"
  164. end).should raise_error(Addressable::URI::InvalidURIError)
  165. end
  166. it "should raise an error if the scheme is set to all digits" do
  167. (lambda do
  168. @uri.scheme = "123"
  169. end).should raise_error(Addressable::URI::InvalidURIError)
  170. end
  171. it "should raise an error if set into an invalid state" do
  172. (lambda do
  173. @uri.user = "user"
  174. end).should raise_error(Addressable::URI::InvalidURIError)
  175. end
  176. it "should raise an error if set into an invalid state" do
  177. (lambda do
  178. @uri.password = "pass"
  179. end).should raise_error(Addressable::URI::InvalidURIError)
  180. end
  181. it "should raise an error if set into an invalid state" do
  182. (lambda do
  183. @uri.scheme = "http"
  184. @uri.fragment = "fragment"
  185. end).should raise_error(Addressable::URI::InvalidURIError)
  186. end
  187. it "should raise an error if set into an invalid state" do
  188. (lambda do
  189. @uri.fragment = "fragment"
  190. @uri.scheme = "http"
  191. end).should raise_error(Addressable::URI::InvalidURIError)
  192. end
  193. end
  194. describe Addressable::URI, "when initialized from individual components" do
  195. before do
  196. @uri = Addressable::URI.new(
  197. :scheme => "http",
  198. :user => "user",
  199. :password => "password",
  200. :host => "example.com",
  201. :port => 8080,
  202. :path => "/path",
  203. :query => "query=value",
  204. :fragment => "fragment"
  205. )
  206. end
  207. it "returns 'http' for #scheme" do
  208. @uri.scheme.should == "http"
  209. end
  210. it "returns 'http' for #normalized_scheme" do
  211. @uri.normalized_scheme.should == "http"
  212. end
  213. it "returns 'user' for #user" do
  214. @uri.user.should == "user"
  215. end
  216. it "returns 'user' for #normalized_user" do
  217. @uri.normalized_user.should == "user"
  218. end
  219. it "returns 'password' for #password" do
  220. @uri.password.should == "password"
  221. end
  222. it "returns 'password' for #normalized_password" do
  223. @uri.normalized_password.should == "password"
  224. end
  225. it "returns 'user:password' for #userinfo" do
  226. @uri.userinfo.should == "user:password"
  227. end
  228. it "returns 'user:password' for #normalized_userinfo" do
  229. @uri.normalized_userinfo.should == "user:password"
  230. end
  231. it "returns 'example.com' for #host" do
  232. @uri.host.should == "example.com"
  233. end
  234. it "returns 'example.com' for #normalized_host" do
  235. @uri.normalized_host.should == "example.com"
  236. end
  237. it "returns 'user:password@example.com:8080' for #authority" do
  238. @uri.authority.should == "user:password@example.com:8080"
  239. end
  240. it "returns 'user:password@example.com:8080' for #normalized_authority" do
  241. @uri.normalized_authority.should == "user:password@example.com:8080"
  242. end
  243. it "returns 8080 for #port" do
  244. @uri.port.should == 8080
  245. end
  246. it "returns 8080 for #normalized_port" do
  247. @uri.normalized_port.should == 8080
  248. end
  249. it "returns 80 for #default_port" do
  250. @uri.default_port.should == 80
  251. end
  252. it "returns 'http://user:password@example.com:8080' for #site" do
  253. @uri.site.should == "http://user:password@example.com:8080"
  254. end
  255. it "returns 'http://user:password@example.com:8080' for #normalized_site" do
  256. @uri.normalized_site.should == "http://user:password@example.com:8080"
  257. end
  258. it "returns '/path' for #path" do
  259. @uri.path.should == "/path"
  260. end
  261. it "returns '/path' for #normalized_path" do
  262. @uri.normalized_path.should == "/path"
  263. end
  264. it "returns 'query=value' for #query" do
  265. @uri.query.should == "query=value"
  266. end
  267. it "returns 'query=value' for #normalized_query" do
  268. @uri.normalized_query.should == "query=value"
  269. end
  270. it "returns 'fragment' for #fragment" do
  271. @uri.fragment.should == "fragment"
  272. end
  273. it "returns 'fragment' for #normalized_fragment" do
  274. @uri.normalized_fragment.should == "fragment"
  275. end
  276. it "returns #hash" do
  277. @uri.hash.should_not be nil
  278. end
  279. it "returns #to_s" do
  280. @uri.to_s.should ==
  281. "http://user:password@example.com:8080/path?query=value#fragment"
  282. end
  283. it "should not be empty" do
  284. @uri.should_not be_empty
  285. end
  286. it "should not be frozen" do
  287. @uri.should_not be_frozen
  288. end
  289. it "should allow destructive operations" do
  290. expect { @uri.normalize! }.not_to raise_error
  291. end
  292. end
  293. describe Addressable::URI, "when initialized from " +
  294. "frozen individual components" do
  295. before do
  296. @uri = Addressable::URI.new(
  297. :scheme => "http".freeze,
  298. :user => "user".freeze,
  299. :password => "password".freeze,
  300. :host => "example.com".freeze,
  301. :port => "8080".freeze,
  302. :path => "/path".freeze,
  303. :query => "query=value".freeze,
  304. :fragment => "fragment".freeze
  305. )
  306. end
  307. it "returns 'http' for #scheme" do
  308. @uri.scheme.should == "http"
  309. end
  310. it "returns 'http' for #normalized_scheme" do
  311. @uri.normalized_scheme.should == "http"
  312. end
  313. it "returns 'user' for #user" do
  314. @uri.user.should == "user"
  315. end
  316. it "returns 'user' for #normalized_user" do
  317. @uri.normalized_user.should == "user"
  318. end
  319. it "returns 'password' for #password" do
  320. @uri.password.should == "password"
  321. end
  322. it "returns 'password' for #normalized_password" do
  323. @uri.normalized_password.should == "password"
  324. end
  325. it "returns 'user:password' for #userinfo" do
  326. @uri.userinfo.should == "user:password"
  327. end
  328. it "returns 'user:password' for #normalized_userinfo" do
  329. @uri.normalized_userinfo.should == "user:password"
  330. end
  331. it "returns 'example.com' for #host" do
  332. @uri.host.should == "example.com"
  333. end
  334. it "returns 'example.com' for #normalized_host" do
  335. @uri.normalized_host.should == "example.com"
  336. end
  337. it "returns 'user:password@example.com:8080' for #authority" do
  338. @uri.authority.should == "user:password@example.com:8080"
  339. end
  340. it "returns 'user:password@example.com:8080' for #normalized_authority" do
  341. @uri.normalized_authority.should == "user:password@example.com:8080"
  342. end
  343. it "returns 8080 for #port" do
  344. @uri.port.should == 8080
  345. end
  346. it "returns 8080 for #normalized_port" do
  347. @uri.normalized_port.should == 8080
  348. end
  349. it "returns 80 for #default_port" do
  350. @uri.default_port.should == 80
  351. end
  352. it "returns 'http://user:password@example.com:8080' for #site" do
  353. @uri.site.should == "http://user:password@example.com:8080"
  354. end
  355. it "returns 'http://user:password@example.com:8080' for #normalized_site" do
  356. @uri.normalized_site.should == "http://user:password@example.com:8080"
  357. end
  358. it "returns '/path' for #path" do
  359. @uri.path.should == "/path"
  360. end
  361. it "returns '/path' for #normalized_path" do
  362. @uri.normalized_path.should == "/path"
  363. end
  364. it "returns 'query=value' for #query" do
  365. @uri.query.should == "query=value"
  366. end
  367. it "returns 'query=value' for #normalized_query" do
  368. @uri.normalized_query.should == "query=value"
  369. end
  370. it "returns 'fragment' for #fragment" do
  371. @uri.fragment.should == "fragment"
  372. end
  373. it "returns 'fragment' for #normalized_fragment" do
  374. @uri.normalized_fragment.should == "fragment"
  375. end
  376. it "returns #hash" do
  377. @uri.hash.should_not be nil
  378. end
  379. it "returns #to_s" do
  380. @uri.to_s.should ==
  381. "http://user:password@example.com:8080/path?query=value#fragment"
  382. end
  383. it "should not be empty" do
  384. @uri.should_not be_empty
  385. end
  386. it "should not be frozen" do
  387. @uri.should_not be_frozen
  388. end
  389. it "should allow destructive operations" do
  390. expect { @uri.normalize! }.not_to raise_error
  391. end
  392. end
  393. describe Addressable::URI, "when parsed from a frozen string" do
  394. before do
  395. @uri = Addressable::URI.parse(
  396. "http://user:password@example.com:8080/path?query=value#fragment".freeze
  397. )
  398. end
  399. it "returns 'http' for #scheme" do
  400. @uri.scheme.should == "http"
  401. end
  402. it "returns 'http' for #normalized_scheme" do
  403. @uri.normalized_scheme.should == "http"
  404. end
  405. it "returns 'user' for #user" do
  406. @uri.user.should == "user"
  407. end
  408. it "returns 'user' for #normalized_user" do
  409. @uri.normalized_user.should == "user"
  410. end
  411. it "returns 'password' for #password" do
  412. @uri.password.should == "password"
  413. end
  414. it "returns 'password' for #normalized_password" do
  415. @uri.normalized_password.should == "password"
  416. end
  417. it "returns 'user:password' for #userinfo" do
  418. @uri.userinfo.should == "user:password"
  419. end
  420. it "returns 'user:password' for #normalized_userinfo" do
  421. @uri.normalized_userinfo.should == "user:password"
  422. end
  423. it "returns 'example.com' for #host" do
  424. @uri.host.should == "example.com"
  425. end
  426. it "returns 'example.com' for #normalized_host" do
  427. @uri.normalized_host.should == "example.com"
  428. end
  429. it "returns 'user:password@example.com:8080' for #authority" do
  430. @uri.authority.should == "user:password@example.com:8080"
  431. end
  432. it "returns 'user:password@example.com:8080' for #normalized_authority" do
  433. @uri.normalized_authority.should == "user:password@example.com:8080"
  434. end
  435. it "returns 8080 for #port" do
  436. @uri.port.should == 8080
  437. end
  438. it "returns 8080 for #normalized_port" do
  439. @uri.normalized_port.should == 8080
  440. end
  441. it "returns 80 for #default_port" do
  442. @uri.default_port.should == 80
  443. end
  444. it "returns 'http://user:password@example.com:8080' for #site" do
  445. @uri.site.should == "http://user:password@example.com:8080"
  446. end
  447. it "returns 'http://user:password@example.com:8080' for #normalized_site" do
  448. @uri.normalized_site.should == "http://user:password@example.com:8080"
  449. end
  450. it "returns '/path' for #path" do
  451. @uri.path.should == "/path"
  452. end
  453. it "returns '/path' for #normalized_path" do
  454. @uri.normalized_path.should == "/path"
  455. end
  456. it "returns 'query=value' for #query" do
  457. @uri.query.should == "query=value"
  458. end
  459. it "returns 'query=value' for #normalized_query" do
  460. @uri.normalized_query.should == "query=value"
  461. end
  462. it "returns 'fragment' for #fragment" do
  463. @uri.fragment.should == "fragment"
  464. end
  465. it "returns 'fragment' for #normalized_fragment" do
  466. @uri.normalized_fragment.should == "fragment"
  467. end
  468. it "returns #hash" do
  469. @uri.hash.should_not be nil
  470. end
  471. it "returns #to_s" do
  472. @uri.to_s.should ==
  473. "http://user:password@example.com:8080/path?query=value#fragment"
  474. end
  475. it "should not be empty" do
  476. @uri.should_not be_empty
  477. end
  478. it "should not be frozen" do
  479. @uri.should_not be_frozen
  480. end
  481. it "should allow destructive operations" do
  482. expect { @uri.normalize! }.not_to raise_error
  483. end
  484. end
  485. describe Addressable::URI, "when frozen" do
  486. before do
  487. @uri = Addressable::URI.new.freeze
  488. end
  489. it "returns nil for #scheme" do
  490. @uri.scheme.should == nil
  491. end
  492. it "returns nil for #normalized_scheme" do
  493. @uri.normalized_scheme.should == nil
  494. end
  495. it "returns nil for #user" do
  496. @uri.user.should == nil
  497. end
  498. it "returns nil for #normalized_user" do
  499. @uri.normalized_user.should == nil
  500. end
  501. it "returns nil for #password" do
  502. @uri.password.should == nil
  503. end
  504. it "returns nil for #normalized_password" do
  505. @uri.normalized_password.should == nil
  506. end
  507. it "returns nil for #userinfo" do
  508. @uri.userinfo.should == nil
  509. end
  510. it "returns nil for #normalized_userinfo" do
  511. @uri.normalized_userinfo.should == nil
  512. end
  513. it "returns nil for #host" do
  514. @uri.host.should == nil
  515. end
  516. it "returns nil for #normalized_host" do
  517. @uri.normalized_host.should == nil
  518. end
  519. it "returns nil for #authority" do
  520. @uri.authority.should == nil
  521. end
  522. it "returns nil for #normalized_authority" do
  523. @uri.normalized_authority.should == nil
  524. end
  525. it "returns nil for #port" do
  526. @uri.port.should == nil
  527. end
  528. it "returns nil for #normalized_port" do
  529. @uri.normalized_port.should == nil
  530. end
  531. it "returns nil for #default_port" do
  532. @uri.default_port.should == nil
  533. end
  534. it "returns nil for #site" do
  535. @uri.site.should == nil
  536. end
  537. it "returns nil for #normalized_site" do
  538. @uri.normalized_site.should == nil
  539. end
  540. it "returns '' for #path" do
  541. @uri.path.should == ''
  542. end
  543. it "returns '' for #normalized_path" do
  544. @uri.normalized_path.should == ''
  545. end
  546. it "returns nil for #query" do
  547. @uri.query.should == nil
  548. end
  549. it "returns nil for #normalized_query" do
  550. @uri.normalized_query.should == nil
  551. end
  552. it "returns nil for #fragment" do
  553. @uri.fragment.should == nil
  554. end
  555. it "returns nil for #normalized_fragment" do
  556. @uri.normalized_fragment.should == nil
  557. end
  558. it "returns #hash" do
  559. @uri.hash.should_not be nil
  560. end
  561. it "returns #to_s" do
  562. @uri.to_s.should == ''
  563. end
  564. it "should be empty" do
  565. @uri.should be_empty
  566. end
  567. it "should be frozen" do
  568. @uri.should be_frozen
  569. end
  570. it "should not be frozen after duping" do
  571. @uri.dup.should_not be_frozen
  572. end
  573. it "should not allow destructive operations" do
  574. expect { @uri.normalize! }.to raise_error { |error|
  575. error.message.should match(/can't modify frozen/)
  576. error.should satisfy { |e| RuntimeError === e || TypeError === e }
  577. }
  578. end
  579. end
  580. describe Addressable::URI, "when frozen" do
  581. before do
  582. @uri = Addressable::URI.parse(
  583. "HTTP://example.com.:%38%30/%70a%74%68?a=%31#1%323"
  584. ).freeze
  585. end
  586. it "returns 'HTTP' for #scheme" do
  587. @uri.scheme.should == "HTTP"
  588. end
  589. it "returns 'http' for #normalized_scheme" do
  590. @uri.normalized_scheme.should == "http"
  591. @uri.normalize.scheme.should == "http"
  592. end
  593. it "returns nil for #user" do
  594. @uri.user.should == nil
  595. end
  596. it "returns nil for #normalized_user" do
  597. @uri.normalized_user.should == nil
  598. end
  599. it "returns nil for #password" do
  600. @uri.password.should == nil
  601. end
  602. it "returns nil for #normalized_password" do
  603. @uri.normalized_password.should == nil
  604. end
  605. it "returns nil for #userinfo" do
  606. @uri.userinfo.should == nil
  607. end
  608. it "returns nil for #normalized_userinfo" do
  609. @uri.normalized_userinfo.should == nil
  610. end
  611. it "returns 'example.com.' for #host" do
  612. @uri.host.should == "example.com."
  613. end
  614. it "returns nil for #normalized_host" do
  615. @uri.normalized_host.should == "example.com"
  616. @uri.normalize.host.should == "example.com"
  617. end
  618. it "returns 'example.com.:80' for #authority" do
  619. @uri.authority.should == "example.com.:80"
  620. end
  621. it "returns 'example.com:80' for #normalized_authority" do
  622. @uri.normalized_authority.should == "example.com"
  623. @uri.normalize.authority.should == "example.com"
  624. end
  625. it "returns 80 for #port" do
  626. @uri.port.should == 80
  627. end
  628. it "returns nil for #normalized_port" do
  629. @uri.normalized_port.should == nil
  630. @uri.normalize.port.should == nil
  631. end
  632. it "returns 80 for #default_port" do
  633. @uri.default_port.should == 80
  634. end
  635. it "returns 'HTTP://example.com.:80' for #site" do
  636. @uri.site.should == "HTTP://example.com.:80"
  637. end
  638. it "returns 'http://example.com' for #normalized_site" do
  639. @uri.normalized_site.should == "http://example.com"
  640. @uri.normalize.site.should == "http://example.com"
  641. end
  642. it "returns '/%70a%74%68' for #path" do
  643. @uri.path.should == "/%70a%74%68"
  644. end
  645. it "returns '/path' for #normalized_path" do
  646. @uri.normalized_path.should == "/path"
  647. @uri.normalize.path.should == "/path"
  648. end
  649. it "returns 'a=%31' for #query" do
  650. @uri.query.should == "a=%31"
  651. end
  652. it "returns 'a=1' for #normalized_query" do
  653. @uri.normalized_query.should == "a=1"
  654. @uri.normalize.query.should == "a=1"
  655. end
  656. it "returns '1%323' for #fragment" do
  657. @uri.fragment.should == "1%323"
  658. end
  659. it "returns '123' for #normalized_fragment" do
  660. @uri.normalized_fragment.should == "123"
  661. @uri.normalize.fragment.should == "123"
  662. end
  663. it "returns #hash" do
  664. @uri.hash.should_not be nil
  665. end
  666. it "returns #to_s" do
  667. @uri.to_s.should == 'HTTP://example.com.:80/%70a%74%68?a=%31#1%323'
  668. @uri.normalize.to_s.should == 'http://example.com/path?a=1#123'
  669. end
  670. it "should not be empty" do
  671. @uri.should_not be_empty
  672. end
  673. it "should be frozen" do
  674. @uri.should be_frozen
  675. end
  676. it "should not be frozen after duping" do
  677. @uri.dup.should_not be_frozen
  678. end
  679. it "should not allow destructive operations" do
  680. expect { @uri.normalize! }.to raise_error { |error|
  681. error.message.should match(/can't modify frozen/)
  682. error.should satisfy { |e| RuntimeError === e || TypeError === e }
  683. }
  684. end
  685. end
  686. describe Addressable::URI, "when created from string components" do
  687. before do
  688. @uri = Addressable::URI.new(
  689. :scheme => "http", :host => "example.com"
  690. )
  691. end
  692. it "should have a site value of 'http://example.com'" do
  693. @uri.site.should == "http://example.com"
  694. end
  695. it "should be equal to the equivalent parsed URI" do
  696. @uri.should == Addressable::URI.parse("http://example.com")
  697. end
  698. it "should raise an error if invalid components omitted" do
  699. (lambda do
  700. @uri.omit(:bogus)
  701. end).should raise_error(ArgumentError)
  702. (lambda do
  703. @uri.omit(:scheme, :bogus, :path)
  704. end).should raise_error(ArgumentError)
  705. end
  706. end
  707. describe Addressable::URI, "when created with a nil host but " +
  708. "non-nil authority components" do
  709. it "should raise an error" do
  710. (lambda do
  711. Addressable::URI.new(:user => "user", :password => "pass", :port => 80)
  712. end).should raise_error(Addressable::URI::InvalidURIError)
  713. end
  714. end
  715. describe Addressable::URI, "when created with both an authority and a user" do
  716. it "should raise an error" do
  717. (lambda do
  718. Addressable::URI.new(
  719. :user => "user", :authority => "user@example.com:80"
  720. )
  721. end).should raise_error(ArgumentError)
  722. end
  723. end
  724. describe Addressable::URI, "when created with an authority and no port" do
  725. before do
  726. @uri = Addressable::URI.new(:authority => "user@example.com")
  727. end
  728. it "should not infer a port" do
  729. @uri.port.should == nil
  730. @uri.default_port.should == nil
  731. @uri.inferred_port.should == nil
  732. end
  733. it "should have a site value of '//user@example.com'" do
  734. @uri.site.should == "//user@example.com"
  735. end
  736. it "should have a 'null' origin" do
  737. @uri.origin.should == 'null'
  738. end
  739. end
  740. describe Addressable::URI, "when created with both a userinfo and a user" do
  741. it "should raise an error" do
  742. (lambda do
  743. Addressable::URI.new(:user => "user", :userinfo => "user:pass")
  744. end).should raise_error(ArgumentError)
  745. end
  746. end
  747. describe Addressable::URI, "when created with a path that hasn't been " +
  748. "prefixed with a '/' but a host specified" do
  749. before do
  750. @uri = Addressable::URI.new(
  751. :scheme => "http", :host => "example.com", :path => "path"
  752. )
  753. end
  754. it "should prefix a '/' to the path" do
  755. @uri.should == Addressable::URI.parse("http://example.com/path")
  756. end
  757. it "should have a site value of 'http://example.com'" do
  758. @uri.site.should == "http://example.com"
  759. end
  760. it "should have an origin of 'http://example.com" do
  761. @uri.origin.should == 'http://example.com'
  762. end
  763. end
  764. describe Addressable::URI, "when created with a path that hasn't been " +
  765. "prefixed with a '/' but no host specified" do
  766. before do
  767. @uri = Addressable::URI.new(
  768. :scheme => "http", :path => "path"
  769. )
  770. end
  771. it "should not prefix a '/' to the path" do
  772. @uri.should == Addressable::URI.parse("http:path")
  773. end
  774. it "should have a site value of 'http:'" do
  775. @uri.site.should == "http:"
  776. end
  777. it "should have a 'null' origin" do
  778. @uri.origin.should == 'null'
  779. end
  780. end
  781. describe Addressable::URI, "when parsed from an Addressable::URI object" do
  782. it "should not have unexpected side-effects" do
  783. original_uri = Addressable::URI.parse("http://example.com/")
  784. new_uri = Addressable::URI.parse(original_uri)
  785. new_uri.host = 'www.example.com'
  786. new_uri.host.should == 'www.example.com'
  787. new_uri.to_s.should == 'http://www.example.com/'
  788. original_uri.host.should == 'example.com'
  789. original_uri.to_s.should == 'http://example.com/'
  790. end
  791. it "should not have unexpected side-effects" do
  792. original_uri = Addressable::URI.parse("http://example.com/")
  793. new_uri = Addressable::URI.heuristic_parse(original_uri)
  794. new_uri.host = 'www.example.com'
  795. new_uri.host.should == 'www.example.com'
  796. new_uri.to_s.should == 'http://www.example.com/'
  797. original_uri.host.should == 'example.com'
  798. original_uri.to_s.should == 'http://example.com/'
  799. end
  800. end
  801. describe Addressable::URI, "when parsed from something that looks " +
  802. "like a URI object" do
  803. it "should parse without error" do
  804. uri = Addressable::URI.parse(Fake::URI::HTTP.new("http://example.com/"))
  805. (lambda do
  806. Addressable::URI.parse(uri)
  807. end).should_not raise_error
  808. end
  809. end
  810. describe Addressable::URI, "when parsed from ''" do
  811. before do
  812. @uri = Addressable::URI.parse("")
  813. end
  814. it "should have no scheme" do
  815. @uri.scheme.should == nil
  816. end
  817. it "should not be considered to be ip-based" do
  818. @uri.should_not be_ip_based
  819. end
  820. it "should have a path of ''" do
  821. @uri.path.should == ""
  822. end
  823. it "should have a request URI of '/'" do
  824. @uri.request_uri.should == "/"
  825. end
  826. it "should be considered relative" do
  827. @uri.should be_relative
  828. end
  829. it "should be considered to be in normal form" do
  830. @uri.normalize.should be_eql(@uri)
  831. end
  832. it "should have a 'null' origin" do
  833. @uri.origin.should == 'null'
  834. end
  835. end
  836. # Section 1.1.2 of RFC 3986
  837. describe Addressable::URI, "when parsed from " +
  838. "'ftp://ftp.is.co.za/rfc/rfc1808.txt'" do
  839. before do
  840. @uri = Addressable::URI.parse("ftp://ftp.is.co.za/rfc/rfc1808.txt")
  841. end
  842. it "should use the 'ftp' scheme" do
  843. @uri.scheme.should == "ftp"
  844. end
  845. it "should be considered to be ip-based" do
  846. @uri.should be_ip_based
  847. end
  848. it "should have a host of 'ftp.is.co.za'" do
  849. @uri.host.should == "ftp.is.co.za"
  850. end
  851. it "should have inferred_port of 21" do
  852. @uri.inferred_port.should == 21
  853. end
  854. it "should have a path of '/rfc/rfc1808.txt'" do
  855. @uri.path.should == "/rfc/rfc1808.txt"
  856. end
  857. it "should not have a request URI" do
  858. @uri.request_uri.should == nil
  859. end
  860. it "should be considered to be in normal form" do
  861. @uri.normalize.should be_eql(@uri)
  862. end
  863. it "should have an origin of 'ftp://ftp.is.co.za'" do
  864. @uri.origin.should == 'ftp://ftp.is.co.za'
  865. end
  866. end
  867. # Section 1.1.2 of RFC 3986
  868. describe Addressable::URI, "when parsed from " +
  869. "'http://www.ietf.org/rfc/rfc2396.txt'" do
  870. before do
  871. @uri = Addressable::URI.parse("http://www.ietf.org/rfc/rfc2396.txt")
  872. end
  873. it "should use the 'http' scheme" do
  874. @uri.scheme.should == "http"
  875. end
  876. it "should be considered to be ip-based" do
  877. @uri.should be_ip_based
  878. end
  879. it "should have a host of 'www.ietf.org'" do
  880. @uri.host.should == "www.ietf.org"
  881. end
  882. it "should have inferred_port of 80" do
  883. @uri.inferred_port.should == 80
  884. end
  885. it "should have a path of '/rfc/rfc2396.txt'" do
  886. @uri.path.should == "/rfc/rfc2396.txt"
  887. end
  888. it "should have a request URI of '/rfc/rfc2396.txt'" do
  889. @uri.request_uri.should == "/rfc/rfc2396.txt"
  890. end
  891. it "should be considered to be in normal form" do
  892. @uri.normalize.should be_eql(@uri)
  893. end
  894. it "should correctly omit components" do
  895. @uri.omit(:scheme).to_s.should == "//www.ietf.org/rfc/rfc2396.txt"
  896. @uri.omit(:path).to_s.should == "http://www.ietf.org"
  897. end
  898. it "should correctly omit components destructively" do
  899. @uri.omit!(:scheme)
  900. @uri.to_s.should == "//www.ietf.org/rfc/rfc2396.txt"
  901. end
  902. it "should have an origin of 'http://www.ietf.org'" do
  903. @uri.origin.should == 'http://www.ietf.org'
  904. end
  905. end
  906. # Section 1.1.2 of RFC 3986
  907. describe Addressable::URI, "when parsed from " +
  908. "'ldap://[2001:db8::7]/c=GB?objectClass?one'" do
  909. before do
  910. @uri = Addressable::URI.parse("ldap://[2001:db8::7]/c=GB?objectClass?one")
  911. end
  912. it "should use the 'ldap' scheme" do
  913. @uri.scheme.should == "ldap"
  914. end
  915. it "should be considered to be ip-based" do
  916. @uri.should be_ip_based
  917. end
  918. it "should have a host of '[2001:db8::7]'" do
  919. @uri.host.should == "[2001:db8::7]"
  920. end
  921. it "should have inferred_port of 389" do
  922. @uri.inferred_port.should == 389
  923. end
  924. it "should have a path of '/c=GB'" do
  925. @uri.path.should == "/c=GB"
  926. end
  927. it "should not have a request URI" do
  928. @uri.request_uri.should == nil
  929. end
  930. it "should not allow request URI assignment" do
  931. (lambda do
  932. @uri.request_uri = "/"
  933. end).should raise_error(Addressable::URI::InvalidURIError)
  934. end
  935. it "should have a query of 'objectClass?one'" do
  936. @uri.query.should == "objectClass?one"
  937. end
  938. it "should be considered to be in normal form" do
  939. @uri.normalize.should be_eql(@uri)
  940. end
  941. it "should correctly omit components" do
  942. @uri.omit(:scheme, :authority).to_s.should == "/c=GB?objectClass?one"
  943. @uri.omit(:path).to_s.should == "ldap://[2001:db8::7]?objectClass?one"
  944. end
  945. it "should correctly omit components destructively" do
  946. @uri.omit!(:scheme, :authority)
  947. @uri.to_s.should == "/c=GB?objectClass?one"
  948. end
  949. it "should raise an error if omission would create an invalid URI" do
  950. (lambda do
  951. @uri.omit(:authority, :path)
  952. end).should raise_error(Addressable::URI::InvalidURIError)
  953. end
  954. it "should have an origin of 'ldap://[2001:db8::7]'" do
  955. @uri.origin.should == 'ldap://[2001:db8::7]'
  956. end
  957. end
  958. # Section 1.1.2 of RFC 3986
  959. describe Addressable::URI, "when parsed from " +
  960. "'mailto:John.Doe@example.com'" do
  961. before do
  962. @uri = Addressable::URI.parse("mailto:John.Doe@example.com")
  963. end
  964. it "should use the 'mailto' scheme" do
  965. @uri.scheme.should == "mailto"
  966. end
  967. it "should not be considered to be ip-based" do
  968. @uri.should_not be_ip_based
  969. end
  970. it "should not have an inferred_port" do
  971. @uri.inferred_port.should == nil
  972. end
  973. it "should have a path of 'John.Doe@example.com'" do
  974. @uri.path.should == "John.Doe@example.com"
  975. end
  976. it "should not have a request URI" do
  977. @uri.request_uri.should == nil
  978. end
  979. it "should be considered to be in normal form" do
  980. @uri.normalize.should be_eql(@uri)
  981. end
  982. it "should have a 'null' origin" do
  983. @uri.origin.should == 'null'
  984. end
  985. end
  986. # Section 2 of RFC 6068
  987. describe Addressable::URI, "when parsed from " +
  988. "'mailto:?to=addr1@an.example,addr2@an.example'" do
  989. before do
  990. @uri = Addressable::URI.parse(
  991. "mailto:?to=addr1@an.example,addr2@an.example"
  992. )
  993. end
  994. it "should use the 'mailto' scheme" do
  995. @uri.scheme.should == "mailto"
  996. end
  997. it "should not be considered to be ip-based" do
  998. @uri.should_not be_ip_based
  999. end
  1000. it "should not have an inferred_port" do
  1001. @uri.inferred_port.should == nil
  1002. end
  1003. it "should have a path of ''" do
  1004. @uri.path.should == ""
  1005. end
  1006. it "should not have a request URI" do
  1007. @uri.request_uri.should == nil
  1008. end
  1009. it "should have the To: field value parameterized" do
  1010. @uri.query_values(Hash)["to"].should == (
  1011. "addr1@an.example,addr2@an.example"
  1012. )
  1013. end
  1014. it "should be considered to be in normal form" do
  1015. @uri.normalize.should be_eql(@uri)
  1016. end
  1017. it "should have a 'null' origin" do
  1018. @uri.origin.should == 'null'
  1019. end
  1020. end
  1021. # Section 1.1.2 of RFC 3986
  1022. describe Addressable::URI, "when parsed from " +
  1023. "'news:comp.infosystems.www.servers.unix'" do
  1024. before do
  1025. @uri = Addressable::URI.parse("news:comp.infosystems.www.servers.unix")
  1026. end
  1027. it "should use the 'news' scheme" do
  1028. @uri.scheme.should == "news"
  1029. end
  1030. it "should not have an inferred_port" do
  1031. @uri.inferred_port.should == nil
  1032. end
  1033. it "should not be considered to be ip-based" do
  1034. @uri.should_not be_ip_based
  1035. end
  1036. it "should have a path of 'comp.infosystems.www.servers.unix'" do
  1037. @uri.path.should == "comp.infosystems.www.servers.unix"
  1038. end
  1039. it "should not have a request URI" do
  1040. @uri.request_uri.should == nil
  1041. end
  1042. it "should be considered to be in normal form" do
  1043. @uri.normalize.should be_eql(@uri)
  1044. end
  1045. it "should have a 'null' origin" do
  1046. @uri.origin.should == 'null'
  1047. end
  1048. end
  1049. # Section 1.1.2 of RFC 3986
  1050. describe Addressable::URI, "when parsed from " +
  1051. "'tel:+1-816-555-1212'" do
  1052. before do
  1053. @uri = Addressable::URI.parse("tel:+1-816-555-1212")
  1054. end
  1055. it "should use the 'tel' scheme" do
  1056. @uri.scheme.should == "tel"
  1057. end
  1058. it "should not be considered to be ip-based" do
  1059. @uri.should_not be_ip_based
  1060. end
  1061. it "should not have an inferred_port" do
  1062. @uri.inferred_port.should == nil
  1063. end
  1064. it "should have a path of '+1-816-555-1212'" do
  1065. @uri.path.should == "+1-816-555-1212"
  1066. end
  1067. it "should not have a request URI" do
  1068. @uri.request_uri.should == nil
  1069. end
  1070. it "should be considered to be in normal form" do
  1071. @uri.normalize.should be_eql(@uri)
  1072. end
  1073. it "should have a 'null' origin" do
  1074. @uri.origin.should == 'null'
  1075. end
  1076. end
  1077. # Section 1.1.2 of RFC 3986
  1078. describe Addressable::URI, "when parsed from " +
  1079. "'telnet://192.0.2.16:80/'" do
  1080. before do
  1081. @uri = Addressable::URI.parse("telnet://192.0.2.16:80/")
  1082. end
  1083. it "should use the 'telnet' scheme" do
  1084. @uri.scheme.should == "telnet"
  1085. end
  1086. it "should have a host of '192.0.2.16'" do
  1087. @uri.host.should == "192.0.2.16"
  1088. end
  1089. it "should have a port of 80" do
  1090. @uri.port.should == 80
  1091. end
  1092. it "should have a inferred_port of 80" do
  1093. @uri.inferred_port.should == 80
  1094. end
  1095. it "should have a default_port of 23" do
  1096. @uri.default_port.should == 23
  1097. end
  1098. it "should be considered to be ip-based" do
  1099. @uri.should be_ip_based
  1100. end
  1101. it "should have a path of '/'" do
  1102. @uri.path.should == "/"
  1103. end
  1104. it "should not have a request URI" do
  1105. @uri.request_uri.should == nil
  1106. end
  1107. it "should be considered to be in normal form" do
  1108. @uri.normalize.should be_eql(@uri)
  1109. end
  1110. it "should have an origin of 'telnet://192.0.2.16:80'" do
  1111. @uri.origin.should == 'telnet://192.0.2.16:80'
  1112. end
  1113. end
  1114. # Section 1.1.2 of RFC 3986
  1115. describe Addressable::URI, "when parsed from " +
  1116. "'urn:oasis:names:specification:docbook:dtd:xml:4.1.2'" do
  1117. before do
  1118. @uri = Addressable::URI.parse(
  1119. "urn:oasis:names:specification:docbook:dtd:xml:4.1.2")
  1120. end
  1121. it "should use the 'urn' scheme" do
  1122. @uri.scheme.should == "urn"
  1123. end
  1124. it "should not have an inferred_port" do
  1125. @uri.inferred_port.should == nil
  1126. end
  1127. it "should not be considered to be ip-based" do
  1128. @uri.should_not be_ip_based
  1129. end
  1130. it "should have a path of " +
  1131. "'oasis:names:specification:docbook:dtd:xml:4.1.2'" do
  1132. @uri.path.should == "oasis:names:specification:docbook:dtd:xml:4.1.2"
  1133. end
  1134. it "should not have a request URI" do
  1135. @uri.request_uri.should == nil
  1136. end
  1137. it "should be considered to be in normal form" do
  1138. @uri.normalize.should be_eql(@uri)
  1139. end
  1140. it "should have a 'null' origin" do
  1141. @uri.origin.should == 'null'
  1142. end
  1143. end
  1144. describe Addressable::URI, "when heuristically parsed from " +
  1145. "'192.0.2.16:8000/path'" do
  1146. before do
  1147. @uri = Addressable::URI.heuristic_parse("192.0.2.16:8000/path")
  1148. end
  1149. it "should use the 'http' scheme" do
  1150. @uri.scheme.should == "http"
  1151. end
  1152. it "should have a host of '192.0.2.16'" do
  1153. @uri.host.should == "192.0.2.16"
  1154. end
  1155. it "should have a port of '8000'" do
  1156. @uri.port.should == 8000
  1157. end
  1158. it "should be considered to be ip-based" do
  1159. @uri.should be_ip_based
  1160. end
  1161. it "should have a path of '/path'" do
  1162. @uri.path.should == "/path"
  1163. end
  1164. it "should be considered to be in normal form" do
  1165. @uri.normalize.should be_eql(@uri)
  1166. end
  1167. it "should have an origin of 'http://192.0.2.16:8000'" do
  1168. @uri.origin.should == 'http://192.0.2.16:8000'
  1169. end
  1170. end
  1171. describe Addressable::URI, "when parsed from " +
  1172. "'http://example.com'" do
  1173. before do
  1174. @uri = Addressable::URI.parse("http://example.com")
  1175. end
  1176. it "when inspected, should have the correct URI" do
  1177. @uri.inspect.should include("http://example.com")
  1178. end
  1179. it "when inspected, should have the correct class name" do
  1180. @uri.inspect.should include("Addressable::URI")
  1181. end
  1182. it "when inspected, should have the correct object id" do
  1183. @uri.inspect.should include("%#0x" % @uri.object_id)
  1184. end
  1185. it "should use the 'http' scheme" do
  1186. @uri.scheme.should == "http"
  1187. end
  1188. it "should be considered to be ip-based" do
  1189. @uri.should be_ip_based
  1190. end
  1191. it "should have an authority segment of 'example.com'" do
  1192. @uri.authority.should == "example.com"
  1193. end
  1194. it "should have a host of 'example.com'" do
  1195. @uri.host.should == "example.com"
  1196. end
  1197. it "should be considered ip-based" do
  1198. @uri.should be_ip_based
  1199. end
  1200. it "should have no username" do
  1201. @uri.user.should == nil
  1202. end
  1203. it "should have no password" do
  1204. @uri.password.should == nil
  1205. end
  1206. it "should use port 80" do
  1207. @uri.inferred_port.should == 80
  1208. end
  1209. it "should not have a specified port" do
  1210. @uri.port.should == nil
  1211. end
  1212. it "should have an empty path" do
  1213. @uri.path.should == ""
  1214. end
  1215. it "should have no query string" do
  1216. @uri.query.should == nil
  1217. @uri.query_values.should == nil
  1218. end
  1219. it "should have a request URI of '/'" do
  1220. @uri.request_uri.should == "/"
  1221. end
  1222. it "should have no fragment" do
  1223. @uri.fragment.should == nil
  1224. end
  1225. it "should be considered absolute" do
  1226. @uri.should be_absolute
  1227. end
  1228. it "should not be considered relative" do
  1229. @uri.should_not be_relative
  1230. end
  1231. it "should not be exactly equal to 42" do
  1232. @uri.eql?(42).should == false
  1233. end
  1234. it "should not be equal to 42" do
  1235. (@uri == 42).should == false
  1236. end
  1237. it "should not be roughly equal to 42" do
  1238. (@uri === 42).should == false
  1239. end
  1240. it "should be exactly equal to http://example.com" do
  1241. @uri.eql?(Addressable::URI.parse("http://example.com")).should == true
  1242. end
  1243. it "should be roughly equal to http://example.com/" do
  1244. (@uri === Addressable::URI.parse("http://example.com/")).should == true
  1245. end
  1246. it "should be roughly equal to the string 'http://example.com/'" do
  1247. (@uri === "http://example.com/").should == true
  1248. end
  1249. it "should not be roughly equal to the string " +
  1250. "'http://example.com:bogus/'" do
  1251. (lambda do
  1252. (@uri === "http://example.com:bogus/").should == false
  1253. end).should_not raise_error
  1254. end
  1255. it "should result in itself when joined with itself" do
  1256. @uri.join(@uri).to_s.should == "http://example.com"
  1257. @uri.join!(@uri).to_s.should == "http://example.com"
  1258. end
  1259. it "should be equivalent to http://EXAMPLE.com" do
  1260. @uri.should == Addressable::URI.parse("http://EXAMPLE.com")
  1261. end
  1262. it "should be equivalent to http://EXAMPLE.com:80/" do
  1263. @uri.should == Addressable::URI.parse("http://EXAMPLE.com:80/")
  1264. end
  1265. it "should have the same hash as http://example.com" do
  1266. @uri.hash.should == Addressable::URI.parse("http://example.com").hash
  1267. end
  1268. it "should have the same hash as http://EXAMPLE.com after assignment" do
  1269. @uri.host = "EXAMPLE.com"
  1270. @uri.hash.should == Addressable::URI.parse("http://EXAMPLE.com").hash
  1271. end
  1272. it "should have a different hash from http://EXAMPLE.com" do
  1273. @uri.hash.should_not == Addressable::URI.parse("http://EXAMPLE.com").hash
  1274. end
  1275. # Section 6.2.3 of RFC 3986
  1276. it "should be equivalent to http://example.com/" do
  1277. @uri.should == Addressable::URI.parse("http://example.com/")
  1278. end
  1279. # Section 6.2.3 of RFC 3986
  1280. it "should be equivalent to http://example.com:/" do
  1281. @uri.should == Addressable::URI.parse("http://example.com:/")
  1282. end
  1283. # Section 6.2.3 of RFC 3986
  1284. it "should be equivalent to http://example.com:80/" do
  1285. @uri.should == Addressable::URI.parse("http://example.com:80/")
  1286. end
  1287. # Section 6.2.2.1 of RFC 3986
  1288. it "should be equivalent to http://EXAMPLE.COM/" do
  1289. @uri.should == Addressable::URI.parse("http://EXAMPLE.COM/")
  1290. end
  1291. it "should have a route of '/path/' to 'http://example.com/path/'" do
  1292. @uri.route_to("http://example.com/path/").should ==
  1293. Addressable::URI.parse("/path/")
  1294. end
  1295. it "should have a route of '..' from 'http://example.com/path/'" do
  1296. @uri.route_from("http://example.com/path/").should ==
  1297. Addressable::URI.parse("..")
  1298. end
  1299. it "should have a route of '#' to 'http://example.com/'" do
  1300. @uri.route_to("http://example.com/").should ==
  1301. Addressable::URI.parse("#")
  1302. end
  1303. it "should have a route of 'http://elsewhere.com/' to " +
  1304. "'http://elsewhere.com/'" do
  1305. @uri.route_to("http://elsewhere.com/").should ==
  1306. Addressable::URI.parse("http://elsewhere.com/")
  1307. end
  1308. it "when joined with 'relative/path' should be " +
  1309. "'http://example.com/relative/path'" do
  1310. @uri.join('relative/path').should ==
  1311. Addressable::URI.parse("http://example.com/relative/path")
  1312. end
  1313. it "when joined with a bogus object a TypeError should be raised" do
  1314. (lambda do
  1315. @uri.join(42)
  1316. end).should raise_error(TypeError)
  1317. end
  1318. it "should have the correct username after assignment" do
  1319. @uri.user = "newuser"
  1320. @uri.user.should == "newuser"
  1321. @uri.password.should == nil
  1322. @uri.to_s.should == "http://newuser@example.com"
  1323. end
  1324. it "should have the correct username after assignment" do
  1325. @uri.user = "user@123!"
  1326. @uri.user.should == "user@123!"
  1327. @uri.normalized_user.should == "user%40123%21"
  1328. @uri.password.should == nil
  1329. @uri.normalize.to_s.should == "http://user%40123%21@example.com/"
  1330. end
  1331. it "should have the correct password after assignment" do
  1332. @uri.password = "newpass"
  1333. @uri.password.should == "newpass"
  1334. @uri.user.should == ""
  1335. @uri.to_s.should == "http://:newpass@example.com"
  1336. end
  1337. it "should have the correct password after assignment" do
  1338. @uri.password = "#secret@123!"
  1339. @uri.password.should == "#secret@123!"
  1340. @uri.normalized_password.should == "%23secret%40123%21"
  1341. @uri.user.should == ""
  1342. @uri.normalize.to_s.should == "http://:%23secret%40123%21@example.com/"
  1343. @uri.omit(:password).to_s.should == "http://example.com"
  1344. end
  1345. it "should have the correct user/pass after repeated assignment" do
  1346. @uri.user = nil
  1347. @uri.user.should == nil
  1348. @uri.password = "newpass"
  1349. @uri.password.should == "newpass"
  1350. # Username cannot be nil if the password is set
  1351. @uri.user.should == ""
  1352. @uri.to_s.should == "http://:newpass@example.com"
  1353. @uri.user = "newuser"
  1354. @uri.user.should == "newuser"
  1355. @uri.password = nil
  1356. @uri.password.should == nil
  1357. @uri.to_s.should == "http://newuser@example.com"
  1358. @uri.user = "newuser"
  1359. @uri.user.should == "newuser"
  1360. @uri.password = ""
  1361. @uri.password.should == ""
  1362. @uri.to_s.should == "http://newuser:@example.com"
  1363. @uri.password = "newpass"
  1364. @uri.password.should == "newpass"
  1365. @uri.user = nil
  1366. # Username cannot be nil if the password is set
  1367. @uri.user.should == ""
  1368. @uri.to_s.should == "http://:newpass@example.com"
  1369. end
  1370. it "should have the correct user/pass after userinfo assignment" do
  1371. @uri.user = "newuser"
  1372. @uri.user.should == "newuser"
  1373. @uri.password = "newpass"
  1374. @uri.password.should == "newpass"
  1375. @uri.userinfo = nil
  1376. @uri.userinfo.should == nil
  1377. @uri.user.should == nil
  1378. @uri.password.should == nil
  1379. end
  1380. it "should correctly convert to a hash" do
  1381. @uri.to_hash.should == {
  1382. :scheme => "http",
  1383. :user => nil,
  1384. :password => nil,
  1385. :host => "example.com",
  1386. :port => nil,
  1387. :path => "",
  1388. :query => nil,
  1389. :fragment => nil
  1390. }
  1391. end
  1392. it "should be identical to its duplicate" do
  1393. @uri.should == @uri.dup
  1394. end
  1395. it "should have an origin of 'http://example.com'" do
  1396. @uri.origin.should == 'http://example.com'
  1397. end
  1398. end
  1399. # Section 5.1.2 of RFC 2616
  1400. describe Addressable::URI, "when parsed from " +
  1401. "'http://www.w3.org/pub/WWW/TheProject.html'" do
  1402. before do
  1403. @uri = Addressable::URI.parse("http://www.w3.org/pub/WWW/TheProject.html")
  1404. end
  1405. it "should have the correct request URI" do
  1406. @uri.request_uri.should == "/pub/WWW/TheProject.html"
  1407. end
  1408. it "should have the correct request URI after assignment" do
  1409. @uri.request_uri = "/some/where/else.html?query?string"
  1410. @uri.request_uri.should == "/some/where/else.html?query?string"
  1411. @uri.path.should == "/some/where/else.html"
  1412. @uri.query.should == "query?string"
  1413. end
  1414. it "should have the correct request URI after assignment" do
  1415. @uri.request_uri = "?x=y"
  1416. @uri.request_uri.should == "/?x=y"
  1417. @uri.path.should == "/"
  1418. @uri.query.should == "x=y"
  1419. end
  1420. it "should raise an error if the site value is set to something bogus" do
  1421. (lambda do
  1422. @uri.site = 42
  1423. end).should raise_error(TypeError)
  1424. end
  1425. it "should raise an error if the request URI is set to something bogus" do
  1426. (lambda do
  1427. @uri.request_uri = 42
  1428. end).should raise_error(TypeError)
  1429. end
  1430. it "should correctly convert to a hash" do
  1431. @uri.to_hash.should == {
  1432. :scheme => "http",
  1433. :user => nil,
  1434. :password => nil,
  1435. :host => "www.w3.org",
  1436. :port => nil,
  1437. :path => "/pub/WWW/TheProject.html",
  1438. :query => nil,
  1439. :fragment => nil
  1440. }
  1441. end
  1442. it "should have an origin of 'http://www.w3.org'" do
  1443. @uri.origin.should == 'http://www.w3.org'
  1444. end
  1445. end
  1446. describe Addressable::URI, "when parsing IPv6 addresses" do
  1447. it "should not raise an error for " +
  1448. "'http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
  1449. Addressable::URI.parse("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
  1450. end
  1451. it "should not raise an error for " +
  1452. "'http://[fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
  1453. Addressable::URI.parse("http://[fe80:0:0:0:200:f8ff:fe21:67cf]/")
  1454. end
  1455. it "should not raise an error for " +
  1456. "'http://[fe80::200:f8ff:fe21:67cf]/'" do
  1457. Addressable::URI.parse("http://[fe80::200:f8ff:fe21:67cf]/")
  1458. end
  1459. it "should not raise an error for " +
  1460. "'http://[::1]/'" do
  1461. Addressable::URI.parse("http://[::1]/")
  1462. end
  1463. it "should not raise an error for " +
  1464. "'http://[fe80::1]/'" do
  1465. Addressable::URI.parse("http://[fe80::1]/")
  1466. end
  1467. it "should raise an error for " +
  1468. "'http://[<invalid>]/'" do
  1469. (lambda do
  1470. Addressable::URI.parse("http://[<invalid>]/")
  1471. end).should raise_error(Addressable::URI::InvalidURIError)
  1472. end
  1473. end
  1474. describe Addressable::URI, "when parsing IPv6 address" do
  1475. subject { Addressable::URI.parse("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/") }
  1476. its(:host) { should == '[3ffe:1900:4545:3:200:f8ff:fe21:67cf]' }
  1477. its(:hostname) { should == '3ffe:1900:4545:3:200:f8ff:fe21:67cf' }
  1478. end
  1479. describe Addressable::URI, "when assigning IPv6 address" do
  1480. it "should allow to set bare IPv6 address as hostname" do
  1481. uri = Addressable::URI.parse("http://[::1]/")
  1482. uri.hostname = '3ffe:1900:4545:3:200:f8ff:fe21:67cf'
  1483. uri.to_s.should == 'http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'
  1484. end
  1485. it "should not allow to set bare IPv6 address as host" do
  1486. uri = Addressable::URI.parse("http://[::1]/")
  1487. pending "not checked"
  1488. (lambda do
  1489. uri.host = '3ffe:1900:4545:3:200:f8ff:fe21:67cf'
  1490. end).should raise_error(Addressable::URI::InvalidURIError)
  1491. end
  1492. end
  1493. describe Addressable::URI, "when parsing IPvFuture addresses" do
  1494. it "should not raise an error for " +
  1495. "'http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
  1496. Addressable::URI.parse("http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
  1497. end
  1498. it "should not raise an error for " +
  1499. "'http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
  1500. Addressable::URI.parse("http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/")
  1501. end
  1502. it "should not raise an error for " +
  1503. "'http://[v12.fe80::200:f8ff:fe21:67cf]/'" do
  1504. Addressable::URI.parse("http://[v12.fe80::200:f8ff:fe21:67cf]/")
  1505. end
  1506. it "should not raise an error for " +
  1507. "'http://[va0.::1]/'" do
  1508. Addressable::URI.parse("http://[va0.::1]/")
  1509. end
  1510. it "should not raise an error for " +
  1511. "'http://[v255.fe80::1]/'" do
  1512. Addressable::URI.parse("http://[v255.fe80::1]/")
  1513. end
  1514. it "should raise an error for " +
  1515. "'http://[v0.<invalid>]/'" do
  1516. (lambda do
  1517. Addressable::URI.parse("http://[v0.<invalid>]/")
  1518. end).should raise_error(Addressable::URI::InvalidURIError)
  1519. end
  1520. end
  1521. describe Addressable::URI, "when parsed from " +
  1522. "'http://example.com/'" do
  1523. before do
  1524. @uri = Addressable::URI.parse("http://example.com/")
  1525. end
  1526. # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
  1527. it "should be equivalent to http://example.com" do
  1528. @uri.should == Addressable::URI.parse("http://example.com")
  1529. end
  1530. # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
  1531. it "should be equivalent to HTTP://example.com/" do
  1532. @uri.should == Addressable::URI.parse("HTTP://example.com/")
  1533. end
  1534. # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
  1535. it "should be equivalent to http://example.com:/" do
  1536. @uri.should == Addressable::URI.parse("http://example.com:/")
  1537. end
  1538. # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
  1539. it "should be equivalent to http://example.com:80/" do
  1540. @uri.should == Addressable::URI.parse("http://example.com:80/")
  1541. end
  1542. # Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
  1543. it "should be equivalent to http://Example.com/" do
  1544. @uri.should == Addressable::URI.parse("http://Example.com/")
  1545. end
  1546. it "should have the correct username after assignment" do
  1547. @uri.user = nil
  1548. @uri.user.should == nil
  1549. @uri.password.should == nil
  1550. @uri.to

Large files files are truncated, but you can click here to view the full file