PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.9/uri/test_generic.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 720 lines | 524 code | 90 blank | 106 comment | 15 complexity | e97fbb95f8a82dfaaedacb69a7e51b6c MD5 | raw file
  1. require 'test/unit'
  2. require 'uri'
  3. class URI::TestGeneric < Test::Unit::TestCase
  4. def setup
  5. @url = 'http://a/b/c/d;p?q'
  6. @base_url = URI.parse(@url)
  7. end
  8. def teardown
  9. end
  10. def uri_to_ary(uri)
  11. uri.class.component.collect {|c| uri.send(c)}
  12. end
  13. def test_parse
  14. # 0
  15. assert_kind_of(URI::HTTP, @base_url)
  16. exp = [
  17. 'http',
  18. nil, 'a', URI::HTTP.default_port,
  19. '/b/c/d;p',
  20. 'q',
  21. nil
  22. ]
  23. ary = uri_to_ary(@base_url)
  24. assert_equal(exp, ary)
  25. # 1
  26. url = URI.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt')
  27. assert_kind_of(URI::FTP, url)
  28. exp = [
  29. 'ftp',
  30. nil, 'ftp.is.co.za', URI::FTP.default_port,
  31. 'rfc/rfc1808.txt', nil,
  32. ]
  33. ary = uri_to_ary(url)
  34. assert_equal(exp, ary)
  35. # 1'
  36. url = URI.parse('ftp://ftp.is.co.za/%2Frfc/rfc1808.txt')
  37. assert_kind_of(URI::FTP, url)
  38. exp = [
  39. 'ftp',
  40. nil, 'ftp.is.co.za', URI::FTP.default_port,
  41. '/rfc/rfc1808.txt', nil,
  42. ]
  43. ary = uri_to_ary(url)
  44. assert_equal(exp, ary)
  45. # 2
  46. url = URI.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles')
  47. assert_kind_of(URI::Generic, url)
  48. exp = [
  49. 'gopher',
  50. nil, 'spinaltap.micro.umn.edu', nil, nil,
  51. '/00/Weather/California/Los%20Angeles', nil,
  52. nil,
  53. nil
  54. ]
  55. ary = uri_to_ary(url)
  56. assert_equal(exp, ary)
  57. # 3
  58. url = URI.parse('http://www.math.uio.no/faq/compression-faq/part1.html')
  59. assert_kind_of(URI::HTTP, url)
  60. exp = [
  61. 'http',
  62. nil, 'www.math.uio.no', URI::HTTP.default_port,
  63. '/faq/compression-faq/part1.html',
  64. nil,
  65. nil
  66. ]
  67. ary = uri_to_ary(url)
  68. assert_equal(exp, ary)
  69. # 4
  70. url = URI.parse('mailto:mduerst@ifi.unizh.ch')
  71. assert_kind_of(URI::Generic, url)
  72. exp = [
  73. 'mailto',
  74. 'mduerst@ifi.unizh.ch',
  75. []
  76. ]
  77. ary = uri_to_ary(url)
  78. assert_equal(exp, ary)
  79. # 5
  80. url = URI.parse('news:comp.infosystems.www.servers.unix')
  81. assert_kind_of(URI::Generic, url)
  82. exp = [
  83. 'news',
  84. nil, nil, nil, nil,
  85. nil, 'comp.infosystems.www.servers.unix',
  86. nil,
  87. nil
  88. ]
  89. ary = uri_to_ary(url)
  90. assert_equal(exp, ary)
  91. # 6
  92. url = URI.parse('telnet://melvyl.ucop.edu/')
  93. assert_kind_of(URI::Generic, url)
  94. exp = [
  95. 'telnet',
  96. nil, 'melvyl.ucop.edu', nil, nil,
  97. '/', nil,
  98. nil,
  99. nil
  100. ]
  101. ary = uri_to_ary(url)
  102. assert_equal(exp, ary)
  103. # 7
  104. # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>
  105. assert_raise(URI::InvalidURIError) { URI.parse('http://a_b:80/') }
  106. assert_raise(URI::InvalidURIError) { URI.parse('http://a_b/') }
  107. # 8
  108. # reported by m_seki
  109. uri = URI.parse('file:///foo/bar.txt')
  110. assert_kind_of(URI::Generic, url)
  111. uri = URI.parse('file:/foo/bar.txt')
  112. assert_kind_of(URI::Generic, url)
  113. # 9
  114. url = URI.parse('ftp://:pass@localhost/')
  115. assert_equal('', url.user, "[ruby-dev:25667]")
  116. assert_equal('pass', url.password)
  117. assert_equal(':pass', url.userinfo, "[ruby-dev:25667]")
  118. url = URI.parse('ftp://user@localhost/')
  119. assert_equal('user', url.user)
  120. assert_equal(nil, url.password)
  121. assert_equal('user', url.userinfo)
  122. url = URI.parse('ftp://localhost/')
  123. assert_equal(nil, url.user)
  124. assert_equal(nil, url.password)
  125. assert_equal(nil, url.userinfo)
  126. end
  127. def test_merge
  128. u1 = URI.parse('http://foo')
  129. u2 = URI.parse('http://foo/')
  130. u3 = URI.parse('http://foo/bar')
  131. u4 = URI.parse('http://foo/bar/')
  132. assert_equal(URI.parse('http://foo/baz'), u1 + 'baz')
  133. assert_equal(URI.parse('http://foo/baz'), u2 + 'baz')
  134. assert_equal(URI.parse('http://foo/baz'), u3 + 'baz')
  135. assert_equal(URI.parse('http://foo/bar/baz'), u4 + 'baz')
  136. assert_equal(URI.parse('http://foo/baz'), u1 + '/baz')
  137. assert_equal(URI.parse('http://foo/baz'), u2 + '/baz')
  138. assert_equal(URI.parse('http://foo/baz'), u3 + '/baz')
  139. assert_equal(URI.parse('http://foo/baz'), u4 + '/baz')
  140. url = URI.parse('http://hoge/a.html') + 'b.html'
  141. assert_equal('http://hoge/b.html', url.to_s, "[ruby-dev:11508]")
  142. # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>
  143. url = URI.parse('http://a/b') + 'http://x/y'
  144. assert_equal('http://x/y', url.to_s)
  145. assert_equal(url, URI.parse('') + 'http://x/y')
  146. assert_equal(url, URI.parse('').normalize + 'http://x/y')
  147. assert_equal(url, URI.parse('http://a/b').normalize + 'http://x/y')
  148. u = URI.parse('http://foo/bar/baz')
  149. assert_equal(nil, u.merge!(""))
  150. assert_equal(nil, u.merge!(u))
  151. assert(nil != u.merge!("."))
  152. assert_equal('http://foo/bar/', u.to_s)
  153. assert(nil != u.merge!("../baz"))
  154. assert_equal('http://foo/baz', u.to_s)
  155. u0 = URI.parse('mailto:foo@example.com')
  156. u1 = URI.parse('mailto:foo@example.com#bar')
  157. assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")
  158. u0 = URI.parse('http://www.example.com/')
  159. u1 = URI.parse('http://www.example.com/foo/..') + './'
  160. assert_equal(u0, u1, "[ruby-list:39838]")
  161. u0 = URI.parse('http://www.example.com/foo/')
  162. u1 = URI.parse('http://www.example.com/foo/bar/..') + './'
  163. assert_equal(u0, u1)
  164. u0 = URI.parse('http://www.example.com/foo/bar/')
  165. u1 = URI.parse('http://www.example.com/foo/bar/baz/..') + './'
  166. assert_equal(u0, u1)
  167. u0 = URI.parse('http://www.example.com/')
  168. u1 = URI.parse('http://www.example.com/foo/bar/../..') + './'
  169. assert_equal(u0, u1)
  170. u0 = URI.parse('http://www.example.com/foo/')
  171. u1 = URI.parse('http://www.example.com/foo/bar/baz/../..') + './'
  172. assert_equal(u0, u1)
  173. u = URI.parse('http://www.example.com/')
  174. u0 = u + './foo/'
  175. u1 = u + './foo/bar/..'
  176. assert_equal(u0, u1, "[ruby-list:39844]")
  177. u = URI.parse('http://www.example.com/')
  178. u0 = u + './'
  179. u1 = u + './foo/bar/../..'
  180. assert_equal(u0, u1)
  181. end
  182. def test_route
  183. url = URI.parse('http://hoge/a.html').route_to('http://hoge/b.html')
  184. assert_equal('b.html', url.to_s)
  185. url = URI.parse('http://hoge/a/').route_to('http://hoge/b/')
  186. assert_equal('../b/', url.to_s)
  187. url = URI.parse('http://hoge/a/b').route_to('http://hoge/b/')
  188. assert_equal('../b/', url.to_s)
  189. url = URI.parse('http://hoge/a/b/').route_to('http://hoge/b/')
  190. assert_equal('../../b/', url.to_s)
  191. url = URI.parse('http://hoge/a/b/').route_to('http://HOGE/b/')
  192. assert_equal('../../b/', url.to_s)
  193. url = URI.parse('http://hoge/a/b/').route_to('http://MOGE/b/')
  194. assert_equal('//MOGE/b/', url.to_s)
  195. url = URI.parse('http://hoge/b').route_to('http://hoge/b/')
  196. assert_equal('b/', url.to_s)
  197. url = URI.parse('http://hoge/b/a').route_to('http://hoge/b/')
  198. assert_equal('./', url.to_s)
  199. url = URI.parse('http://hoge/b/').route_to('http://hoge/b')
  200. assert_equal('../b', url.to_s)
  201. url = URI.parse('http://hoge/b').route_to('http://hoge/b:c')
  202. assert_equal('./b:c', url.to_s)
  203. url = URI.parse('file:///a/b/').route_to('file:///a/b/')
  204. assert_equal('', url.to_s)
  205. url = URI.parse('file:///a/b/').route_to('file:///a/b')
  206. assert_equal('../b', url.to_s)
  207. url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar')
  208. assert_equal('#bar', url.to_s)
  209. url = URI.parse('mailto:foo@example.com#bar').route_to('mailto:foo@example.com')
  210. assert_equal('', url.to_s)
  211. url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com')
  212. assert_equal('', url.to_s)
  213. end
  214. def test_rfc3986_examples
  215. # http://a/b/c/d;p?q
  216. # g:h = g:h
  217. url = @base_url.merge('g:h')
  218. assert_kind_of(URI::Generic, url)
  219. assert_equal('g:h', url.to_s)
  220. url = @base_url.route_to('g:h')
  221. assert_kind_of(URI::Generic, url)
  222. assert_equal('g:h', url.to_s)
  223. # http://a/b/c/d;p?q
  224. # g = http://a/b/c/g
  225. url = @base_url.merge('g')
  226. assert_kind_of(URI::HTTP, url)
  227. assert_equal('http://a/b/c/g', url.to_s)
  228. url = @base_url.route_to('http://a/b/c/g')
  229. assert_kind_of(URI::Generic, url)
  230. assert_equal('g', url.to_s)
  231. # http://a/b/c/d;p?q
  232. # ./g = http://a/b/c/g
  233. url = @base_url.merge('./g')
  234. assert_kind_of(URI::HTTP, url)
  235. assert_equal('http://a/b/c/g', url.to_s)
  236. url = @base_url.route_to('http://a/b/c/g')
  237. assert_kind_of(URI::Generic, url)
  238. assert('./g' != url.to_s) # ok
  239. assert_equal('g', url.to_s)
  240. # http://a/b/c/d;p?q
  241. # g/ = http://a/b/c/g/
  242. url = @base_url.merge('g/')
  243. assert_kind_of(URI::HTTP, url)
  244. assert_equal('http://a/b/c/g/', url.to_s)
  245. url = @base_url.route_to('http://a/b/c/g/')
  246. assert_kind_of(URI::Generic, url)
  247. assert_equal('g/', url.to_s)
  248. # http://a/b/c/d;p?q
  249. # /g = http://a/g
  250. url = @base_url.merge('/g')
  251. assert_kind_of(URI::HTTP, url)
  252. assert_equal('http://a/g', url.to_s)
  253. url = @base_url.route_to('http://a/g')
  254. assert_kind_of(URI::Generic, url)
  255. assert('/g' != url.to_s) # ok
  256. assert_equal('../../g', url.to_s)
  257. # http://a/b/c/d;p?q
  258. # //g = http://g
  259. url = @base_url.merge('//g')
  260. assert_kind_of(URI::HTTP, url)
  261. assert_equal('http://g', url.to_s)
  262. url = @base_url.route_to('http://g')
  263. assert_kind_of(URI::Generic, url)
  264. assert_equal('//g', url.to_s)
  265. # http://a/b/c/d;p?q
  266. # ?y = http://a/b/c/d;p?y
  267. url = @base_url.merge('?y')
  268. assert_kind_of(URI::HTTP, url)
  269. assert_equal('http://a/b/c/d;p?y', url.to_s)
  270. url = @base_url.route_to('http://a/b/c/d;p?y')
  271. assert_kind_of(URI::Generic, url)
  272. assert_equal('?y', url.to_s)
  273. # http://a/b/c/d;p?q
  274. # g?y = http://a/b/c/g?y
  275. url = @base_url.merge('g?y')
  276. assert_kind_of(URI::HTTP, url)
  277. assert_equal('http://a/b/c/g?y', url.to_s)
  278. url = @base_url.route_to('http://a/b/c/g?y')
  279. assert_kind_of(URI::Generic, url)
  280. assert_equal('g?y', url.to_s)
  281. # http://a/b/c/d;p?q
  282. # #s = http://a/b/c/d;p?q#s
  283. url = @base_url.merge('#s')
  284. assert_kind_of(URI::HTTP, url)
  285. assert_equal('http://a/b/c/d;p?q#s', url.to_s)
  286. url = @base_url.route_to('http://a/b/c/d;p?q#s')
  287. assert_kind_of(URI::Generic, url)
  288. assert_equal('#s', url.to_s)
  289. # http://a/b/c/d;p?q
  290. # g#s = http://a/b/c/g#s
  291. url = @base_url.merge('g#s')
  292. assert_kind_of(URI::HTTP, url)
  293. assert_equal('http://a/b/c/g#s', url.to_s)
  294. url = @base_url.route_to('http://a/b/c/g#s')
  295. assert_kind_of(URI::Generic, url)
  296. assert_equal('g#s', url.to_s)
  297. # http://a/b/c/d;p?q
  298. # g?y#s = http://a/b/c/g?y#s
  299. url = @base_url.merge('g?y#s')
  300. assert_kind_of(URI::HTTP, url)
  301. assert_equal('http://a/b/c/g?y#s', url.to_s)
  302. url = @base_url.route_to('http://a/b/c/g?y#s')
  303. assert_kind_of(URI::Generic, url)
  304. assert_equal('g?y#s', url.to_s)
  305. # http://a/b/c/d;p?q
  306. # ;x = http://a/b/c/;x
  307. url = @base_url.merge(';x')
  308. assert_kind_of(URI::HTTP, url)
  309. assert_equal('http://a/b/c/;x', url.to_s)
  310. url = @base_url.route_to('http://a/b/c/;x')
  311. assert_kind_of(URI::Generic, url)
  312. assert_equal(';x', url.to_s)
  313. # http://a/b/c/d;p?q
  314. # g;x = http://a/b/c/g;x
  315. url = @base_url.merge('g;x')
  316. assert_kind_of(URI::HTTP, url)
  317. assert_equal('http://a/b/c/g;x', url.to_s)
  318. url = @base_url.route_to('http://a/b/c/g;x')
  319. assert_kind_of(URI::Generic, url)
  320. assert_equal('g;x', url.to_s)
  321. # http://a/b/c/d;p?q
  322. # g;x?y#s = http://a/b/c/g;x?y#s
  323. url = @base_url.merge('g;x?y#s')
  324. assert_kind_of(URI::HTTP, url)
  325. assert_equal('http://a/b/c/g;x?y#s', url.to_s)
  326. url = @base_url.route_to('http://a/b/c/g;x?y#s')
  327. assert_kind_of(URI::Generic, url)
  328. assert_equal('g;x?y#s', url.to_s)
  329. # http://a/b/c/d;p?q
  330. # . = http://a/b/c/
  331. url = @base_url.merge('.')
  332. assert_kind_of(URI::HTTP, url)
  333. assert_equal('http://a/b/c/', url.to_s)
  334. url = @base_url.route_to('http://a/b/c/')
  335. assert_kind_of(URI::Generic, url)
  336. assert('.' != url.to_s) # ok
  337. assert_equal('./', url.to_s)
  338. # http://a/b/c/d;p?q
  339. # ./ = http://a/b/c/
  340. url = @base_url.merge('./')
  341. assert_kind_of(URI::HTTP, url)
  342. assert_equal('http://a/b/c/', url.to_s)
  343. url = @base_url.route_to('http://a/b/c/')
  344. assert_kind_of(URI::Generic, url)
  345. assert_equal('./', url.to_s)
  346. # http://a/b/c/d;p?q
  347. # .. = http://a/b/
  348. url = @base_url.merge('..')
  349. assert_kind_of(URI::HTTP, url)
  350. assert_equal('http://a/b/', url.to_s)
  351. url = @base_url.route_to('http://a/b/')
  352. assert_kind_of(URI::Generic, url)
  353. assert('..' != url.to_s) # ok
  354. assert_equal('../', url.to_s)
  355. # http://a/b/c/d;p?q
  356. # ../ = http://a/b/
  357. url = @base_url.merge('../')
  358. assert_kind_of(URI::HTTP, url)
  359. assert_equal('http://a/b/', url.to_s)
  360. url = @base_url.route_to('http://a/b/')
  361. assert_kind_of(URI::Generic, url)
  362. assert_equal('../', url.to_s)
  363. # http://a/b/c/d;p?q
  364. # ../g = http://a/b/g
  365. url = @base_url.merge('../g')
  366. assert_kind_of(URI::HTTP, url)
  367. assert_equal('http://a/b/g', url.to_s)
  368. url = @base_url.route_to('http://a/b/g')
  369. assert_kind_of(URI::Generic, url)
  370. assert_equal('../g', url.to_s)
  371. # http://a/b/c/d;p?q
  372. # ../.. = http://a/
  373. url = @base_url.merge('../..')
  374. assert_kind_of(URI::HTTP, url)
  375. assert_equal('http://a/', url.to_s)
  376. url = @base_url.route_to('http://a/')
  377. assert_kind_of(URI::Generic, url)
  378. assert('../..' != url.to_s) # ok
  379. assert_equal('../../', url.to_s)
  380. # http://a/b/c/d;p?q
  381. # ../../ = http://a/
  382. url = @base_url.merge('../../')
  383. assert_kind_of(URI::HTTP, url)
  384. assert_equal('http://a/', url.to_s)
  385. url = @base_url.route_to('http://a/')
  386. assert_kind_of(URI::Generic, url)
  387. assert_equal('../../', url.to_s)
  388. # http://a/b/c/d;p?q
  389. # ../../g = http://a/g
  390. url = @base_url.merge('../../g')
  391. assert_kind_of(URI::HTTP, url)
  392. assert_equal('http://a/g', url.to_s)
  393. url = @base_url.route_to('http://a/g')
  394. assert_kind_of(URI::Generic, url)
  395. assert_equal('../../g', url.to_s)
  396. # http://a/b/c/d;p?q
  397. # <> = (current document)
  398. url = @base_url.merge('')
  399. assert_kind_of(URI::HTTP, url)
  400. assert_equal('http://a/b/c/d;p?q', url.to_s)
  401. url = @base_url.route_to('http://a/b/c/d;p?q')
  402. assert_kind_of(URI::Generic, url)
  403. assert_equal('', url.to_s)
  404. # http://a/b/c/d;p?q
  405. # /./g = http://a/g
  406. url = @base_url.merge('/./g')
  407. assert_kind_of(URI::HTTP, url)
  408. assert_equal('http://a/g', url.to_s)
  409. # url = @base_url.route_to('http://a/./g')
  410. # assert_kind_of(URI::Generic, url)
  411. # assert_equal('/./g', url.to_s)
  412. # http://a/b/c/d;p?q
  413. # /../g = http://a/g
  414. url = @base_url.merge('/../g')
  415. assert_kind_of(URI::HTTP, url)
  416. assert_equal('http://a/g', url.to_s)
  417. # url = @base_url.route_to('http://a/../g')
  418. # assert_kind_of(URI::Generic, url)
  419. # assert_equal('/../g', url.to_s)
  420. # http://a/b/c/d;p?q
  421. # g. = http://a/b/c/g.
  422. url = @base_url.merge('g.')
  423. assert_kind_of(URI::HTTP, url)
  424. assert_equal('http://a/b/c/g.', url.to_s)
  425. url = @base_url.route_to('http://a/b/c/g.')
  426. assert_kind_of(URI::Generic, url)
  427. assert_equal('g.', url.to_s)
  428. # http://a/b/c/d;p?q
  429. # .g = http://a/b/c/.g
  430. url = @base_url.merge('.g')
  431. assert_kind_of(URI::HTTP, url)
  432. assert_equal('http://a/b/c/.g', url.to_s)
  433. url = @base_url.route_to('http://a/b/c/.g')
  434. assert_kind_of(URI::Generic, url)
  435. assert_equal('.g', url.to_s)
  436. # http://a/b/c/d;p?q
  437. # g.. = http://a/b/c/g..
  438. url = @base_url.merge('g..')
  439. assert_kind_of(URI::HTTP, url)
  440. assert_equal('http://a/b/c/g..', url.to_s)
  441. url = @base_url.route_to('http://a/b/c/g..')
  442. assert_kind_of(URI::Generic, url)
  443. assert_equal('g..', url.to_s)
  444. # http://a/b/c/d;p?q
  445. # ..g = http://a/b/c/..g
  446. url = @base_url.merge('..g')
  447. assert_kind_of(URI::HTTP, url)
  448. assert_equal('http://a/b/c/..g', url.to_s)
  449. url = @base_url.route_to('http://a/b/c/..g')
  450. assert_kind_of(URI::Generic, url)
  451. assert_equal('..g', url.to_s)
  452. # http://a/b/c/d;p?q
  453. # ../../../g = http://a/g
  454. url = @base_url.merge('../../../g')
  455. assert_kind_of(URI::HTTP, url)
  456. assert_equal('http://a/g', url.to_s)
  457. url = @base_url.route_to('http://a/g')
  458. assert_kind_of(URI::Generic, url)
  459. assert('../../../g' != url.to_s) # ok? yes, it confuses you
  460. assert_equal('../../g', url.to_s) # and it is clearly
  461. # http://a/b/c/d;p?q
  462. # ../../../../g = http://a/g
  463. url = @base_url.merge('../../../../g')
  464. assert_kind_of(URI::HTTP, url)
  465. assert_equal('http://a/g', url.to_s)
  466. url = @base_url.route_to('http://a/g')
  467. assert_kind_of(URI::Generic, url)
  468. assert('../../../../g' != url.to_s) # ok? yes, it confuses you
  469. assert_equal('../../g', url.to_s) # and it is clearly
  470. # http://a/b/c/d;p?q
  471. # ./../g = http://a/b/g
  472. url = @base_url.merge('./../g')
  473. assert_kind_of(URI::HTTP, url)
  474. assert_equal('http://a/b/g', url.to_s)
  475. url = @base_url.route_to('http://a/b/g')
  476. assert_kind_of(URI::Generic, url)
  477. assert('./../g' != url.to_s) # ok
  478. assert_equal('../g', url.to_s)
  479. # http://a/b/c/d;p?q
  480. # ./g/. = http://a/b/c/g/
  481. url = @base_url.merge('./g/.')
  482. assert_kind_of(URI::HTTP, url)
  483. assert_equal('http://a/b/c/g/', url.to_s)
  484. url = @base_url.route_to('http://a/b/c/g/')
  485. assert_kind_of(URI::Generic, url)
  486. assert('./g/.' != url.to_s) # ok
  487. assert_equal('g/', url.to_s)
  488. # http://a/b/c/d;p?q
  489. # g/./h = http://a/b/c/g/h
  490. url = @base_url.merge('g/./h')
  491. assert_kind_of(URI::HTTP, url)
  492. assert_equal('http://a/b/c/g/h', url.to_s)
  493. url = @base_url.route_to('http://a/b/c/g/h')
  494. assert_kind_of(URI::Generic, url)
  495. assert('g/./h' != url.to_s) # ok
  496. assert_equal('g/h', url.to_s)
  497. # http://a/b/c/d;p?q
  498. # g/../h = http://a/b/c/h
  499. url = @base_url.merge('g/../h')
  500. assert_kind_of(URI::HTTP, url)
  501. assert_equal('http://a/b/c/h', url.to_s)
  502. url = @base_url.route_to('http://a/b/c/h')
  503. assert_kind_of(URI::Generic, url)
  504. assert('g/../h' != url.to_s) # ok
  505. assert_equal('h', url.to_s)
  506. # http://a/b/c/d;p?q
  507. # g;x=1/./y = http://a/b/c/g;x=1/y
  508. url = @base_url.merge('g;x=1/./y')
  509. assert_kind_of(URI::HTTP, url)
  510. assert_equal('http://a/b/c/g;x=1/y', url.to_s)
  511. url = @base_url.route_to('http://a/b/c/g;x=1/y')
  512. assert_kind_of(URI::Generic, url)
  513. assert('g;x=1/./y' != url.to_s) # ok
  514. assert_equal('g;x=1/y', url.to_s)
  515. # http://a/b/c/d;p?q
  516. # g;x=1/../y = http://a/b/c/y
  517. url = @base_url.merge('g;x=1/../y')
  518. assert_kind_of(URI::HTTP, url)
  519. assert_equal('http://a/b/c/y', url.to_s)
  520. url = @base_url.route_to('http://a/b/c/y')
  521. assert_kind_of(URI::Generic, url)
  522. assert('g;x=1/../y' != url.to_s) # ok
  523. assert_equal('y', url.to_s)
  524. # http://a/b/c/d;p?q
  525. # g?y/./x = http://a/b/c/g?y/./x
  526. url = @base_url.merge('g?y/./x')
  527. assert_kind_of(URI::HTTP, url)
  528. assert_equal('http://a/b/c/g?y/./x', url.to_s)
  529. url = @base_url.route_to('http://a/b/c/g?y/./x')
  530. assert_kind_of(URI::Generic, url)
  531. assert_equal('g?y/./x', url.to_s)
  532. # http://a/b/c/d;p?q
  533. # g?y/../x = http://a/b/c/g?y/../x
  534. url = @base_url.merge('g?y/../x')
  535. assert_kind_of(URI::HTTP, url)
  536. assert_equal('http://a/b/c/g?y/../x', url.to_s)
  537. url = @base_url.route_to('http://a/b/c/g?y/../x')
  538. assert_kind_of(URI::Generic, url)
  539. assert_equal('g?y/../x', url.to_s)
  540. # http://a/b/c/d;p?q
  541. # g#s/./x = http://a/b/c/g#s/./x
  542. url = @base_url.merge('g#s/./x')
  543. assert_kind_of(URI::HTTP, url)
  544. assert_equal('http://a/b/c/g#s/./x', url.to_s)
  545. url = @base_url.route_to('http://a/b/c/g#s/./x')
  546. assert_kind_of(URI::Generic, url)
  547. assert_equal('g#s/./x', url.to_s)
  548. # http://a/b/c/d;p?q
  549. # g#s/../x = http://a/b/c/g#s/../x
  550. url = @base_url.merge('g#s/../x')
  551. assert_kind_of(URI::HTTP, url)
  552. assert_equal('http://a/b/c/g#s/../x', url.to_s)
  553. url = @base_url.route_to('http://a/b/c/g#s/../x')
  554. assert_kind_of(URI::Generic, url)
  555. assert_equal('g#s/../x', url.to_s)
  556. # http://a/b/c/d;p?q
  557. # http:g = http:g ; for validating parsers
  558. # | http://a/b/c/g ; for backwards compatibility
  559. url = @base_url.merge('http:g')
  560. assert_kind_of(URI::HTTP, url)
  561. assert_equal('http:g', url.to_s)
  562. url = @base_url.route_to('http:g')
  563. assert_kind_of(URI::Generic, url)
  564. assert_equal('http:g', url.to_s)
  565. end
  566. def test_join
  567. assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar'))
  568. assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar'))
  569. assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/'))
  570. assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz'))
  571. assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz'))
  572. assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/'))
  573. assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz'))
  574. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge'))
  575. assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz'))
  576. assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
  577. assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
  578. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))
  579. assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
  580. assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
  581. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))
  582. end
  583. # ruby-dev:16728
  584. def test_set_component
  585. uri = URI.parse('http://foo:bar@baz')
  586. assert_equal('oof', uri.user = 'oof')
  587. assert_equal('http://oof:bar@baz', uri.to_s)
  588. assert_equal('rab', uri.password = 'rab')
  589. assert_equal('http://oof:rab@baz', uri.to_s)
  590. assert_equal('foo', uri.userinfo = 'foo')
  591. assert_equal('http://foo:rab@baz', uri.to_s)
  592. assert_equal(['foo', 'bar'], uri.userinfo = ['foo', 'bar'])
  593. assert_equal('http://foo:bar@baz', uri.to_s)
  594. assert_equal(['foo'], uri.userinfo = ['foo'])
  595. assert_equal('http://foo:bar@baz', uri.to_s)
  596. assert_equal('zab', uri.host = 'zab')
  597. assert_equal('http://foo:bar@zab', uri.to_s)
  598. assert_equal(8080, uri.port = 8080)
  599. assert_equal('http://foo:bar@zab:8080', uri.to_s)
  600. assert_equal('/', uri.path = '/')
  601. assert_equal('http://foo:bar@zab:8080/', uri.to_s)
  602. assert_equal('a=1', uri.query = 'a=1')
  603. assert_equal('http://foo:bar@zab:8080/?a=1', uri.to_s)
  604. assert_equal('b123', uri.fragment = 'b123')
  605. assert_equal('http://foo:bar@zab:8080/?a=1#b123', uri.to_s)
  606. uri = URI.parse('http://example.com')
  607. assert_raise(URI::InvalidURIError) { uri.password = 'bar' }
  608. assert_raise(URI::InvalidComponentError) { uri.query = "foo\nbar" }
  609. uri.userinfo = 'foo:bar'
  610. assert_equal('http://foo:bar@example.com', uri.to_s)
  611. assert_raise(URI::InvalidURIError) { uri.registry = 'bar' }
  612. assert_raise(URI::InvalidURIError) { uri.opaque = 'bar' }
  613. uri = URI.parse('mailto:foo@example.com')
  614. assert_raise(URI::InvalidURIError) { uri.user = 'bar' }
  615. assert_raise(URI::InvalidURIError) { uri.password = 'bar' }
  616. assert_raise(URI::InvalidURIError) { uri.userinfo = ['bar', 'baz'] }
  617. assert_raise(URI::InvalidURIError) { uri.host = 'bar' }
  618. assert_raise(URI::InvalidURIError) { uri.port = 'bar' }
  619. assert_raise(URI::InvalidURIError) { uri.path = 'bar' }
  620. assert_raise(URI::InvalidURIError) { uri.query = 'bar' }
  621. end
  622. def test_ipv6
  623. assert_equal("[::1]", URI("http://[::1]/bar/baz").host)
  624. assert_equal("::1", URI("http://[::1]/bar/baz").hostname)
  625. u = URI("http://foo/bar")
  626. assert_equal("http://foo/bar", u.to_s)
  627. u.hostname = "::1"
  628. assert_equal("http://[::1]/bar", u.to_s)
  629. end
  630. end