equal.test.js 3.2 KB

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