1
0

url_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsUrlTest < MiniTest::Spec
  4. URL = Docs::URL
  5. describe ".new" do
  6. it "works with no arguments" do
  7. assert_instance_of URL, URL.new
  8. end
  9. it "works with a Hash of components" do
  10. assert_equal '/path', URL.new(path: '/path').to_s
  11. end
  12. it "raises an error with an invalid component" do
  13. assert_raises(ArgumentError) { URL.new test: nil }
  14. end
  15. end
  16. describe ".parse" do
  17. it "returns a URL when given a string" do
  18. assert_instance_of URL, URL.parse('http://example.com')
  19. end
  20. it "returns the same URL when given a URL" do
  21. url = URL.new
  22. assert_same url, URL.parse(url)
  23. end
  24. end
  25. describe "#join" do
  26. it "joins urls" do
  27. url = URL.parse 'http://example.com/path/to/'
  28. assert_equal 'http://example.com/path/to/file', url.join('..', 'to/file').to_s
  29. end
  30. end
  31. describe "#merge!" do
  32. it "works with a Hash of components" do
  33. assert_equal '/path', URL.new.merge!(path: '/path').to_s
  34. end
  35. it "raises an error with an invalid component" do
  36. assert_raises(ArgumentError) { URL.new.merge! test: nil }
  37. end
  38. end
  39. describe "#origin" do
  40. it "returns 'http://example.com' when the URL is 'http://example.com/path?#'" do
  41. assert_equal 'http://example.com', URL.parse('http://example.com/path?#').origin
  42. end
  43. it "returns 'http://example.com' when the URL is 'HTTP://EXAMPLE.COM'" do
  44. assert_equal 'http://example.com', URL.parse('HTTP://EXAMPLE.COM').origin
  45. end
  46. it "returns 'http://example.com:8080' when the URL is 'http://example.com:8080'" do
  47. assert_equal 'http://example.com:8080', URL.parse('http://example.com:8080').origin
  48. end
  49. it "returns nil when the URL is 'example.com'" do
  50. assert_nil URL.parse('example.com').origin
  51. end
  52. it "returns nil when the URL is 'mailto:test@example.com'" do
  53. assert_nil URL.parse('mailto:test@example.com').origin
  54. end
  55. end
  56. describe "#normalized_path" do
  57. it "returns '/' when the URL is ''" do
  58. assert_equal '/', URL.parse('').normalized_path
  59. end
  60. it "returns '/path' when the URL is '/path'" do
  61. assert_equal '/path', URL.parse('/path').normalized_path
  62. end
  63. end
  64. describe "#subpath_to" do
  65. context "when the URL is '/'" do
  66. let :url do
  67. URL.parse '/'
  68. end
  69. it "returns nil with ''" do
  70. assert_nil url.subpath_to('')
  71. end
  72. it "returns '' with '/'" do
  73. assert_equal '', url.subpath_to('/')
  74. end
  75. it "returns 'path' with '/path'" do
  76. assert_equal 'path', url.subpath_to('/path')
  77. end
  78. it "returns nil with 'path'" do
  79. assert_nil url.subpath_to('path')
  80. end
  81. it "returns nil with 'http://example.com/'" do
  82. assert_nil url.subpath_to('http://example.com/')
  83. end
  84. end
  85. context "when the URL is '/path/to'" do
  86. let :url do
  87. URL.parse '/path/to'
  88. end
  89. it "returns nil with '/path/'" do
  90. assert_nil url.subpath_to('/path/')
  91. end
  92. it "returns '' with '/path/to'" do
  93. assert_equal '', url.subpath_to('/path/to')
  94. end
  95. it "returns '/file' with '/path/to/file'" do
  96. assert_equal '/file', url.subpath_to('/path/to/file')
  97. end
  98. it "returns nil with 'path/to/file'" do
  99. assert_nil url.subpath_to('path/to/file')
  100. end
  101. it "returns nil with '/path/tofile'" do
  102. assert_nil url.subpath_to('/path/tofile')
  103. end
  104. it "returns nil with '/PATH/to/file'" do
  105. assert_nil url.subpath_to('/PATH/to/file')
  106. end
  107. context "and :ignore_case is true" do
  108. it "returns '/file' with '/PATH/to/file'" do
  109. assert_equal '/file', url.subpath_to('/PATH/to/file', ignore_case: true)
  110. end
  111. end
  112. end
  113. context "when the URL is '/path/to/'" do
  114. let :url do
  115. URL.parse '/path/to/'
  116. end
  117. it "returns nil with '/path/to'" do
  118. assert_nil url.subpath_to('/path/to')
  119. end
  120. it "returns 'file' with '/path/to/file'" do
  121. assert_equal 'file', url.subpath_to('/path/to/file')
  122. end
  123. end
  124. context "when the URL is 'path/to'" do
  125. let :url do
  126. URL.parse 'path/to'
  127. end
  128. it "returns nil with '/path/to'" do
  129. assert_nil url.subpath_to('/path/to')
  130. end
  131. it "returns '/file' with 'path/to/file'" do
  132. assert_equal '/file', url.subpath_to('path/to/file')
  133. end
  134. end
  135. context "when the URL is 'http://example.com'" do
  136. let :url do
  137. URL.parse 'http://example.com'
  138. end
  139. it "returns '' with 'HTTP://EXAMPLE.COM'" do
  140. assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM')
  141. end
  142. it "returns '/path' with 'http://example.com/path?query#frag'" do
  143. assert_equal '/path', url.subpath_to('http://example.com/path?query#frag')
  144. end
  145. it "returns nil with 'https://example.com/'" do
  146. assert_nil url.subpath_to('https://example.com/')
  147. end
  148. it "returns nil with 'http://not.example.com/'" do
  149. assert_nil url.subpath_to('http://not.example.com/')
  150. end
  151. end
  152. context "when the URL is 'http://example.com/'" do
  153. let :url do
  154. URL.parse 'http://example.com/'
  155. end
  156. it "returns nil with 'http://example.com'" do
  157. assert_equal nil, url.subpath_to('http://example.com')
  158. end
  159. end
  160. context "when the URL is 'http://example.com/path/to'" do
  161. let :url do
  162. URL.parse 'http://example.com/path/to'
  163. end
  164. it "returns '/file' with 'http://example.com/path/to/file'" do
  165. assert_equal '/file', url.subpath_to('http://example.com/path/to/file')
  166. end
  167. it "returns nil with 'http://example.com/path/tofile'" do
  168. assert_nil url.subpath_to('http://example.com/path/tofile')
  169. end
  170. it "returns nil with '/path/to/file'" do
  171. assert_nil url.subpath_to('/path/tofile')
  172. end
  173. end
  174. end
  175. describe "#subpath_from" do
  176. let :url do
  177. URL.new
  178. end
  179. before do
  180. any_instance_of URL do |instance|
  181. stub(instance).subpath_to
  182. end
  183. end
  184. it "returns the given url's #subpath_to to self" do
  185. any_instance_of URL do |instance|
  186. stub(instance).subpath_to(url, nil) { 'subpath' }
  187. end
  188. assert_equal 'subpath', url.subpath_from('url')
  189. end
  190. it "calls #subpath_to with the given options" do
  191. any_instance_of URL do |instance|
  192. stub(instance).subpath_to(url, 'options') { 'subpath' }
  193. end
  194. assert_equal 'subpath', url.subpath_from('url', 'options')
  195. end
  196. end
  197. describe "#contains?" do
  198. let :url do
  199. URL.new
  200. end
  201. before do
  202. stub(url).subpath_to
  203. end
  204. it "calls #subpath_to with the given url" do
  205. mock(url).subpath_to('url', nil)
  206. url.contains?('url')
  207. end
  208. it "calls #subpath_to with the given options" do
  209. mock(url).subpath_to('url', 'options')
  210. url.contains?('url', 'options')
  211. end
  212. it "returns true when the #subpath_to the given url is a string" do
  213. stub(url).subpath_to { '' }
  214. assert url.contains?('url')
  215. end
  216. it "returns true when the #subpath_to the given url is nil" do
  217. stub(url).subpath_to { nil }
  218. refute url.contains?('url')
  219. end
  220. end
  221. describe "#relative_path_to" do
  222. context "when the URL is '/'" do
  223. let :url do
  224. URL.parse '/'
  225. end
  226. it "returns '.' with '/'" do
  227. assert_equal '.', url.relative_path_to('/')
  228. end
  229. it "returns 'file' with '/file'" do
  230. assert_equal 'file', url.relative_path_to('/file')
  231. end
  232. it "returns 'file/' with '/file/'" do
  233. assert_equal 'file/', url.relative_path_to('/file/')
  234. end
  235. it "raises an error with 'file'" do
  236. assert_raises ArgumentError do
  237. url.relative_path_to 'file'
  238. end
  239. end
  240. end
  241. context "when the URL is '/path/to'" do
  242. let :url do
  243. URL.parse '/path/to'
  244. end
  245. it "returns '../' with '/'" do
  246. assert_equal '../', url.relative_path_to('/')
  247. end
  248. it "returns '../path' with '/path'" do
  249. assert_equal '../path', url.relative_path_to('/path')
  250. end
  251. it "returns '.' with '/path/'" do
  252. assert_equal '.', url.relative_path_to('/path/')
  253. end
  254. it "returns 'to' with '/path/to'" do
  255. assert_equal 'to', url.relative_path_to('/path/to')
  256. end
  257. it "returns '../PATH/to' with '/PATH/to'" do
  258. assert_equal '../PATH/to', url.relative_path_to('/PATH/to')
  259. end
  260. it "returns 'to/' with '/path/to/'" do
  261. assert_equal 'to/', url.relative_path_to('/path/to/')
  262. end
  263. it "returns 'to/file' with '/path/to/file'" do
  264. assert_equal 'to/file', url.relative_path_to('/path/to/file')
  265. end
  266. it "returns 'to/file/' with '/path/to/file/'" do
  267. assert_equal 'to/file/', url.relative_path_to('/path/to/file/')
  268. end
  269. end
  270. context "when the URL is '/path/to/'" do
  271. let :url do
  272. URL.parse '/path/to/'
  273. end
  274. it "returns '../../' with ''" do
  275. assert_equal '../../', url.relative_path_to('')
  276. end
  277. it "returns '../../' with '/'" do
  278. assert_equal '../../', url.relative_path_to('/')
  279. end
  280. it "returns '../../path' with '/path'" do
  281. assert_equal '../../path', url.relative_path_to('/path')
  282. end
  283. it "returns '../' with '/path/'" do
  284. assert_equal '../', url.relative_path_to('/path/')
  285. end
  286. it "returns '../to' with '/path/to'" do
  287. assert_equal '../to', url.relative_path_to('/path/to')
  288. end
  289. it "returns '.' with '/path/to/'" do
  290. assert_equal '.', url.relative_path_to('/path/to/')
  291. end
  292. it "returns 'file' with '/path/to/file'" do
  293. assert_equal 'file', url.relative_path_to('/path/to/file')
  294. end
  295. it "returns 'file/' with '/path/to/file/'" do
  296. assert_equal 'file/', url.relative_path_to('/path/to/file/')
  297. end
  298. end
  299. context "when the URL is 'http://example.com'" do
  300. let :url do
  301. URL.parse 'http://example.com'
  302. end
  303. it "returns '.' with 'http://example.com'" do
  304. assert_equal '.', url.relative_path_to('http://example.com')
  305. end
  306. it "returns '.' with 'http://example.com/'" do
  307. assert_equal '.', url.relative_path_to('http://example.com/')
  308. end
  309. it "returns 'file' with 'http://example.com/file?query#frag'" do
  310. assert_equal 'file', url.relative_path_to('http://example.com/file?query#frag')
  311. end
  312. it "returns 'some:file' with 'http://example.com/some:file'" do
  313. assert_equal 'some:file', url.relative_path_to('http://example.com/some:file')
  314. end
  315. it "returns 'some:file' with 'http://example.com/some:file?query#frag'" do
  316. assert_equal 'some:file', url.relative_path_to('http://example.com/some:file?query#frag')
  317. end
  318. it "returns nil with '/file'" do
  319. assert_nil url.relative_path_to('/file')
  320. end
  321. it "returns nil with 'file'" do
  322. assert_nil url.relative_path_to('file')
  323. end
  324. it "returns nil with 'https://example.com'" do
  325. assert_nil url.relative_path_to('https://example.com')
  326. end
  327. it "returns nil with 'http://not.example.com/file'" do
  328. assert_nil url.relative_path_to('http://not.example.com/file')
  329. end
  330. end
  331. context "when the URL is 'http://example.com/'" do
  332. let :url do
  333. URL.parse 'http://example.com/'
  334. end
  335. it "returns '.' with 'http://example.com'" do
  336. assert_equal '.', url.relative_path_to('http://example.com')
  337. end
  338. end
  339. end
  340. describe "#relative_path_from" do
  341. let :url do
  342. URL.new
  343. end
  344. it "returns the given url's #relative_path_to to self" do
  345. any_instance_of URL do |instance|
  346. stub(instance).relative_path_to(url) { 'path' }
  347. end
  348. assert_equal 'path', url.relative_path_from('url')
  349. end
  350. end
  351. end