parse.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict'
  2. const test = require('tape')
  3. const URI = require('../')
  4. test('URI parse', (t) => {
  5. let components
  6. // scheme
  7. components = URI.parse('uri:')
  8. t.equal(components.error, undefined, 'scheme errors')
  9. t.equal(components.scheme, 'uri', 'scheme')
  10. // t.equal(components.authority, undefined, "authority");
  11. t.equal(components.userinfo, undefined, 'userinfo')
  12. t.equal(components.host, undefined, 'host')
  13. t.equal(components.port, undefined, 'port')
  14. t.equal(components.path, '', 'path')
  15. t.equal(components.query, undefined, 'query')
  16. t.equal(components.fragment, undefined, 'fragment')
  17. // userinfo
  18. components = URI.parse('//@')
  19. t.equal(components.error, undefined, 'userinfo errors')
  20. t.equal(components.scheme, undefined, 'scheme')
  21. // t.equal(components.authority, "@", "authority");
  22. t.equal(components.userinfo, '', 'userinfo')
  23. t.equal(components.host, '', 'host')
  24. t.equal(components.port, undefined, 'port')
  25. t.equal(components.path, '', 'path')
  26. t.equal(components.query, undefined, 'query')
  27. t.equal(components.fragment, undefined, 'fragment')
  28. // host
  29. components = URI.parse('//')
  30. t.equal(components.error, undefined, 'host errors')
  31. t.equal(components.scheme, undefined, 'scheme')
  32. // t.equal(components.authority, "", "authority");
  33. t.equal(components.userinfo, undefined, 'userinfo')
  34. t.equal(components.host, '', 'host')
  35. t.equal(components.port, undefined, 'port')
  36. t.equal(components.path, '', 'path')
  37. t.equal(components.query, undefined, 'query')
  38. t.equal(components.fragment, undefined, 'fragment')
  39. // port
  40. components = URI.parse('//:')
  41. t.equal(components.error, undefined, 'port errors')
  42. t.equal(components.scheme, undefined, 'scheme')
  43. // t.equal(components.authority, ":", "authority");
  44. t.equal(components.userinfo, undefined, 'userinfo')
  45. t.equal(components.host, '', 'host')
  46. t.equal(components.port, '', 'port')
  47. t.equal(components.path, '', 'path')
  48. t.equal(components.query, undefined, 'query')
  49. t.equal(components.fragment, undefined, 'fragment')
  50. // path
  51. components = URI.parse('')
  52. t.equal(components.error, undefined, 'path errors')
  53. t.equal(components.scheme, undefined, 'scheme')
  54. // t.equal(components.authority, undefined, "authority");
  55. t.equal(components.userinfo, undefined, 'userinfo')
  56. t.equal(components.host, undefined, 'host')
  57. t.equal(components.port, undefined, 'port')
  58. t.equal(components.path, '', 'path')
  59. t.equal(components.query, undefined, 'query')
  60. t.equal(components.fragment, undefined, 'fragment')
  61. // query
  62. components = URI.parse('?')
  63. t.equal(components.error, undefined, 'query errors')
  64. t.equal(components.scheme, undefined, 'scheme')
  65. // t.equal(components.authority, undefined, "authority");
  66. t.equal(components.userinfo, undefined, 'userinfo')
  67. t.equal(components.host, undefined, 'host')
  68. t.equal(components.port, undefined, 'port')
  69. t.equal(components.path, '', 'path')
  70. t.equal(components.query, '', 'query')
  71. t.equal(components.fragment, undefined, 'fragment')
  72. // fragment
  73. components = URI.parse('#')
  74. t.equal(components.error, undefined, 'fragment errors')
  75. t.equal(components.scheme, undefined, 'scheme')
  76. // t.equal(components.authority, undefined, "authority");
  77. t.equal(components.userinfo, undefined, 'userinfo')
  78. t.equal(components.host, undefined, 'host')
  79. t.equal(components.port, undefined, 'port')
  80. t.equal(components.path, '', 'path')
  81. t.equal(components.query, undefined, 'query')
  82. t.equal(components.fragment, '', 'fragment')
  83. // fragment with character tabulation
  84. components = URI.parse('#\t')
  85. t.equal(components.error, undefined, 'path errors')
  86. t.equal(components.scheme, undefined, 'scheme')
  87. // t.equal(components.authority, undefined, "authority");
  88. t.equal(components.userinfo, undefined, 'userinfo')
  89. t.equal(components.host, undefined, 'host')
  90. t.equal(components.port, undefined, 'port')
  91. t.equal(components.path, '', 'path')
  92. t.equal(components.query, undefined, 'query')
  93. t.equal(components.fragment, '%09', 'fragment')
  94. // fragment with line feed
  95. components = URI.parse('#\n')
  96. t.equal(components.error, undefined, 'path errors')
  97. t.equal(components.scheme, undefined, 'scheme')
  98. // t.equal(components.authority, undefined, "authority");
  99. t.equal(components.userinfo, undefined, 'userinfo')
  100. t.equal(components.host, undefined, 'host')
  101. t.equal(components.port, undefined, 'port')
  102. t.equal(components.path, '', 'path')
  103. t.equal(components.query, undefined, 'query')
  104. t.equal(components.fragment, '%0A', 'fragment')
  105. // fragment with line tabulation
  106. components = URI.parse('#\v')
  107. t.equal(components.error, undefined, 'path errors')
  108. t.equal(components.scheme, undefined, 'scheme')
  109. // t.equal(components.authority, undefined, "authority");
  110. t.equal(components.userinfo, undefined, 'userinfo')
  111. t.equal(components.host, undefined, 'host')
  112. t.equal(components.port, undefined, 'port')
  113. t.equal(components.path, '', 'path')
  114. t.equal(components.query, undefined, 'query')
  115. t.equal(components.fragment, '%0B', 'fragment')
  116. // fragment with form feed
  117. components = URI.parse('#\f')
  118. t.equal(components.error, undefined, 'path errors')
  119. t.equal(components.scheme, undefined, 'scheme')
  120. // t.equal(components.authority, undefined, "authority");
  121. t.equal(components.userinfo, undefined, 'userinfo')
  122. t.equal(components.host, undefined, 'host')
  123. t.equal(components.port, undefined, 'port')
  124. t.equal(components.path, '', 'path')
  125. t.equal(components.query, undefined, 'query')
  126. t.equal(components.fragment, '%0C', 'fragment')
  127. // fragment with carriage return
  128. components = URI.parse('#\r')
  129. t.equal(components.error, undefined, 'path errors')
  130. t.equal(components.scheme, undefined, 'scheme')
  131. // t.equal(components.authority, undefined, "authority");
  132. t.equal(components.userinfo, undefined, 'userinfo')
  133. t.equal(components.host, undefined, 'host')
  134. t.equal(components.port, undefined, 'port')
  135. t.equal(components.path, '', 'path')
  136. t.equal(components.query, undefined, 'query')
  137. t.equal(components.fragment, '%0D', 'fragment')
  138. // all
  139. components = URI.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
  140. t.equal(components.error, undefined, 'all errors')
  141. t.equal(components.scheme, 'uri', 'scheme')
  142. // t.equal(components.authority, "user:pass@example.com:123", "authority");
  143. t.equal(components.userinfo, 'user:pass', 'userinfo')
  144. t.equal(components.host, 'example.com', 'host')
  145. t.equal(components.port, 123, 'port')
  146. t.equal(components.path, '/one/two.three', 'path')
  147. t.equal(components.query, 'q1=a1&q2=a2', 'query')
  148. t.equal(components.fragment, 'body', 'fragment')
  149. // IPv4address
  150. components = URI.parse('//10.10.10.10')
  151. t.equal(components.error, undefined, 'IPv4address errors')
  152. t.equal(components.scheme, undefined, 'scheme')
  153. t.equal(components.userinfo, undefined, 'userinfo')
  154. t.equal(components.host, '10.10.10.10', 'host')
  155. t.equal(components.port, undefined, 'port')
  156. t.equal(components.path, '', 'path')
  157. t.equal(components.query, undefined, 'query')
  158. t.equal(components.fragment, undefined, 'fragment')
  159. // IPv4address with unformated 0 stay as-is
  160. components = URI.parse('//10.10.000.10') // not valid as per https://datatracker.ietf.org/doc/html/rfc5954#section-4.1
  161. t.equal(components.error, undefined, 'IPv4address errors')
  162. t.equal(components.scheme, undefined, 'scheme')
  163. t.equal(components.userinfo, undefined, 'userinfo')
  164. t.equal(components.host, '10.10.000.10', 'host')
  165. t.equal(components.port, undefined, 'port')
  166. t.equal(components.path, '', 'path')
  167. t.equal(components.query, undefined, 'query')
  168. t.equal(components.fragment, undefined, 'fragment')
  169. components = URI.parse('//01.01.01.01') // not valid in URIs: https://datatracker.ietf.org/doc/html/rfc3986#section-7.4
  170. t.equal(components.error, undefined, 'IPv4address errors')
  171. t.equal(components.scheme, undefined, 'scheme')
  172. t.equal(components.userinfo, undefined, 'userinfo')
  173. t.equal(components.host, '01.01.01.01', 'host')
  174. t.equal(components.port, undefined, 'port')
  175. t.equal(components.path, '', 'path')
  176. t.equal(components.query, undefined, 'query')
  177. t.equal(components.fragment, undefined, 'fragment')
  178. // IPv6address
  179. components = URI.parse('//[2001:db8::7]')
  180. t.equal(components.error, undefined, 'IPv4address errors')
  181. t.equal(components.scheme, undefined, 'scheme')
  182. t.equal(components.userinfo, undefined, 'userinfo')
  183. t.equal(components.host, '2001:db8::7', 'host')
  184. t.equal(components.port, undefined, 'port')
  185. t.equal(components.path, '', 'path')
  186. t.equal(components.query, undefined, 'query')
  187. t.equal(components.fragment, undefined, 'fragment')
  188. // invalid IPv6
  189. components = URI.parse('//[2001:dbZ::7]')
  190. t.equal(components.host, '[2001:dbz::7]')
  191. // mixed IPv4address & IPv6address
  192. components = URI.parse('//[::ffff:129.144.52.38]')
  193. t.equal(components.error, undefined, 'IPv4address errors')
  194. t.equal(components.scheme, undefined, 'scheme')
  195. t.equal(components.userinfo, undefined, 'userinfo')
  196. t.equal(components.host, '::ffff:129.144.52.38', 'host')
  197. t.equal(components.port, undefined, 'port')
  198. t.equal(components.path, '', 'path')
  199. t.equal(components.query, undefined, 'query')
  200. t.equal(components.fragment, undefined, 'fragment')
  201. // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4)
  202. components = URI.parse('uri://10.10.10.10.example.com/en/process')
  203. t.equal(components.error, undefined, 'mixed errors')
  204. t.equal(components.scheme, 'uri', 'scheme')
  205. t.equal(components.userinfo, undefined, 'userinfo')
  206. t.equal(components.host, '10.10.10.10.example.com', 'host')
  207. t.equal(components.port, undefined, 'port')
  208. t.equal(components.path, '/en/process', 'path')
  209. t.equal(components.query, undefined, 'query')
  210. t.equal(components.fragment, undefined, 'fragment')
  211. // IPv6address, example from bkw (https://github.com/garycourt/uri-js/pull/16)
  212. components = URI.parse('//[2606:2800:220:1:248:1893:25c8:1946]/test')
  213. t.equal(components.error, undefined, 'IPv6address errors')
  214. t.equal(components.scheme, undefined, 'scheme')
  215. t.equal(components.userinfo, undefined, 'userinfo')
  216. t.equal(components.host, '2606:2800:220:1:248:1893:25c8:1946', 'host')
  217. t.equal(components.port, undefined, 'port')
  218. t.equal(components.path, '/test', 'path')
  219. t.equal(components.query, undefined, 'query')
  220. t.equal(components.fragment, undefined, 'fragment')
  221. // IPv6address, example from RFC 5952
  222. components = URI.parse('//[2001:db8::1]:80')
  223. t.equal(components.error, undefined, 'IPv6address errors')
  224. t.equal(components.scheme, undefined, 'scheme')
  225. t.equal(components.userinfo, undefined, 'userinfo')
  226. t.equal(components.host, '2001:db8::1', 'host')
  227. t.equal(components.port, 80, 'port')
  228. t.equal(components.path, '', 'path')
  229. t.equal(components.query, undefined, 'query')
  230. t.equal(components.fragment, undefined, 'fragment')
  231. // IPv6address with zone identifier, RFC 6874
  232. components = URI.parse('//[fe80::a%25en1]')
  233. t.equal(components.error, undefined, 'IPv4address errors')
  234. t.equal(components.scheme, undefined, 'scheme')
  235. t.equal(components.userinfo, undefined, 'userinfo')
  236. t.equal(components.host, 'fe80::a%en1', 'host')
  237. t.equal(components.port, undefined, 'port')
  238. t.equal(components.path, '', 'path')
  239. t.equal(components.query, undefined, 'query')
  240. t.equal(components.fragment, undefined, 'fragment')
  241. // IPv6address with an unescaped interface specifier, example from pekkanikander (https://github.com/garycourt/uri-js/pull/22)
  242. components = URI.parse('//[2001:db8::7%en0]')
  243. t.equal(components.error, undefined, 'IPv6address interface errors')
  244. t.equal(components.scheme, undefined, 'scheme')
  245. t.equal(components.userinfo, undefined, 'userinfo')
  246. t.equal(components.host, '2001:db8::7%en0', 'host')
  247. t.equal(components.port, undefined, 'port')
  248. t.equal(components.path, '', 'path')
  249. t.equal(components.query, undefined, 'query')
  250. t.equal(components.fragment, undefined, 'fragment')
  251. // UUID V1
  252. components = URI.parse('urn:uuid:b571b0bc-4713-11ec-81d3-0242ac130003')
  253. t.equal(components.error, undefined, 'errors')
  254. t.equal(components.scheme, 'urn', 'scheme')
  255. // t.equal(components.authority, undefined, "authority");
  256. t.equal(components.userinfo, undefined, 'userinfo')
  257. t.equal(components.host, undefined, 'host')
  258. t.equal(components.port, undefined, 'port')
  259. t.equal(components.path, undefined, 'path')
  260. t.equal(components.query, undefined, 'query')
  261. t.equal(components.fragment, undefined, 'fragment')
  262. t.equal(components.nid, 'uuid', 'nid')
  263. t.equal(components.nss, undefined, 'nss')
  264. t.equal(components.uuid, 'b571b0bc-4713-11ec-81d3-0242ac130003', 'uuid')
  265. // UUID v4
  266. components = URI.parse('urn:uuid:97a32222-89b7-420e-8507-4360723e2c2a')
  267. t.equal(components.uuid, '97a32222-89b7-420e-8507-4360723e2c2a', 'uuid')
  268. components = URI.parse('urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6')
  269. t.notSame(components.error, undefined, 'errors')
  270. components = URI.parse('urn:foo:a123,456')
  271. t.equal(components.error, undefined, 'errors')
  272. t.equal(components.scheme, 'urn', 'scheme')
  273. // t.equal(components.authority, undefined, "authority");
  274. t.equal(components.userinfo, undefined, 'userinfo')
  275. t.equal(components.host, undefined, 'host')
  276. t.equal(components.port, undefined, 'port')
  277. t.equal(components.path, undefined, 'path')
  278. t.equal(components.query, undefined, 'query')
  279. t.equal(components.fragment, undefined, 'fragment')
  280. t.equal(components.nid, 'foo', 'nid')
  281. t.equal(components.nss, 'a123,456', 'nss')
  282. components = URI.parse('//[2606:2800:220:1:248:1893:25c8:1946:43209]')
  283. t.equal(components.host, '[2606:2800:220:1:248:1893:25c8:1946:43209]')
  284. components = URI.parse('urn:foo:|\\24fpl')
  285. t.equal(components.error, 'URN can not be parsed.')
  286. t.end()
  287. })