url_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 relative" do
  66. let :url do
  67. URL.parse '/'
  68. end
  69. it "raises an error with a relative url" do
  70. assert_raises(ArgumentError) { url.subpath_to '/' }
  71. end
  72. it "raises an error with an absolute url" do
  73. assert_raises(ArgumentError) { url.subpath_to 'http://example.com' }
  74. end
  75. end
  76. context "when the URL is 'http://example.com'" do
  77. let :url do
  78. URL.parse 'http://example.com'
  79. end
  80. it "raises an error with a relative url" do
  81. assert_raises(ArgumentError) { url.subpath_to '/' }
  82. end
  83. it "returns '' with 'http://example.com'" do
  84. assert_equal '', url.subpath_to('http://example.com')
  85. end
  86. it "returns '' with 'HTTP://EXAMPLE.COM'" do
  87. assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM')
  88. end
  89. it "returns '/' with 'http://example.com/'" do
  90. assert_equal '/', url.subpath_to('http://example.com/')
  91. end
  92. it "returns '/path' with 'http://example.com/path'" do
  93. assert_equal '/path', url.subpath_to('http://example.com/path')
  94. end
  95. it "returns '/' with 'http://example.com/?query'" do
  96. assert_equal '/', url.subpath_to('http://example.com/?query')
  97. end
  98. it "returns '/' with 'http://example.com/#frag'" do
  99. assert_equal '/', url.subpath_to('http://example.com/#frag')
  100. end
  101. it "returns nil with 'https://example.com/'" do
  102. assert_nil url.subpath_to('https://example.com/')
  103. end
  104. it "returns nil with 'http://not.example.com/'" do
  105. assert_nil url.subpath_to('http://not.example.com/')
  106. end
  107. end
  108. context "when the URL is 'http://example.com/'" do
  109. let :url do
  110. URL.parse 'http://example.com/'
  111. end
  112. it "returns nil with 'http://example.com'" do
  113. assert_equal nil, url.subpath_to('http://example.com')
  114. end
  115. it "returns '' with 'http://example.com/'" do
  116. assert_equal '', url.subpath_to('http://example.com/')
  117. end
  118. end
  119. context "when the URL is 'http://example.com/path/to'" do
  120. let :url do
  121. URL.parse 'http://example.com/path/to'
  122. end
  123. it "returns nil with 'http://example.com'" do
  124. assert_nil url.subpath_to('http://example.com')
  125. end
  126. it "returns nil with 'http://example.com/'" do
  127. assert_nil url.subpath_to('http://example.com/')
  128. end
  129. it "returns nil with 'http://example.com/path/'" do
  130. assert_nil url.subpath_to('http://example.com/path/')
  131. end
  132. it "returns '' with 'http://example.com/path/to'" do
  133. assert_equal '', url.subpath_to('http://example.com/path/to')
  134. end
  135. it "returns '/file' with 'http://example.com/path/to/file'" do
  136. assert_equal '/file', url.subpath_to('http://example.com/path/to/file')
  137. end
  138. it "returns nil with 'http://example.com/path/tofile'" do
  139. assert_nil url.subpath_to('http://example.com/path/tofile')
  140. end
  141. it "returns nil with 'http://example.com/PATH/to/file'" do
  142. assert_nil url.subpath_to('http://example.com/PATH/to/file')
  143. end
  144. context "and :ignore_case is true" do
  145. it "returns '/file' with 'http://example.com/PATH/to/file'" do
  146. assert_equal '/file', url.subpath_to('http://example.com/PATH/to/file', ignore_case: true)
  147. end
  148. end
  149. end
  150. context "when the URL is 'http://example.com/path/to/'" do
  151. let :url do
  152. URL.parse 'http://example.com/path/to/'
  153. end
  154. it "returns nil with 'http://example.com/path/to'" do
  155. assert_nil url.subpath_to('http://example.com/path/to')
  156. end
  157. it "returns '' with 'http://example.com/path/to/'" do
  158. assert_equal '', url.subpath_to('http://example.com/path/to/')
  159. end
  160. it "returns 'file' with 'http://example.com/path/to/file'" do
  161. assert_equal 'file', url.subpath_to('http://example.com/path/to/file')
  162. end
  163. end
  164. end
  165. describe "#subpath_from" do
  166. let :url do
  167. URL.new
  168. end
  169. before do
  170. any_instance_of URL do |instance|
  171. stub(instance).subpath_to
  172. end
  173. end
  174. it "returns the given url's #subpath_to to self" do
  175. any_instance_of URL do |instance|
  176. stub(instance).subpath_to(url, nil) { 'subpath' }
  177. end
  178. assert_equal 'subpath', url.subpath_from('url')
  179. end
  180. it "calls #subpath_to with the given options" do
  181. any_instance_of URL do |instance|
  182. stub(instance).subpath_to(url, 'options') { 'subpath' }
  183. end
  184. assert_equal 'subpath', url.subpath_from('url', 'options')
  185. end
  186. end
  187. describe "#contains?" do
  188. let :url do
  189. URL.new
  190. end
  191. before do
  192. stub(url).subpath_to
  193. end
  194. it "calls #subpath_to with the given url" do
  195. mock(url).subpath_to('url', nil)
  196. url.contains?('url')
  197. end
  198. it "calls #subpath_to with the given options" do
  199. mock(url).subpath_to('url', 'options')
  200. url.contains?('url', 'options')
  201. end
  202. it "returns true when the #subpath_to the given url is a string" do
  203. stub(url).subpath_to { '' }
  204. assert url.contains?('url')
  205. end
  206. it "returns true when the #subpath_to the given url is nil" do
  207. stub(url).subpath_to { nil }
  208. refute url.contains?('url')
  209. end
  210. end
  211. describe "#relative_path_to" do
  212. context "when the URL is relative" do
  213. let :url do
  214. URL.parse '/'
  215. end
  216. it "raises an error with a relative url" do
  217. assert_raises(ArgumentError) { url.relative_path_to '/' }
  218. end
  219. it "raises an error with an absolute url" do
  220. assert_raises(ArgumentError) { url.relative_path_to 'http://example.com' }
  221. end
  222. end
  223. context "when the URL is 'http://example.com'" do
  224. let :url do
  225. URL.parse 'http://example.com'
  226. end
  227. it "raises an error with a relative url" do
  228. assert_raises(ArgumentError) { url.relative_path_to '/' }
  229. end
  230. it "returns '.' with 'http://example.com'" do
  231. assert_equal '.', url.relative_path_to('http://example.com')
  232. end
  233. it "returns '.' with 'http://example.com/'" do
  234. assert_equal '.', url.relative_path_to('http://example.com/')
  235. end
  236. it "returns 'file' with 'http://example.com/file'" do
  237. assert_equal 'file', url.relative_path_to('http://example.com/file')
  238. end
  239. it "returns 'file/' with 'http://example.com/file/'" do
  240. assert_equal 'file/', url.relative_path_to('http://example.com/file/')
  241. end
  242. it "returns nil with 'https://example.com'" do
  243. assert_nil url.relative_path_to('https://example.com')
  244. end
  245. it "returns nil with 'http://not.example.com/file'" do
  246. assert_nil url.relative_path_to('http://not.example.com/file')
  247. end
  248. end
  249. context "when the URL is 'http://example.com/'" do
  250. let :url do
  251. URL.parse 'http://example.com/'
  252. end
  253. it "returns '.' with 'http://example.com'" do
  254. assert_equal '.', url.relative_path_to('http://example.com')
  255. end
  256. end
  257. context "when the URL is 'http://example.com/path/to'" do
  258. let :url do
  259. URL.parse 'http://example.com/path/to'
  260. end
  261. it "returns '../' with 'http://example.com'" do
  262. assert_equal '../', url.relative_path_to('http://example.com')
  263. end
  264. it "returns '../' with 'http://example.com/'" do
  265. assert_equal '../', url.relative_path_to('http://example.com/')
  266. end
  267. it "returns '../path' with 'http://example.com/path'" do
  268. assert_equal '../path', url.relative_path_to('http://example.com/path')
  269. end
  270. it "returns '.' with 'http://example.com/path/'" do
  271. assert_equal '.', url.relative_path_to('http://example.com/path/')
  272. end
  273. it "returns 'to' with 'http://example.com/path/to'" do
  274. assert_equal 'to', url.relative_path_to('http://example.com/path/to')
  275. end
  276. it "returns '../PATH/to' with 'http://example.com/PATH/to'" do
  277. assert_equal '../PATH/to', url.relative_path_to('http://example.com/PATH/to')
  278. end
  279. it "returns 'to/' with 'http://example.com/path/to/'" do
  280. assert_equal 'to/', url.relative_path_to('http://example.com/path/to/')
  281. end
  282. it "returns 'to/file' with 'http://example.com/path/to/file'" do
  283. assert_equal 'to/file', url.relative_path_to('http://example.com/path/to/file')
  284. end
  285. it "returns 'to/file/' with 'http://example.com/path/to/file/'" do
  286. assert_equal 'to/file/', url.relative_path_to('http://example.com/path/to/file/')
  287. end
  288. end
  289. context "when the URL is 'http://example.com/path/to/'" do
  290. let :url do
  291. URL.parse 'http://example.com/path/to/'
  292. end
  293. it "returns '../../' with 'http://example.com'" do
  294. assert_equal '../../', url.relative_path_to('http://example.com')
  295. end
  296. it "returns '../../' with 'http://example.com/'" do
  297. assert_equal '../../', url.relative_path_to('http://example.com/')
  298. end
  299. it "returns '../../path' with 'http://example.com/path'" do
  300. assert_equal '../../path', url.relative_path_to('http://example.com/path')
  301. end
  302. it "returns '../' with 'http://example.com/path/'" do
  303. assert_equal '../', url.relative_path_to('http://example.com/path/')
  304. end
  305. it "returns '../to' with 'http://example.com/path/to'" do
  306. assert_equal '../to', url.relative_path_to('http://example.com/path/to')
  307. end
  308. it "returns '.' with 'http://example.com/path/to/'" do
  309. assert_equal '.', url.relative_path_to('http://example.com/path/to/')
  310. end
  311. it "returns 'file' with 'http://example.com/path/to/file'" do
  312. assert_equal 'file', url.relative_path_to('http://example.com/path/to/file')
  313. end
  314. it "returns 'file/' with 'http://example.com/path/to/file/'" do
  315. assert_equal 'file/', url.relative_path_to('http://example.com/path/to/file/')
  316. end
  317. end
  318. end
  319. describe "#relative_path_from" do
  320. context "when the URL is 'http://example.com/path/file'" do
  321. let :url do
  322. URL.parse 'http://example.com/path/file'
  323. end
  324. it "returns 'path/file' with 'http://example.com/path'" do
  325. assert_equal 'path/file', url.relative_path_from('http://example.com/path')
  326. end
  327. end
  328. end
  329. end