compatibility.test.js 4.0 KB

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