PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/uri/test_generic.rb

http://github.com/ruby/ruby
Ruby | 1048 lines | 807 code | 133 blank | 108 comment | 17 complexity | 70f99ed4efe502dd3fb207c32ba0407f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'envutil'
  4. require 'uri'
  5. class URI::TestGeneric < Test::Unit::TestCase
  6. def setup
  7. @url = 'http://a/b/c/d;p?q'
  8. @base_url = URI.parse(@url)
  9. end
  10. def teardown
  11. end
  12. def uri_to_ary(uri)
  13. uri.class.component.collect {|c| uri.send(c)}
  14. end
  15. def test_to_s
  16. exp = 'http://example.com/'.freeze
  17. str = URI(exp).to_s
  18. assert_equal exp, str
  19. assert_not_predicate str, :frozen?, '[ruby-core:71785] [Bug #11759]'
  20. assert_equal "file:///foo", URI("file:///foo").to_s
  21. assert_equal "postgres:///foo", URI("postgres:///foo").to_s
  22. assert_equal "http:/foo", URI("http:///foo").to_s
  23. end
  24. def test_parse
  25. # 0
  26. assert_kind_of(URI::HTTP, @base_url)
  27. exp = [
  28. 'http',
  29. nil, 'a', URI::HTTP.default_port,
  30. '/b/c/d;p',
  31. 'q',
  32. nil
  33. ]
  34. ary = uri_to_ary(@base_url)
  35. assert_equal(exp, ary)
  36. # 1
  37. url = URI.parse('ftp://ftp.is.co.za/rfc/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. # 1'
  47. url = URI.parse('ftp://ftp.is.co.za/%2Frfc/rfc1808.txt')
  48. assert_kind_of(URI::FTP, url)
  49. exp = [
  50. 'ftp',
  51. nil, 'ftp.is.co.za', URI::FTP.default_port,
  52. '/rfc/rfc1808.txt', nil,
  53. ]
  54. ary = uri_to_ary(url)
  55. assert_equal(exp, ary)
  56. # 2
  57. url = URI.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles')
  58. assert_kind_of(URI::Generic, url)
  59. exp = [
  60. 'gopher',
  61. nil, 'spinaltap.micro.umn.edu', nil, nil,
  62. '/00/Weather/California/Los%20Angeles', nil,
  63. nil,
  64. nil
  65. ]
  66. ary = uri_to_ary(url)
  67. assert_equal(exp, ary)
  68. # 3
  69. url = URI.parse('http://www.math.uio.no/faq/compression-faq/part1.html')
  70. assert_kind_of(URI::HTTP, url)
  71. exp = [
  72. 'http',
  73. nil, 'www.math.uio.no', URI::HTTP.default_port,
  74. '/faq/compression-faq/part1.html',
  75. nil,
  76. nil
  77. ]
  78. ary = uri_to_ary(url)
  79. assert_equal(exp, ary)
  80. # 4
  81. url = URI.parse('mailto:mduerst@ifi.unizh.ch')
  82. assert_kind_of(URI::Generic, url)
  83. exp = [
  84. 'mailto',
  85. 'mduerst@ifi.unizh.ch',
  86. []
  87. ]
  88. ary = uri_to_ary(url)
  89. assert_equal(exp, ary)
  90. # 5
  91. url = URI.parse('news:comp.infosystems.www.servers.unix')
  92. assert_kind_of(URI::Generic, url)
  93. exp = [
  94. 'news',
  95. nil, nil, nil, nil,
  96. nil, 'comp.infosystems.www.servers.unix',
  97. nil,
  98. nil
  99. ]
  100. ary = uri_to_ary(url)
  101. assert_equal(exp, ary)
  102. # 6
  103. url = URI.parse('telnet://melvyl.ucop.edu/')
  104. assert_kind_of(URI::Generic, url)
  105. exp = [
  106. 'telnet',
  107. nil, 'melvyl.ucop.edu', nil, nil,
  108. '/', nil,
  109. nil,
  110. nil
  111. ]
  112. ary = uri_to_ary(url)
  113. assert_equal(exp, ary)
  114. # 7
  115. # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>
  116. assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b:80/') }
  117. assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b/') }
  118. # 8
  119. # reported by m_seki
  120. url = URI.parse('file:///foo/bar.txt')
  121. assert_kind_of(URI::Generic, url)
  122. url = URI.parse('file:/foo/bar.txt')
  123. assert_kind_of(URI::Generic, url)
  124. # 9
  125. url = URI.parse('ftp://:pass@localhost/')
  126. assert_equal('', url.user, "[ruby-dev:25667]")
  127. assert_equal('pass', url.password)
  128. assert_equal(':pass', url.userinfo, "[ruby-dev:25667]")
  129. url = URI.parse('ftp://user@localhost/')
  130. assert_equal('user', url.user)
  131. assert_equal(nil, url.password)
  132. assert_equal('user', url.userinfo)
  133. url = URI.parse('ftp://localhost/')
  134. assert_equal(nil, url.user)
  135. assert_equal(nil, url.password)
  136. assert_equal(nil, url.userinfo)
  137. end
  138. def test_merge
  139. u1 = URI.parse('http://foo')
  140. u2 = URI.parse('http://foo/')
  141. u3 = URI.parse('http://foo/bar')
  142. u4 = URI.parse('http://foo/bar/')
  143. {
  144. u1 => {
  145. 'baz' => 'http://foo/baz',
  146. '/baz' => 'http://foo/baz',
  147. },
  148. u2 => {
  149. 'baz' => 'http://foo/baz',
  150. '/baz' => 'http://foo/baz',
  151. },
  152. u3 => {
  153. 'baz' => 'http://foo/baz',
  154. '/baz' => 'http://foo/baz',
  155. },
  156. u4 => {
  157. 'baz' => 'http://foo/bar/baz',
  158. '/baz' => 'http://foo/baz',
  159. },
  160. }.each { |base, map|
  161. map.each { |url, result|
  162. expected = URI.parse(result)
  163. uri = URI.parse(url)
  164. assert_equal expected, base + url, "<#{base}> + #{url.inspect} to become <#{expected}>"
  165. assert_equal expected, base + uri, "<#{base}> + <#{uri}> to become <#{expected}>"
  166. }
  167. }
  168. url = URI.parse('http://hoge/a.html') + 'b.html'
  169. assert_equal('http://hoge/b.html', url.to_s, "[ruby-dev:11508]")
  170. # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>
  171. url = URI.parse('http://a/b') + 'http://x/y'
  172. assert_equal('http://x/y', url.to_s)
  173. assert_equal(url, URI.parse('') + 'http://x/y')
  174. assert_equal(url, URI.parse('').normalize + 'http://x/y')
  175. assert_equal(url, URI.parse('http://a/b').normalize + 'http://x/y')
  176. u = URI.parse('http://foo/bar/baz')
  177. assert_equal(nil, u.merge!(""))
  178. assert_equal(nil, u.merge!(u))
  179. assert(nil != u.merge!("."))
  180. assert_equal('http://foo/bar/', u.to_s)
  181. assert(nil != u.merge!("../baz"))
  182. assert_equal('http://foo/baz', u.to_s)
  183. url = URI.parse('http://a/b//c') + 'd//e'
  184. assert_equal('http://a/b//d//e', url.to_s)
  185. u0 = URI.parse('mailto:foo@example.com')
  186. u1 = URI.parse('mailto:foo@example.com#bar')
  187. assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")
  188. u0 = URI.parse('http://www.example.com/')
  189. u1 = URI.parse('http://www.example.com/foo/..') + './'
  190. assert_equal(u0, u1, "[ruby-list:39838]")
  191. u0 = URI.parse('http://www.example.com/foo/')
  192. u1 = URI.parse('http://www.example.com/foo/bar/..') + './'
  193. assert_equal(u0, u1)
  194. u0 = URI.parse('http://www.example.com/foo/bar/')
  195. u1 = URI.parse('http://www.example.com/foo/bar/baz/..') + './'
  196. assert_equal(u0, u1)
  197. u0 = URI.parse('http://www.example.com/')
  198. u1 = URI.parse('http://www.example.com/foo/bar/../..') + './'
  199. assert_equal(u0, u1)
  200. u0 = URI.parse('http://www.example.com/foo/')
  201. u1 = URI.parse('http://www.example.com/foo/bar/baz/../..') + './'
  202. assert_equal(u0, u1)
  203. u = URI.parse('http://www.example.com/')
  204. u0 = u + './foo/'
  205. u1 = u + './foo/bar/..'
  206. assert_equal(u0, u1, "[ruby-list:39844]")
  207. u = URI.parse('http://www.example.com/')
  208. u0 = u + './'
  209. u1 = u + './foo/bar/../..'
  210. assert_equal(u0, u1)
  211. end
  212. def test_route
  213. url = URI.parse('http://hoge/a.html').route_to('http://hoge/b.html')
  214. assert_equal('b.html', url.to_s)
  215. url = URI.parse('http://hoge/a/').route_to('http://hoge/b/')
  216. assert_equal('../b/', url.to_s)
  217. url = URI.parse('http://hoge/a/b').route_to('http://hoge/b/')
  218. assert_equal('../b/', url.to_s)
  219. url = URI.parse('http://hoge/a/b/').route_to('http://hoge/b/')
  220. assert_equal('../../b/', url.to_s)
  221. url = URI.parse('http://hoge/a/b/').route_to('http://HOGE/b/')
  222. assert_equal('../../b/', url.to_s)
  223. url = URI.parse('http://hoge/a/b/').route_to('http://MOGE/b/')
  224. assert_equal('//MOGE/b/', url.to_s)
  225. url = URI.parse('http://hoge/b').route_to('http://hoge/b/')
  226. assert_equal('b/', url.to_s)
  227. url = URI.parse('http://hoge/b/a').route_to('http://hoge/b/')
  228. assert_equal('./', url.to_s)
  229. url = URI.parse('http://hoge/b/').route_to('http://hoge/b')
  230. assert_equal('../b', url.to_s)
  231. url = URI.parse('http://hoge/b').route_to('http://hoge/b:c')
  232. assert_equal('./b:c', url.to_s)
  233. url = URI.parse('http://hoge/b//c').route_to('http://hoge/b/c')
  234. assert_equal('../c', url.to_s)
  235. url = URI.parse('file:///a/b/').route_to('file:///a/b/')
  236. assert_equal('', url.to_s)
  237. url = URI.parse('file:///a/b/').route_to('file:///a/b')
  238. assert_equal('../b', url.to_s)
  239. url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar')
  240. assert_equal('#bar', url.to_s)
  241. url = URI.parse('mailto:foo@example.com#bar').route_to('mailto:foo@example.com')
  242. assert_equal('', url.to_s)
  243. url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com')
  244. assert_equal('', url.to_s)
  245. end
  246. def test_rfc3986_examples
  247. # http://a/b/c/d;p?q
  248. # g:h = g:h
  249. url = @base_url.merge('g:h')
  250. assert_kind_of(URI::Generic, url)
  251. assert_equal('g:h', url.to_s)
  252. url = @base_url.route_to('g:h')
  253. assert_kind_of(URI::Generic, url)
  254. assert_equal('g:h', url.to_s)
  255. # http://a/b/c/d;p?q
  256. # g = http://a/b/c/g
  257. url = @base_url.merge('g')
  258. assert_kind_of(URI::HTTP, url)
  259. assert_equal('http://a/b/c/g', url.to_s)
  260. url = @base_url.route_to('http://a/b/c/g')
  261. assert_kind_of(URI::Generic, url)
  262. assert_equal('g', url.to_s)
  263. # http://a/b/c/d;p?q
  264. # ./g = http://a/b/c/g
  265. url = @base_url.merge('./g')
  266. assert_kind_of(URI::HTTP, url)
  267. assert_equal('http://a/b/c/g', url.to_s)
  268. url = @base_url.route_to('http://a/b/c/g')
  269. assert_kind_of(URI::Generic, url)
  270. assert('./g' != url.to_s) # ok
  271. assert_equal('g', url.to_s)
  272. # http://a/b/c/d;p?q
  273. # g/ = http://a/b/c/g/
  274. url = @base_url.merge('g/')
  275. assert_kind_of(URI::HTTP, url)
  276. assert_equal('http://a/b/c/g/', url.to_s)
  277. url = @base_url.route_to('http://a/b/c/g/')
  278. assert_kind_of(URI::Generic, url)
  279. assert_equal('g/', url.to_s)
  280. # http://a/b/c/d;p?q
  281. # /g = http://a/g
  282. url = @base_url.merge('/g')
  283. assert_kind_of(URI::HTTP, url)
  284. assert_equal('http://a/g', url.to_s)
  285. url = @base_url.route_to('http://a/g')
  286. assert_kind_of(URI::Generic, url)
  287. assert('/g' != url.to_s) # ok
  288. assert_equal('../../g', url.to_s)
  289. # http://a/b/c/d;p?q
  290. # //g = http://g
  291. url = @base_url.merge('//g')
  292. assert_kind_of(URI::HTTP, url)
  293. assert_equal('http://g', url.to_s)
  294. url = @base_url.route_to('http://g')
  295. assert_kind_of(URI::Generic, url)
  296. assert_equal('//g', url.to_s)
  297. # http://a/b/c/d;p?q
  298. # ?y = http://a/b/c/d;p?y
  299. url = @base_url.merge('?y')
  300. assert_kind_of(URI::HTTP, url)
  301. assert_equal('http://a/b/c/d;p?y', url.to_s)
  302. url = @base_url.route_to('http://a/b/c/d;p?y')
  303. assert_kind_of(URI::Generic, url)
  304. assert_equal('?y', url.to_s)
  305. # http://a/b/c/d;p?q
  306. # g?y = http://a/b/c/g?y
  307. url = @base_url.merge('g?y')
  308. assert_kind_of(URI::HTTP, url)
  309. assert_equal('http://a/b/c/g?y', url.to_s)
  310. url = @base_url.route_to('http://a/b/c/g?y')
  311. assert_kind_of(URI::Generic, url)
  312. assert_equal('g?y', url.to_s)
  313. # http://a/b/c/d;p?q
  314. # #s = http://a/b/c/d;p?q#s
  315. url = @base_url.merge('#s')
  316. assert_kind_of(URI::HTTP, url)
  317. assert_equal('http://a/b/c/d;p?q#s', url.to_s)
  318. url = @base_url.route_to('http://a/b/c/d;p?q#s')
  319. assert_kind_of(URI::Generic, url)
  320. assert_equal('#s', url.to_s)
  321. # http://a/b/c/d;p?q
  322. # g#s = http://a/b/c/g#s
  323. url = @base_url.merge('g#s')
  324. assert_kind_of(URI::HTTP, url)
  325. assert_equal('http://a/b/c/g#s', url.to_s)
  326. url = @base_url.route_to('http://a/b/c/g#s')
  327. assert_kind_of(URI::Generic, url)
  328. assert_equal('g#s', url.to_s)
  329. # http://a/b/c/d;p?q
  330. # g?y#s = http://a/b/c/g?y#s
  331. url = @base_url.merge('g?y#s')
  332. assert_kind_of(URI::HTTP, url)
  333. assert_equal('http://a/b/c/g?y#s', url.to_s)
  334. url = @base_url.route_to('http://a/b/c/g?y#s')
  335. assert_kind_of(URI::Generic, url)
  336. assert_equal('g?y#s', url.to_s)
  337. # http://a/b/c/d;p?q
  338. # ;x = http://a/b/c/;x
  339. url = @base_url.merge(';x')
  340. assert_kind_of(URI::HTTP, url)
  341. assert_equal('http://a/b/c/;x', url.to_s)
  342. url = @base_url.route_to('http://a/b/c/;x')
  343. assert_kind_of(URI::Generic, url)
  344. assert_equal(';x', url.to_s)
  345. # http://a/b/c/d;p?q
  346. # g;x = http://a/b/c/g;x
  347. url = @base_url.merge('g;x')
  348. assert_kind_of(URI::HTTP, url)
  349. assert_equal('http://a/b/c/g;x', url.to_s)
  350. url = @base_url.route_to('http://a/b/c/g;x')
  351. assert_kind_of(URI::Generic, url)
  352. assert_equal('g;x', url.to_s)
  353. # http://a/b/c/d;p?q
  354. # g;x?y#s = http://a/b/c/g;x?y#s
  355. url = @base_url.merge('g;x?y#s')
  356. assert_kind_of(URI::HTTP, url)
  357. assert_equal('http://a/b/c/g;x?y#s', url.to_s)
  358. url = @base_url.route_to('http://a/b/c/g;x?y#s')
  359. assert_kind_of(URI::Generic, url)
  360. assert_equal('g;x?y#s', url.to_s)
  361. # http://a/b/c/d;p?q
  362. # . = http://a/b/c/
  363. url = @base_url.merge('.')
  364. assert_kind_of(URI::HTTP, url)
  365. assert_equal('http://a/b/c/', url.to_s)
  366. url = @base_url.route_to('http://a/b/c/')
  367. assert_kind_of(URI::Generic, url)
  368. assert('.' != url.to_s) # ok
  369. assert_equal('./', url.to_s)
  370. # http://a/b/c/d;p?q
  371. # ./ = http://a/b/c/
  372. url = @base_url.merge('./')
  373. assert_kind_of(URI::HTTP, url)
  374. assert_equal('http://a/b/c/', url.to_s)
  375. url = @base_url.route_to('http://a/b/c/')
  376. assert_kind_of(URI::Generic, url)
  377. assert_equal('./', url.to_s)
  378. # http://a/b/c/d;p?q
  379. # .. = http://a/b/
  380. url = @base_url.merge('..')
  381. assert_kind_of(URI::HTTP, url)
  382. assert_equal('http://a/b/', url.to_s)
  383. url = @base_url.route_to('http://a/b/')
  384. assert_kind_of(URI::Generic, url)
  385. assert('..' != url.to_s) # ok
  386. assert_equal('../', url.to_s)
  387. # http://a/b/c/d;p?q
  388. # ../ = http://a/b/
  389. url = @base_url.merge('../')
  390. assert_kind_of(URI::HTTP, url)
  391. assert_equal('http://a/b/', url.to_s)
  392. url = @base_url.route_to('http://a/b/')
  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/b/g
  397. url = @base_url.merge('../g')
  398. assert_kind_of(URI::HTTP, url)
  399. assert_equal('http://a/b/g', url.to_s)
  400. url = @base_url.route_to('http://a/b/g')
  401. assert_kind_of(URI::Generic, url)
  402. assert_equal('../g', url.to_s)
  403. # http://a/b/c/d;p?q
  404. # ../.. = http://a/
  405. url = @base_url.merge('../..')
  406. assert_kind_of(URI::HTTP, url)
  407. assert_equal('http://a/', url.to_s)
  408. url = @base_url.route_to('http://a/')
  409. assert_kind_of(URI::Generic, url)
  410. assert('../..' != url.to_s) # ok
  411. assert_equal('../../', url.to_s)
  412. # http://a/b/c/d;p?q
  413. # ../../ = http://a/
  414. url = @base_url.merge('../../')
  415. assert_kind_of(URI::HTTP, url)
  416. assert_equal('http://a/', url.to_s)
  417. url = @base_url.route_to('http://a/')
  418. assert_kind_of(URI::Generic, url)
  419. assert_equal('../../', url.to_s)
  420. # http://a/b/c/d;p?q
  421. # ../../g = http://a/g
  422. url = @base_url.merge('../../g')
  423. assert_kind_of(URI::HTTP, url)
  424. assert_equal('http://a/g', url.to_s)
  425. url = @base_url.route_to('http://a/g')
  426. assert_kind_of(URI::Generic, url)
  427. assert_equal('../../g', url.to_s)
  428. # http://a/b/c/d;p?q
  429. # <> = (current document)
  430. url = @base_url.merge('')
  431. assert_kind_of(URI::HTTP, url)
  432. assert_equal('http://a/b/c/d;p?q', url.to_s)
  433. url = @base_url.route_to('http://a/b/c/d;p?q')
  434. assert_kind_of(URI::Generic, url)
  435. assert_equal('', url.to_s)
  436. # http://a/b/c/d;p?q
  437. # /./g = http://a/g
  438. url = @base_url.merge('/./g')
  439. assert_kind_of(URI::HTTP, url)
  440. assert_equal('http://a/g', url.to_s)
  441. # url = @base_url.route_to('http://a/./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/g
  446. url = @base_url.merge('/../g')
  447. assert_kind_of(URI::HTTP, url)
  448. assert_equal('http://a/g', url.to_s)
  449. # url = @base_url.route_to('http://a/../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/b/c/g.
  454. url = @base_url.merge('g.')
  455. assert_kind_of(URI::HTTP, url)
  456. assert_equal('http://a/b/c/g.', url.to_s)
  457. url = @base_url.route_to('http://a/b/c/g.')
  458. assert_kind_of(URI::Generic, url)
  459. assert_equal('g.', url.to_s)
  460. # http://a/b/c/d;p?q
  461. # .g = http://a/b/c/.g
  462. url = @base_url.merge('.g')
  463. assert_kind_of(URI::HTTP, url)
  464. assert_equal('http://a/b/c/.g', url.to_s)
  465. url = @base_url.route_to('http://a/b/c/.g')
  466. assert_kind_of(URI::Generic, url)
  467. assert_equal('.g', url.to_s)
  468. # http://a/b/c/d;p?q
  469. # g.. = http://a/b/c/g..
  470. url = @base_url.merge('g..')
  471. assert_kind_of(URI::HTTP, url)
  472. assert_equal('http://a/b/c/g..', url.to_s)
  473. url = @base_url.route_to('http://a/b/c/g..')
  474. assert_kind_of(URI::Generic, url)
  475. assert_equal('g..', url.to_s)
  476. # http://a/b/c/d;p?q
  477. # ..g = http://a/b/c/..g
  478. url = @base_url.merge('..g')
  479. assert_kind_of(URI::HTTP, url)
  480. assert_equal('http://a/b/c/..g', url.to_s)
  481. url = @base_url.route_to('http://a/b/c/..g')
  482. assert_kind_of(URI::Generic, url)
  483. assert_equal('..g', url.to_s)
  484. # http://a/b/c/d;p?q
  485. # ../../../g = http://a/g
  486. url = @base_url.merge('../../../g')
  487. assert_kind_of(URI::HTTP, url)
  488. assert_equal('http://a/g', url.to_s)
  489. url = @base_url.route_to('http://a/g')
  490. assert_kind_of(URI::Generic, url)
  491. assert('../../../g' != url.to_s) # ok? yes, it confuses you
  492. assert_equal('../../g', url.to_s) # and it is clearly
  493. # http://a/b/c/d;p?q
  494. # ../../../../g = http://a/g
  495. url = @base_url.merge('../../../../g')
  496. assert_kind_of(URI::HTTP, url)
  497. assert_equal('http://a/g', url.to_s)
  498. url = @base_url.route_to('http://a/g')
  499. assert_kind_of(URI::Generic, url)
  500. assert('../../../../g' != url.to_s) # ok? yes, it confuses you
  501. assert_equal('../../g', url.to_s) # and it is clearly
  502. # http://a/b/c/d;p?q
  503. # ./../g = http://a/b/g
  504. url = @base_url.merge('./../g')
  505. assert_kind_of(URI::HTTP, url)
  506. assert_equal('http://a/b/g', url.to_s)
  507. url = @base_url.route_to('http://a/b/g')
  508. assert_kind_of(URI::Generic, url)
  509. assert('./../g' != url.to_s) # ok
  510. assert_equal('../g', url.to_s)
  511. # http://a/b/c/d;p?q
  512. # ./g/. = http://a/b/c/g/
  513. url = @base_url.merge('./g/.')
  514. assert_kind_of(URI::HTTP, url)
  515. assert_equal('http://a/b/c/g/', url.to_s)
  516. url = @base_url.route_to('http://a/b/c/g/')
  517. assert_kind_of(URI::Generic, url)
  518. assert('./g/.' != url.to_s) # ok
  519. assert_equal('g/', url.to_s)
  520. # http://a/b/c/d;p?q
  521. # g/./h = http://a/b/c/g/h
  522. url = @base_url.merge('g/./h')
  523. assert_kind_of(URI::HTTP, url)
  524. assert_equal('http://a/b/c/g/h', url.to_s)
  525. url = @base_url.route_to('http://a/b/c/g/h')
  526. assert_kind_of(URI::Generic, url)
  527. assert('g/./h' != url.to_s) # ok
  528. assert_equal('g/h', url.to_s)
  529. # http://a/b/c/d;p?q
  530. # g/../h = http://a/b/c/h
  531. url = @base_url.merge('g/../h')
  532. assert_kind_of(URI::HTTP, url)
  533. assert_equal('http://a/b/c/h', url.to_s)
  534. url = @base_url.route_to('http://a/b/c/h')
  535. assert_kind_of(URI::Generic, url)
  536. assert('g/../h' != url.to_s) # ok
  537. assert_equal('h', url.to_s)
  538. # http://a/b/c/d;p?q
  539. # g;x=1/./y = http://a/b/c/g;x=1/y
  540. url = @base_url.merge('g;x=1/./y')
  541. assert_kind_of(URI::HTTP, url)
  542. assert_equal('http://a/b/c/g;x=1/y', url.to_s)
  543. url = @base_url.route_to('http://a/b/c/g;x=1/y')
  544. assert_kind_of(URI::Generic, url)
  545. assert('g;x=1/./y' != url.to_s) # ok
  546. assert_equal('g;x=1/y', url.to_s)
  547. # http://a/b/c/d;p?q
  548. # g;x=1/../y = http://a/b/c/y
  549. url = @base_url.merge('g;x=1/../y')
  550. assert_kind_of(URI::HTTP, url)
  551. assert_equal('http://a/b/c/y', url.to_s)
  552. url = @base_url.route_to('http://a/b/c/y')
  553. assert_kind_of(URI::Generic, url)
  554. assert('g;x=1/../y' != url.to_s) # ok
  555. assert_equal('y', url.to_s)
  556. # http://a/b/c/d;p?q
  557. # g?y/./x = http://a/b/c/g?y/./x
  558. url = @base_url.merge('g?y/./x')
  559. assert_kind_of(URI::HTTP, url)
  560. assert_equal('http://a/b/c/g?y/./x', url.to_s)
  561. url = @base_url.route_to('http://a/b/c/g?y/./x')
  562. assert_kind_of(URI::Generic, url)
  563. assert_equal('g?y/./x', url.to_s)
  564. # http://a/b/c/d;p?q
  565. # g?y/../x = http://a/b/c/g?y/../x
  566. url = @base_url.merge('g?y/../x')
  567. assert_kind_of(URI::HTTP, url)
  568. assert_equal('http://a/b/c/g?y/../x', url.to_s)
  569. url = @base_url.route_to('http://a/b/c/g?y/../x')
  570. assert_kind_of(URI::Generic, url)
  571. assert_equal('g?y/../x', url.to_s)
  572. # http://a/b/c/d;p?q
  573. # g#s/./x = http://a/b/c/g#s/./x
  574. url = @base_url.merge('g#s/./x')
  575. assert_kind_of(URI::HTTP, url)
  576. assert_equal('http://a/b/c/g#s/./x', url.to_s)
  577. url = @base_url.route_to('http://a/b/c/g#s/./x')
  578. assert_kind_of(URI::Generic, url)
  579. assert_equal('g#s/./x', url.to_s)
  580. # http://a/b/c/d;p?q
  581. # g#s/../x = http://a/b/c/g#s/../x
  582. url = @base_url.merge('g#s/../x')
  583. assert_kind_of(URI::HTTP, url)
  584. assert_equal('http://a/b/c/g#s/../x', url.to_s)
  585. url = @base_url.route_to('http://a/b/c/g#s/../x')
  586. assert_kind_of(URI::Generic, url)
  587. assert_equal('g#s/../x', url.to_s)
  588. # http://a/b/c/d;p?q
  589. # http:g = http:g ; for validating parsers
  590. # | http://a/b/c/g ; for backwards compatibility
  591. url = @base_url.merge('http:g')
  592. assert_kind_of(URI::HTTP, url)
  593. assert_equal('http:g', url.to_s)
  594. url = @base_url.route_to('http:g')
  595. assert_kind_of(URI::Generic, url)
  596. assert_equal('http:g', url.to_s)
  597. end
  598. def test_join
  599. assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar'))
  600. assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar'))
  601. assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/'))
  602. assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz'))
  603. assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz'))
  604. assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/'))
  605. assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz'))
  606. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge'))
  607. assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz'))
  608. assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
  609. assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
  610. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))
  611. assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))
  612. assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))
  613. assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))
  614. end
  615. # ruby-dev:16728
  616. def test_set_component
  617. uri = URI.parse('http://foo:bar@baz')
  618. assert_equal('oof', uri.user = 'oof')
  619. assert_equal('http://oof:bar@baz', uri.to_s)
  620. assert_equal('rab', uri.password = 'rab')
  621. assert_equal('http://oof:rab@baz', uri.to_s)
  622. assert_equal('foo', uri.userinfo = 'foo')
  623. assert_equal('http://foo:rab@baz', uri.to_s)
  624. assert_equal(['foo', 'bar'], uri.userinfo = ['foo', 'bar'])
  625. assert_equal('http://foo:bar@baz', uri.to_s)
  626. assert_equal(['foo'], uri.userinfo = ['foo'])
  627. assert_equal('http://foo:bar@baz', uri.to_s)
  628. assert_equal('zab', uri.host = 'zab')
  629. assert_equal('http://foo:bar@zab', uri.to_s)
  630. uri.port = ""
  631. assert_nil(uri.port)
  632. uri.port = "80"
  633. assert_equal(80, uri.port)
  634. uri.port = "080"
  635. assert_equal(80, uri.port)
  636. uri.port = " 080 "
  637. assert_equal(80, uri.port)
  638. assert_equal(8080, uri.port = 8080)
  639. assert_equal('http://foo:bar@zab:8080', uri.to_s)
  640. assert_equal('/', uri.path = '/')
  641. assert_equal('http://foo:bar@zab:8080/', uri.to_s)
  642. assert_equal('a=1', uri.query = 'a=1')
  643. assert_equal('http://foo:bar@zab:8080/?a=1', uri.to_s)
  644. assert_equal('b123', uri.fragment = 'b123')
  645. assert_equal('http://foo:bar@zab:8080/?a=1#b123', uri.to_s)
  646. assert_equal('a[]=1', uri.query = 'a[]=1')
  647. assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)
  648. uri = URI.parse('http://foo:bar@zab:8080/?a[]=1#b123')
  649. assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)
  650. uri = URI.parse('http://example.com')
  651. assert_raise(URI::InvalidURIError) { uri.password = 'bar' }
  652. assert_equal("foo\nbar", uri.query = "foo\nbar")
  653. uri.userinfo = 'foo:bar'
  654. assert_equal('http://foo:bar@example.com?foobar', uri.to_s)
  655. assert_raise(URI::InvalidURIError) { uri.registry = 'bar' }
  656. assert_raise(URI::InvalidURIError) { uri.opaque = 'bar' }
  657. uri = URI.parse('mailto:foo@example.com')
  658. assert_raise(URI::InvalidURIError) { uri.user = 'bar' }
  659. assert_raise(URI::InvalidURIError) { uri.password = 'bar' }
  660. assert_raise(URI::InvalidURIError) { uri.userinfo = ['bar', 'baz'] }
  661. assert_raise(URI::InvalidURIError) { uri.host = 'bar' }
  662. assert_raise(URI::InvalidURIError) { uri.port = 'bar' }
  663. assert_raise(URI::InvalidURIError) { uri.path = 'bar' }
  664. assert_raise(URI::InvalidURIError) { uri.query = 'bar' }
  665. uri = URI.parse('foo:bar')
  666. assert_raise(URI::InvalidComponentError) { uri.opaque = '/baz' }
  667. uri.opaque = 'xyzzy'
  668. assert_equal('foo:xyzzy', uri.to_s)
  669. end
  670. def test_bad_password_component
  671. uri = URI.parse('http://foo:bar@baz')
  672. password = 'foo@bar'
  673. e = assert_raise(URI::InvalidComponentError) do
  674. uri.password = password
  675. end
  676. refute_match Regexp.new(password), e.message
  677. end
  678. def test_set_scheme
  679. uri = URI.parse 'HTTP://example'
  680. assert_equal 'http://example', uri.to_s
  681. end
  682. def test_hierarchical
  683. hierarchical = URI.parse('http://a.b.c/example')
  684. opaque = URI.parse('mailto:mduerst@ifi.unizh.ch')
  685. assert hierarchical.hierarchical?
  686. refute opaque.hierarchical?
  687. end
  688. def test_absolute
  689. abs_uri = URI.parse('http://a.b.c/')
  690. not_abs = URI.parse('a.b.c')
  691. refute not_abs.absolute?
  692. assert abs_uri.absolute
  693. assert abs_uri.absolute?
  694. end
  695. def test_ipv6
  696. assert_equal("[::1]", URI("http://[::1]/bar/baz").host)
  697. assert_equal("::1", URI("http://[::1]/bar/baz").hostname)
  698. u = URI("http://foo/bar")
  699. assert_equal("http://foo/bar", u.to_s)
  700. u.hostname = "::1"
  701. assert_equal("http://[::1]/bar", u.to_s)
  702. end
  703. def test_build
  704. u = URI::Generic.build(['http', nil, 'example.com', 80, nil, '/foo', nil, nil, nil])
  705. assert_equal('http://example.com:80/foo', u.to_s)
  706. assert_equal(Encoding::UTF_8, u.to_s.encoding)
  707. u = URI::Generic.build(:port => "5432")
  708. assert_equal(":5432", u.to_s)
  709. assert_equal(5432, u.port)
  710. u = URI::Generic.build(:scheme => "http", :host => "::1", :path => "/bar/baz")
  711. assert_equal("http://[::1]/bar/baz", u.to_s)
  712. assert_equal("[::1]", u.host)
  713. assert_equal("::1", u.hostname)
  714. u = URI::Generic.build(:scheme => "http", :host => "[::1]", :path => "/bar/baz")
  715. assert_equal("http://[::1]/bar/baz", u.to_s)
  716. assert_equal("[::1]", u.host)
  717. assert_equal("::1", u.hostname)
  718. end
  719. def test_build2
  720. u = URI::Generic.build2(path: "/foo bar/baz")
  721. assert_equal('/foo%20bar/baz', u.to_s)
  722. u = URI::Generic.build2(['http', nil, 'example.com', 80, nil, '/foo bar' , nil, nil, nil])
  723. assert_equal('http://example.com:80/foo%20bar', u.to_s)
  724. end
  725. # 192.0.2.0/24 is TEST-NET. [RFC3330]
  726. def test_find_proxy_bad_uri
  727. assert_raise(URI::BadURIError){ URI("foo").find_proxy }
  728. end
  729. def test_find_proxy_no_env
  730. with_proxy_env({}) {|env|
  731. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  732. assert_nil(URI("ftp://192.0.2.1/").find_proxy(env))
  733. }
  734. end
  735. def test_find_proxy
  736. with_proxy_env('http_proxy'=>'http://127.0.0.1:8080') {|env|
  737. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
  738. assert_nil(URI("ftp://192.0.2.1/").find_proxy(env))
  739. }
  740. with_proxy_env('ftp_proxy'=>'http://127.0.0.1:8080') {|env|
  741. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  742. assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy(env))
  743. }
  744. end
  745. def test_find_proxy_get
  746. with_proxy_env('REQUEST_METHOD'=>'GET') {|env|
  747. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  748. }
  749. with_proxy_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {|env|
  750. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
  751. }
  752. end
  753. def test_find_proxy_no_proxy
  754. getaddress = IPSocket.method(:getaddress)
  755. example_address = nil
  756. IPSocket.singleton_class.class_eval do
  757. undef getaddress
  758. define_method(:getaddress) do |host|
  759. case host
  760. when "example.org", "www.example.org"
  761. example_address
  762. when /\A\d+(?:\.\d+){3}\z/
  763. host
  764. else
  765. raise host
  766. end
  767. end
  768. end
  769. with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {|env|
  770. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
  771. assert_nil(URI("http://192.0.2.2/").find_proxy(env))
  772. example_address = "192.0.2.1"
  773. assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
  774. example_address = "192.0.2.2"
  775. assert_nil(URI.parse("http://example.org").find_proxy(env))
  776. }
  777. with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'example.org') {|env|
  778. assert_nil(URI("http://example.org/").find_proxy(env))
  779. assert_nil(URI("http://www.example.org/").find_proxy(env))
  780. }
  781. with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'.example.org') {|env|
  782. assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env))
  783. assert_nil(URI("http://www.example.org/").find_proxy(env))
  784. }
  785. ensure
  786. IPSocket.singleton_class.class_eval do
  787. undef getaddress
  788. define_method(:getaddress, getaddress)
  789. end
  790. end
  791. def test_find_proxy_no_proxy_cidr
  792. with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.0/24') {|env|
  793. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.1.1/").find_proxy(env))
  794. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  795. assert_nil(URI("http://192.0.2.2/").find_proxy(env))
  796. }
  797. end
  798. def test_find_proxy_bad_value
  799. with_proxy_env('http_proxy'=>'') {|env|
  800. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  801. assert_nil(URI("ftp://192.0.2.1/").find_proxy(env))
  802. }
  803. with_proxy_env('ftp_proxy'=>'') {|env|
  804. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  805. assert_nil(URI("ftp://192.0.2.1/").find_proxy(env))
  806. }
  807. end
  808. def test_find_proxy_case_sensitive_env
  809. with_proxy_env_case_sensitive('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {|env|
  810. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
  811. }
  812. with_proxy_env_case_sensitive('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {|env|
  813. assert_nil(URI("http://192.0.2.1/").find_proxy(env))
  814. }
  815. with_proxy_env_case_sensitive('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {|env|
  816. assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
  817. }
  818. end
  819. def test_use_proxy_p
  820. [
  821. ['example.com', nil, 80, '', true],
  822. ['example.com', nil, 80, 'example.com:80', false],
  823. ['example.com', nil, 80, 'example.org,example.com:80,example.net', false],
  824. ['foo.example.com', nil, 80, 'example.com', false],
  825. ['foo.example.com', nil, 80, '.example.com', false],
  826. ['example.com', nil, 80, '.example.com', true],
  827. ['xample.com', nil, 80, '.example.com', true],
  828. ['fooexample.com', nil, 80, '.example.com', true],
  829. ['foo.example.com', nil, 80, 'example.com:80', false],
  830. ['foo.eXample.com', nil, 80, 'example.com:80', false],
  831. ['foo.example.com', nil, 80, 'eXample.com:80', false],
  832. ['foo.example.com', nil, 80, 'example.com:443', true],
  833. ['127.0.0.1', '127.0.0.1', 80, '10.224.0.0/22', true],
  834. ['10.224.1.1', '10.224.1.1', 80, '10.224.1.1', false],
  835. ['10.224.1.1', '10.224.1.1', 80, '10.224.0.0/22', false],
  836. ].each do |hostname, addr, port, no_proxy, expected|
  837. assert_equal expected, URI::Generic.use_proxy?(hostname, addr, port, no_proxy),
  838. "use_proxy?('#{hostname}', '#{addr}', #{port}, '#{no_proxy}')"
  839. end
  840. end
  841. class CaseInsensitiveEnv
  842. def initialize(h={})
  843. @h = {}
  844. h.each {|k, v| self[k] = v }
  845. end
  846. def []=(k, v)
  847. if v
  848. @h[k.downcase] = [k, v.to_s]
  849. else
  850. @h.delete [k.downcase]
  851. end
  852. v
  853. end
  854. def [](k)
  855. k = k.downcase
  856. @h.has_key?(k) ? @h[k][1] : nil
  857. end
  858. def length
  859. @h.length
  860. end
  861. def include?(k)
  862. @h.include? k.downcase
  863. end
  864. def shift
  865. return nil if @h.empty?
  866. _kd, (k, v) = @h.shift
  867. [k, v]
  868. end
  869. def each
  870. @h.each {|kd, (k, v)| yield [k, v] }
  871. end
  872. def reject
  873. ret = CaseInsensitiveEnv.new
  874. self.each {|k, v|
  875. ret[k] = v unless yield [k, v]
  876. }
  877. ret
  878. end
  879. def to_hash
  880. ret = {}
  881. self.each {|k, v|
  882. ret[k] = v
  883. }
  884. ret
  885. end
  886. end
  887. def with_proxy_real_env(h)
  888. h = h.dup
  889. ['http', 'https', 'ftp'].each do |scheme|
  890. name = "#{scheme}_proxy"
  891. h[name] ||= nil
  892. h["CGI_#{name.upcase}"] ||= nil
  893. end
  894. begin
  895. old = {}
  896. h.each_key {|k| old[k] = ENV[k] }
  897. h.each {|k, v| ENV[k] = v }
  898. yield ENV
  899. ensure
  900. h.each_key {|k| ENV[k] = old[k] }
  901. end
  902. h.reject! {|k, v| v.nil? }
  903. end
  904. def with_proxy_env(h, &b)
  905. with_proxy_real_env(h, &b)
  906. h = h.reject {|k, v| v.nil? }
  907. yield h
  908. yield CaseInsensitiveEnv.new(h)
  909. end
  910. def with_proxy_env_case_sensitive(h, &b)
  911. with_proxy_real_env(h, &b) unless RUBY_PLATFORM =~ /mswin|mingw/
  912. h = h.reject {|k, v| v.nil? }
  913. yield h
  914. end
  915. end