parse.test.js 14 KB

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