Skip to content

Commit

Permalink
test(equal): refactor test suites (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth committed Jun 19, 2022
1 parent b9a9259 commit 8969aa1
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions test/equal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ const tap = require('tap')
const test = tap.test
const URI = require('../')

test('URI Equals', (t) => {
// test from RFC 3986
t.equal(URI.equal('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'), true)
const fn = URI.equal
const runTest = (t, suite) => {
suite.forEach(s => {
const operator = s.result ? '==' : '!='
t.equal(fn(s.pair[0], s.pair[1]), s.result, `${s.pair[0]} ${operator} ${s.pair[1]}`)
t.equal(fn(s.pair[1], s.pair[0]), s.result, `${s.pair[1]} ${operator} ${s.pair[0]}`)
})
}

// test from RFC 3987
t.equal(URI.equal('http://example.org/~user', 'http://example.org/%7euser'), true)
test('URI Equals', (t) => {
const suite = [
{ pair: ['example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'], result: true }, // test from RFC 3986
{ pair: ['http://example.org/~user', 'http://example.org/%7euser'], result: true } // test from RFC 3987
]
runTest(t, suite)
t.end()
})

Expand All @@ -20,39 +29,55 @@ test('URI Equals', (t) => {
// })

test('HTTP Equals', (t) => {
// test from RFC 2616
t.equal(URI.equal('http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'), true)
t.equal(URI.equal({ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'), true)
t.equal(URI.equal('http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'), true)
t.equal(URI.equal('http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'), true)
t.equal(URI.equal('HTTP://ABC.COM', 'http://abc.com/'), true)
// test from RFC 3986
t.equal(URI.equal('http://example.com:/', 'http://example.com:80/'), true)
const suite = [
// test from RFC 2616
{ pair: ['http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'], result: true },
{ pair: [{ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'], result: true },
{ pair: ['http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
{ pair: ['http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
{ pair: ['HTTP://ABC.COM', 'http://abc.com/'], result: true },
// test from RFC 3986
{ pair: ['http://example.com:/', 'http://example.com:80/'], result: true }
]
runTest(t, suite)
t.end()
})

test('HTTPS Equals', (t) => {
t.equal(URI.equal('https://example.com', 'https://example.com:443/'), true)
t.equal(URI.equal('https://example.com:/', 'https://example.com:443/'), true)
const suite = [
{ pair: ['https://example.com', 'https://example.com:443/'], result: true },
{ pair: ['https://example.com:/', 'https://example.com:443/'], result: true }
]
runTest(t, suite)
t.end()
})

test('URN Equals', (t) => {
// test from RFC 2141
t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:a123,456'), true)
t.equal(URI.equal('urn:foo:a123,456', 'URN:foo:a123,456'), true)
t.equal(URI.equal('urn:foo:a123,456', 'urn:FOO:a123,456'), true)
const suite = [
// test from RFC 2141
{ pair: ['urn:foo:a123,456', 'urn:foo:a123,456'], result: true },
{ pair: ['urn:foo:a123,456', 'URN:foo:a123,456'], result: true },
{ pair: ['urn:foo:a123,456', 'urn:FOO:a123,456'], result: true }
]

// Disabling for now as the whole equal logic might need
// to be refactored
// t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
// t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)

runTest(t, suite)
t.end()
})

test('UUID Equals', (t) => {
t.equal(URI.equal('URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'), true)
const suite = [
{ pair: ['URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'], result: true }
]

runTest(t, suite)
t.end()
})

// test('Mailto Equals', (t) => {
// // tests from RFC 6068
// t.equal(URI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true)
Expand All @@ -61,11 +86,19 @@ test('UUID Equals', (t) => {
// })

test('WS Equal', (t) => {
t.equal(URI.equal('WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'), true)
const suite = [
{ pair: ['WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'], result: true }
]

runTest(t, suite)
t.end()
})

test('WSS Equal', (t) => {
t.equal(URI.equal('WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'), true)
const suite = [
{ pair: ['WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'], result: true }
]

runTest(t, suite)
t.end()
})

0 comments on commit 8969aa1

Please sign in to comment.