serialize.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict'
  2. const test = require('tape')
  3. const URI = require('../')
  4. test('URI Serialize', (t) => {
  5. let components = {
  6. scheme: undefined,
  7. userinfo: undefined,
  8. host: undefined,
  9. port: undefined,
  10. path: undefined,
  11. query: undefined,
  12. fragment: undefined
  13. }
  14. t.equal(URI.serialize(components), '', 'Undefined Components')
  15. components = {
  16. scheme: '',
  17. userinfo: '',
  18. host: '',
  19. port: 0,
  20. path: '',
  21. query: '',
  22. fragment: ''
  23. }
  24. t.equal(URI.serialize(components), '//@:0?#', 'Empty Components')
  25. components = {
  26. scheme: 'uri',
  27. userinfo: 'foo:bar',
  28. host: 'example.com',
  29. port: 1,
  30. path: 'path',
  31. query: 'query',
  32. fragment: 'fragment'
  33. }
  34. t.equal(URI.serialize(components), 'uri://foo:bar@example.com:1/path?query#fragment', 'All Components')
  35. components = {
  36. scheme: 'uri',
  37. host: 'example.com',
  38. port: '9000'
  39. }
  40. t.equal(URI.serialize(components), 'uri://example.com:9000', 'String port')
  41. t.equal(URI.serialize({ path: '//path' }), '/%2Fpath', 'Double slash path')
  42. t.equal(URI.serialize({ path: 'foo:bar' }), 'foo%3Abar', 'Colon path')
  43. t.equal(URI.serialize({ path: '?query' }), '%3Fquery', 'Query path')
  44. t.equal(URI.serialize({ host: '10.10.10.10' }), '//10.10.10.10', 'IPv4address')
  45. // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4)
  46. t.equal(URI.serialize({ host: '10.10.10.10.example.com' }), '//10.10.10.10.example.com', 'Mixed IPv4address & reg-name')
  47. // IPv6address
  48. t.equal(URI.serialize({ host: '2001:db8::7' }), '//[2001:db8::7]', 'IPv6 Host')
  49. t.equal(URI.serialize({ host: '::ffff:129.144.52.38' }), '//[::ffff:129.144.52.38]', 'IPv6 Mixed Host')
  50. t.equal(URI.serialize({ host: '2606:2800:220:1:248:1893:25c8:1946' }), '//[2606:2800:220:1:248:1893:25c8:1946]', 'IPv6 Full Host')
  51. // IPv6address with zone identifier, RFC 6874
  52. t.equal(URI.serialize({ host: 'fe80::a%en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Unescaped Host')
  53. t.equal(URI.serialize({ host: 'fe80::a%25en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Escaped Host')
  54. t.end()
  55. })
  56. test('WS serialize', (t) => {
  57. t.equal(URI.serialize({ scheme: 'ws' }), 'ws:')
  58. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com' }), 'ws://example.com')
  59. t.equal(URI.serialize({ scheme: 'ws', resourceName: '/' }), 'ws:')
  60. t.equal(URI.serialize({ scheme: 'ws', resourceName: '/foo' }), 'ws:/foo')
  61. t.equal(URI.serialize({ scheme: 'ws', resourceName: '/foo?bar' }), 'ws:/foo?bar')
  62. t.equal(URI.serialize({ scheme: 'ws', secure: false }), 'ws:')
  63. t.equal(URI.serialize({ scheme: 'ws', secure: true }), 'wss:')
  64. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo' }), 'ws://example.com/foo')
  65. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar' }), 'ws://example.com/foo?bar')
  66. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', secure: false }), 'ws://example.com')
  67. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', secure: true }), 'wss://example.com')
  68. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar')
  69. t.equal(URI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar')
  70. t.end()
  71. })
  72. test('WSS serialize', (t) => {
  73. t.equal(URI.serialize({ scheme: 'wss' }), 'wss:')
  74. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com' }), 'wss://example.com')
  75. t.equal(URI.serialize({ scheme: 'wss', resourceName: '/' }), 'wss:')
  76. t.equal(URI.serialize({ scheme: 'wss', resourceName: '/foo' }), 'wss:/foo')
  77. t.equal(URI.serialize({ scheme: 'wss', resourceName: '/foo?bar' }), 'wss:/foo?bar')
  78. t.equal(URI.serialize({ scheme: 'wss', secure: false }), 'ws:')
  79. t.equal(URI.serialize({ scheme: 'wss', secure: true }), 'wss:')
  80. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo' }), 'wss://example.com/foo')
  81. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar' }), 'wss://example.com/foo?bar')
  82. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', secure: false }), 'ws://example.com')
  83. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', secure: true }), 'wss://example.com')
  84. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar')
  85. t.equal(URI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar')
  86. t.end()
  87. })
  88. test('URN serialize', (t) => {
  89. // example from RFC 2141
  90. const components = {
  91. scheme: 'urn',
  92. nid: 'foo',
  93. nss: 'a123,456'
  94. }
  95. t.equal(URI.serialize(components), 'urn:foo:a123,456')
  96. // example from RFC 4122
  97. let uuidcomponents = {
  98. scheme: 'urn',
  99. nid: 'uuid',
  100. uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
  101. }
  102. t.equal(URI.serialize(uuidcomponents), 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6')
  103. uuidcomponents = {
  104. scheme: 'urn',
  105. nid: 'uuid',
  106. uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6'
  107. }
  108. t.equal(URI.serialize(uuidcomponents), 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6')
  109. t.end()
  110. })
  111. test('URN NID Override', (t) => {
  112. let components = URI.parse('urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', { nid: 'uuid' })
  113. t.equal(components.error, undefined, 'errors')
  114. t.equal(components.scheme, 'urn', 'scheme')
  115. t.equal(components.path, undefined, 'path')
  116. t.equal(components.nid, 'foo', 'nid')
  117. t.equal(components.nss, undefined, 'nss')
  118. t.equal(components.uuid, 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'uuid')
  119. components = {
  120. scheme: 'urn',
  121. nid: 'foo',
  122. uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
  123. }
  124. t.equal(URI.serialize(components, { nid: 'uuid' }), 'urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6')
  125. t.end()
  126. })