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