PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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