equal.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict'
  2. const tap = require('tap')
  3. const test = tap.test
  4. const URI = require('../')
  5. const fn = URI.equal
  6. const runTest = (t, suite) => {
  7. suite.forEach(s => {
  8. const operator = s.result ? '==' : '!='
  9. t.equal(fn(s.pair[0], s.pair[1]), s.result, `${s.pair[0]} ${operator} ${s.pair[1]}`)
  10. t.equal(fn(s.pair[1], s.pair[0]), s.result, `${s.pair[1]} ${operator} ${s.pair[0]}`)
  11. })
  12. }
  13. test('URI Equals', (t) => {
  14. const suite = [
  15. { pair: ['example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'], result: true }, // test from RFC 3986
  16. { pair: ['http://example.org/~user', 'http://example.org/%7euser'], result: true } // test from RFC 3987
  17. ]
  18. runTest(t, suite)
  19. t.end()
  20. })
  21. // test('IRI Equals', (t) => {
  22. // // example from RFC 3987
  23. // t.equal(URI.equal('example://a/b/c/%7Bfoo%7D/ros\xE9', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9', IRI_OPTION), true)
  24. // t.end()
  25. // })
  26. test('HTTP Equals', (t) => {
  27. const suite = [
  28. // test from RFC 2616
  29. { pair: ['http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'], result: true },
  30. { pair: [{ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'], result: true },
  31. { pair: ['http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
  32. { pair: ['http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
  33. { pair: ['HTTP://ABC.COM', 'http://abc.com/'], result: true },
  34. // test from RFC 3986
  35. { pair: ['http://example.com:/', 'http://example.com:80/'], result: true }
  36. ]
  37. runTest(t, suite)
  38. t.end()
  39. })
  40. test('HTTPS Equals', (t) => {
  41. const suite = [
  42. { pair: ['https://example.com', 'https://example.com:443/'], result: true },
  43. { pair: ['https://example.com:/', 'https://example.com:443/'], result: true }
  44. ]
  45. runTest(t, suite)
  46. t.end()
  47. })
  48. test('URN Equals', (t) => {
  49. const suite = [
  50. // test from RFC 2141
  51. { pair: ['urn:foo:a123,456', 'urn:foo:a123,456'], result: true },
  52. { pair: ['urn:foo:a123,456', 'URN:foo:a123,456'], result: true },
  53. { pair: ['urn:foo:a123,456', 'urn:FOO:a123,456'], result: true }
  54. ]
  55. // Disabling for now as the whole equal logic might need
  56. // to be refactored
  57. // t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
  58. // t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
  59. runTest(t, suite)
  60. t.end()
  61. })
  62. test('UUID Equals', (t) => {
  63. const suite = [
  64. { pair: ['URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'], result: true }
  65. ]
  66. runTest(t, suite)
  67. t.end()
  68. })
  69. // test('Mailto Equals', (t) => {
  70. // // tests from RFC 6068
  71. // t.equal(URI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true)
  72. // t.equal(URI.equal('mailto:?to=addr1@an.example,addr2@an.example', 'mailto:addr1@an.example?to=addr2@an.example'), true)
  73. // t.end()
  74. // })
  75. test('WS Equal', (t) => {
  76. const suite = [
  77. { pair: ['WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'], result: true }
  78. ]
  79. runTest(t, suite)
  80. t.end()
  81. })
  82. test('WSS Equal', (t) => {
  83. const suite = [
  84. { pair: ['WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'], result: true }
  85. ]
  86. runTest(t, suite)
  87. t.end()
  88. })