[79a0317] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const test = require('tape')
|
---|
| 4 | const fastifyURI = require('../')
|
---|
| 5 | const urijs = require('uri-js')
|
---|
| 6 |
|
---|
| 7 | test('compatibility Parse', (t) => {
|
---|
| 8 | const toParse = [
|
---|
| 9 | '//www.g.com/error\n/bleh/bleh',
|
---|
| 10 | 'https://fastify.org',
|
---|
| 11 | '/definitions/Record%3Cstring%2CPerson%3E',
|
---|
| 12 | '//10.10.10.10',
|
---|
| 13 | // '//10.10.000.10', <-- not a valid URI per URI spec: https://datatracker.ietf.org/doc/html/rfc5954#section-4.1
|
---|
| 14 | '//[2001:db8::7%en0]',
|
---|
| 15 | '//[2001:dbZ::1]:80',
|
---|
| 16 | '//[2001:db8::1]:80',
|
---|
| 17 | '//[2001:db8::001]:80',
|
---|
| 18 | 'uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body',
|
---|
| 19 | 'http://user:pass@example.com:123/one/space in.url?q1=a1&q2=a2#body',
|
---|
| 20 | 'http://User:Pass@example.com:123/one/space in.url?q1=a1&q2=a2#body',
|
---|
| 21 | 'http://A%3AB@example.com:123/one/space',
|
---|
| 22 | '//[::ffff:129.144.52.38]',
|
---|
| 23 | 'uri://10.10.10.10.example.com/en/process',
|
---|
| 24 | '//[2606:2800:220:1:248:1893:25c8:1946]/test',
|
---|
| 25 | 'ws://example.com/chat',
|
---|
| 26 | 'ws://example.com/foo?bar=baz',
|
---|
| 27 | 'wss://example.com/?bar=baz',
|
---|
| 28 | 'wss://example.com/chat',
|
---|
| 29 | 'wss://example.com/foo?bar=baz',
|
---|
| 30 | 'wss://example.com/?bar=baz',
|
---|
| 31 | 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
|
---|
| 32 | 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6',
|
---|
| 33 | 'urn:example:%D0%B0123,z456',
|
---|
| 34 | '//[2606:2800:220:1:248:1893:25c8:1946:43209]',
|
---|
| 35 | 'http://foo.bar',
|
---|
| 36 | 'http://',
|
---|
| 37 | '#/$defs/stringMap',
|
---|
| 38 | '#/$defs/string%20Map',
|
---|
| 39 | '#/$defs/string Map',
|
---|
| 40 | '//?json=%7B%22foo%22%3A%22bar%22%7D'
|
---|
| 41 | // 'mailto:chris@example.com'-203845,
|
---|
| 42 | // 'mailto:infobot@example.com?subject=current-issue',
|
---|
| 43 | // 'mailto:infobot@example.com?body=send%20current-issue',
|
---|
| 44 | // 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index',
|
---|
| 45 | // 'mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E',
|
---|
| 46 | // 'mailto:majordomo@example.com?body=subscribe%20bamboo-l',
|
---|
| 47 | // 'mailto:joe@example.com?cc=bob@example.com&body=hello',
|
---|
| 48 | // 'mailto:gorby%25kremvax@example.com',
|
---|
| 49 | // 'mailto:unlikely%3Faddress@example.com?blat=foop',
|
---|
| 50 | // 'mailto:Mike%26family@example.org',
|
---|
| 51 | // 'mailto:%22not%40me%22@example.org',
|
---|
| 52 | // 'mailto:%22oh%5C%5Cno%22@example.org',
|
---|
| 53 | // 'mailto:%22%5C%5C%5C%22it\'s%5C%20ugly%5C%5C%5C%22%22@example.org',
|
---|
| 54 | // 'mailto:user@example.org?subject=caf%C3%A9',
|
---|
| 55 | // 'mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D',
|
---|
| 56 | // 'mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D',
|
---|
| 57 | // 'mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9',
|
---|
| 58 | // 'mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO'
|
---|
| 59 | ]
|
---|
| 60 | toParse.forEach((x) => {
|
---|
| 61 | t.same(fastifyURI.parse(x), urijs.parse(x), 'Compatibility parse: ' + x)
|
---|
| 62 | })
|
---|
| 63 | t.end()
|
---|
| 64 | })
|
---|
| 65 |
|
---|
| 66 | test('compatibility serialize', (t) => {
|
---|
| 67 | const toSerialize = [
|
---|
| 68 | { host: '10.10.10.10.example.com' },
|
---|
| 69 | { host: '2001:db8::7' },
|
---|
| 70 | { host: '::ffff:129.144.52.38' },
|
---|
| 71 | { host: '2606:2800:220:1:248:1893:25c8:1946' },
|
---|
| 72 | { host: '10.10.10.10.example.com' },
|
---|
| 73 | { host: '10.10.10.10' },
|
---|
| 74 | { path: '?query' },
|
---|
| 75 | { path: 'foo:bar' },
|
---|
| 76 | { path: '//path' },
|
---|
| 77 | {
|
---|
| 78 | scheme: 'uri',
|
---|
| 79 | host: 'example.com',
|
---|
| 80 | port: '9000'
|
---|
| 81 | },
|
---|
| 82 | {
|
---|
| 83 | scheme: 'uri',
|
---|
| 84 | userinfo: 'foo:bar',
|
---|
| 85 | host: 'example.com',
|
---|
| 86 | port: 1,
|
---|
| 87 | path: 'path',
|
---|
| 88 | query: 'query',
|
---|
| 89 | fragment: 'fragment'
|
---|
| 90 | },
|
---|
| 91 | {
|
---|
| 92 | scheme: '',
|
---|
| 93 | userinfo: '',
|
---|
| 94 | host: '',
|
---|
| 95 | port: 0,
|
---|
| 96 | path: '',
|
---|
| 97 | query: '',
|
---|
| 98 | fragment: ''
|
---|
| 99 | },
|
---|
| 100 | {
|
---|
| 101 | scheme: undefined,
|
---|
| 102 | userinfo: undefined,
|
---|
| 103 | host: undefined,
|
---|
| 104 | port: undefined,
|
---|
| 105 | path: undefined,
|
---|
| 106 | query: undefined,
|
---|
| 107 | fragment: undefined
|
---|
| 108 | },
|
---|
| 109 | { host: 'fe80::a%en1' },
|
---|
| 110 | { host: 'fe80::a%25en1' },
|
---|
| 111 | {
|
---|
| 112 | scheme: 'ws',
|
---|
| 113 | host: 'example.com',
|
---|
| 114 | resourceName: '/foo?bar',
|
---|
| 115 | secure: true
|
---|
| 116 | },
|
---|
| 117 | {
|
---|
| 118 | scheme: 'scheme',
|
---|
| 119 | path: 'with:colon'
|
---|
| 120 | }
|
---|
| 121 | ]
|
---|
| 122 | toSerialize.forEach((x) => {
|
---|
| 123 | const r = JSON.stringify(x)
|
---|
| 124 | t.same(
|
---|
| 125 | fastifyURI.serialize(x),
|
---|
| 126 | urijs.serialize(x),
|
---|
| 127 | 'Compatibility serialize: ' + JSON.stringify(r)
|
---|
| 128 | )
|
---|
| 129 | })
|
---|
| 130 | t.end()
|
---|
| 131 | })
|
---|