Debian Codegraph @

fast-uri/test/equal.test.js

raw
1 'use strict'
2
3 const test = require('tape')
4 const fastURI = require('..')
5
6 const fn = fastURI.equal
7 const runTest = (t, suite) => {
8 suite.forEach(s => {
9 const operator = s.result ? '==' : '!='
10 t.equal(fn(s.pair[0], s.pair[1]), s.result, `${s.pair[0]} ${operator} ${s.pair[1]}`)
11 t.equal(fn(s.pair[1], s.pair[0]), s.result, `${s.pair[1]} ${operator} ${s.pair[0]}`)
12 })
13 }
14
15 test('URI Equals', (t) => {
16 const suite = [
17 { pair: ['example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'], result: true }, // test from RFC 3986
18 { pair: ['http://example.org/~user', 'http://example.org/%7euser'], result: true } // test from RFC 3987
19 ]
20 runTest(t, suite)
21 t.end()
22 })
23
24 // test('IRI Equals', (t) => {
25 // // example from RFC 3987
26 // 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)
27 // t.end()
28 // })
29
30 test('HTTP Equals', (t) => {
31 const suite = [
32 // test from RFC 2616
33 { pair: ['http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'], result: true },
34 { pair: [{ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'], result: true },
35 { pair: ['http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
36 { pair: ['http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
37 { pair: ['HTTP://ABC.COM', 'http://abc.com/'], result: true },
38 // test from RFC 3986
39 { pair: ['http://example.com:/', 'http://example.com:80/'], result: true }
40 ]
41 runTest(t, suite)
42 t.end()
43 })
44
45 test('HTTPS Equals', (t) => {
46 const suite = [
47 { pair: ['https://example.com', 'https://example.com:443/'], result: true },
48 { pair: ['https://example.com:/', 'https://example.com:443/'], result: true }
49 ]
50 runTest(t, suite)
51 t.end()
52 })
53
54 test('URN Equals', (t) => {
55 const suite = [
56 // test from RFC 2141
57 { pair: ['urn:foo:a123,456', 'urn:foo:a123,456'], result: true },
58 { pair: ['urn:foo:a123,456', 'URN:foo:a123,456'], result: true },
59 { pair: ['urn:foo:a123,456', 'urn:FOO:a123,456'], result: true }
60 ]
61
62 // Disabling for now as the whole equal logic might need
63 // to be refactored
64 // t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
65 // t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
66
67 runTest(t, suite)
68
69 t.throws(() => {
70 fn('urn:', 'urn:FOO:a123,456')
71 }, 'URN without nid cannot be serialized')
72
73 t.end()
74 })
75
76 test('UUID Equals', (t) => {
77 const suite = [
78 { pair: ['URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'], result: true }
79 ]
80
81 runTest(t, suite)
82 t.end()
83 })
84
85 // test('Mailto Equals', (t) => {
86 // // tests from RFC 6068
87 // t.equal(URI.equal('mailto:[email protected],[email protected]', 'mailto:[email protected],[email protected]'), true)
88 // t.equal(URI.equal('mailto:[email protected],[email protected]', 'mailto:[email protected][email protected]'), true)
89 // t.end()
90 // })
91
92 test('WS Equal', (t) => {
93 const suite = [
94 { pair: ['WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'], result: true }
95 ]
96
97 runTest(t, suite)
98 t.end()
99 })
100
101 test('WSS Equal', (t) => {
102 const suite = [
103 { pair: ['WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'], result: true }
104 ]
105
106 runTest(t, suite)
107 t.end()
108 })
109
110 test('URI Equals tolerates malformed fragments', (t) => {
111 t.equal(
112 fastURI.equal('http://example.com/#%E0%A4A', 'http://example.com/#%E0%A4A'),
113 true,
114 'malformed fragment does not throw during equality checks'
115 )
116 t.end()
117 })