compatibility.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict'
  2. const tap = require('tap')
  3. const test = tap.test
  4. const fastifyURI = require('../')
  5. const urijs = require('uri-js')
  6. test('compatibility Parse', (t) => {
  7. const toParse = [
  8. '//www.g.com/error\n/bleh/bleh',
  9. 'https://fastify.org',
  10. '/definitions/Record%3Cstring%2CPerson%3E',
  11. '//10.10.10.10',
  12. // '//10.10.000.10', <-- not a valid URI per URI spec: https://datatracker.ietf.org/doc/html/rfc5954#section-4.1
  13. '//[2001:db8::7%en0]',
  14. '//[2001:dbZ::1]:80',
  15. '//[2001:db8::1]:80',
  16. '//[2001:db8::001]:80',
  17. 'uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body',
  18. 'http://user:pass@example.com:123/one/space in.url?q1=a1&q2=a2#body',
  19. '//[::ffff:129.144.52.38]',
  20. 'uri://10.10.10.10.example.com/en/process',
  21. '//[2606:2800:220:1:248:1893:25c8:1946]/test',
  22. 'ws://example.com/chat',
  23. 'ws://example.com/foo?bar=baz',
  24. 'wss://example.com/?bar=baz',
  25. 'wss://example.com/chat',
  26. 'wss://example.com/foo?bar=baz',
  27. 'wss://example.com/?bar=baz',
  28. 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
  29. 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6',
  30. 'urn:example:%D0%B0123,z456',
  31. '//[2606:2800:220:1:248:1893:25c8:1946:43209]',
  32. 'http://foo.bar',
  33. 'http://',
  34. '#/$defs/stringMap',
  35. '#/$defs/string%20Map',
  36. '#/$defs/string Map',
  37. '//?json=%7B%22foo%22%3A%22bar%22%7D'
  38. // 'mailto:chris@example.com'-203845,
  39. // 'mailto:infobot@example.com?subject=current-issue',
  40. // 'mailto:infobot@example.com?body=send%20current-issue',
  41. // 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index',
  42. // 'mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E',
  43. // 'mailto:majordomo@example.com?body=subscribe%20bamboo-l',
  44. // 'mailto:joe@example.com?cc=bob@example.com&body=hello',
  45. // 'mailto:gorby%25kremvax@example.com',
  46. // 'mailto:unlikely%3Faddress@example.com?blat=foop',
  47. // 'mailto:Mike%26family@example.org',
  48. // 'mailto:%22not%40me%22@example.org',
  49. // 'mailto:%22oh%5C%5Cno%22@example.org',
  50. // 'mailto:%22%5C%5C%5C%22it\'s%5C%20ugly%5C%5C%5C%22%22@example.org',
  51. // 'mailto:user@example.org?subject=caf%C3%A9',
  52. // 'mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D',
  53. // 'mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D',
  54. // 'mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9',
  55. // 'mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO'
  56. ]
  57. toParse.forEach((x) => {
  58. t.same(fastifyURI.parse(x), urijs.parse(x), 'Compatibility parse: ' + x)
  59. })
  60. t.end()
  61. })
  62. test('compatibility serialize', (t) => {
  63. const toSerialize = [
  64. { host: '10.10.10.10.example.com' },
  65. { host: '2001:db8::7' },
  66. { host: '::ffff:129.144.52.38' },
  67. { host: '2606:2800:220:1:248:1893:25c8:1946' },
  68. { host: '10.10.10.10.example.com' },
  69. { host: '10.10.10.10' },
  70. { path: '?query' },
  71. { path: 'foo:bar' },
  72. { path: '//path' },
  73. {
  74. scheme: 'uri',
  75. host: 'example.com',
  76. port: '9000'
  77. },
  78. {
  79. scheme: 'uri',
  80. userinfo: 'foo:bar',
  81. host: 'example.com',
  82. port: 1,
  83. path: 'path',
  84. query: 'query',
  85. fragment: 'fragment'
  86. },
  87. {
  88. scheme: '',
  89. userinfo: '',
  90. host: '',
  91. port: 0,
  92. path: '',
  93. query: '',
  94. fragment: ''
  95. },
  96. {
  97. scheme: undefined,
  98. userinfo: undefined,
  99. host: undefined,
  100. port: undefined,
  101. path: undefined,
  102. query: undefined,
  103. fragment: undefined
  104. },
  105. { host: 'fe80::a%en1' },
  106. { host: 'fe80::a%25en1' },
  107. {
  108. scheme: 'ws',
  109. host: 'example.com',
  110. resourceName: '/foo?bar',
  111. secure: true
  112. },
  113. {
  114. scheme: 'scheme',
  115. path: 'with:colon'
  116. }
  117. ]
  118. toSerialize.forEach((x) => {
  119. const r = JSON.stringify(x)
  120. t.same(
  121. fastifyURI.serialize(x),
  122. urijs.serialize(x),
  123. 'Compatibility serialize: ' + JSON.stringify(r)
  124. )
  125. })
  126. t.end()
  127. })