|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | + |
| 3 | +import { |
| 4 | + convertAddresses, |
| 5 | + generateBoundary, |
| 6 | + parseAddresses, |
| 7 | + normalizeHeaderKey, |
| 8 | + escapeHeaderArgument, |
| 9 | + encodeHeaderValue, |
| 10 | + buildHeaderValue, |
| 11 | + isPlainText |
| 12 | +} from './utils' |
| 13 | + |
| 14 | +describe('#convertAddresses', function () { |
| 15 | + it('should convert address object to a string', function () { |
| 16 | + expect(convertAddresses([{ |
| 17 | + name: 'Jõgeva Ants', |
| 18 | + address: 'ants@jõgeva.ee' |
| 19 | + }, { |
| 20 | + name: 'Composers', |
| 21 | + group: [{ |
| 22 | + address: 'sebu@example.com', |
| 23 | + name: 'Bach, Sebastian' |
| 24 | + }, { |
| 25 | + address: 'mozart@example.com', |
| 26 | + name: 'Mozzie' |
| 27 | + }] |
| 28 | + }])).to.equal('=?UTF-8?Q?J=C3=B5geva_Ants?= <ants@xn--jgeva-dua.ee>, Composers:"Bach, Sebastian" <sebu@example.com>, Mozzie <mozart@example.com>;') |
| 29 | + }) |
| 30 | + |
| 31 | + it('should keep ascii name as is', function () { |
| 32 | + expect(convertAddresses([{ |
| 33 | + name: 'O\'Vigala Sass', |
| 34 | + address: 'a@b.c' |
| 35 | + }])).to.equal('O\'Vigala Sass <a@b.c>') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should include name in quotes for special symbols', function () { |
| 39 | + expect(convertAddresses([{ |
| 40 | + name: 'Sass, Vigala', |
| 41 | + address: 'a@b.c' |
| 42 | + }])).to.equal('"Sass, Vigala" <a@b.c>') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should escape quotes', function () { |
| 46 | + expect(convertAddresses([{ |
| 47 | + name: '"Vigala Sass"', |
| 48 | + address: 'a@b.c' |
| 49 | + }])).to.equal('"\\"Vigala Sass\\"" <a@b.c>') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should mime encode unicode names', function () { |
| 53 | + expect(convertAddresses([{ |
| 54 | + name: '"Jõgeva Sass"', |
| 55 | + address: 'a@b.c' |
| 56 | + }])).to.equal('=?UTF-8?Q?=22J=C3=B5geva_Sass=22?= <a@b.c>') |
| 57 | + }) |
| 58 | +}) |
| 59 | + |
| 60 | +describe('isPlainText', function () { |
| 61 | + it('should return true', function () { |
| 62 | + expect(isPlainText('az09\t\r\n~!?')).to.be.true |
| 63 | + }) |
| 64 | + |
| 65 | + it('should return false on low bits', function () { |
| 66 | + expect(isPlainText('az09\n\x08!?')).to.be.false |
| 67 | + }) |
| 68 | + |
| 69 | + it('should return false on high bits', function () { |
| 70 | + expect(isPlainText('az09\nõ!?')).to.be.false |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('generateBoundary ', function () { |
| 75 | + it('should genereate boundary string', function () { |
| 76 | + const nodeId = 'abc' |
| 77 | + const rootBoundary = 'def' |
| 78 | + expect(generateBoundary(nodeId, rootBoundary)).to.equal('----sinikael-?=_abc-def') |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +describe('parseAddresses', function () { |
| 83 | + it('should normalize header key', function () { |
| 84 | + expect(parseAddresses('test address@example.com')).to.deep.equal([{ |
| 85 | + address: 'address@example.com', |
| 86 | + name: 'test' |
| 87 | + }]) |
| 88 | + |
| 89 | + expect(parseAddresses(['test address@example.com'])).to.deep.equal([{ |
| 90 | + address: 'address@example.com', |
| 91 | + name: 'test' |
| 92 | + }]) |
| 93 | + |
| 94 | + expect(parseAddresses([ |
| 95 | + ['test address@example.com'] |
| 96 | + ])).to.deep.equal([{ |
| 97 | + address: 'address@example.com', |
| 98 | + name: 'test' |
| 99 | + }]) |
| 100 | + |
| 101 | + expect(parseAddresses([{ |
| 102 | + address: 'address@example.com', |
| 103 | + name: 'test' |
| 104 | + }])).to.deep.equal([{ |
| 105 | + address: 'address@example.com', |
| 106 | + name: 'test' |
| 107 | + }]) |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe('normalizeHeaderKey', function () { |
| 112 | + it('should normalize header key', function () { |
| 113 | + expect(normalizeHeaderKey('key')).to.equal('Key') |
| 114 | + expect(normalizeHeaderKey('mime-vERSION')).to.equal('MIME-Version') |
| 115 | + expect(normalizeHeaderKey('-a-long-name')).to.equal('-A-Long-Name') |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +describe('escapeHeaderArgument', function () { |
| 120 | + it('should return original value if possible', function () { |
| 121 | + expect(escapeHeaderArgument('abc')).to.equal('abc') |
| 122 | + }) |
| 123 | + |
| 124 | + it('should use quotes', function () { |
| 125 | + expect(escapeHeaderArgument('abc "tere"')).to.equal('"abc \\"tere\\""') |
| 126 | + }) |
| 127 | +}) |
| 128 | + |
| 129 | +describe('encodeHeaderValue', function () { |
| 130 | + it('should do noting if possible', function () { |
| 131 | + expect(encodeHeaderValue('x-my', 'test value')).to.equal('test value') |
| 132 | + }) |
| 133 | + |
| 134 | + it('should encode non ascii characters', function () { |
| 135 | + expect(encodeHeaderValue('x-my', 'test jõgeva value')).to.equal('test =?UTF-8?B?asO1Z2V2YQ==?= value') |
| 136 | + }) |
| 137 | + |
| 138 | + it('should format references', function () { |
| 139 | + expect(encodeHeaderValue('references', 'abc def')).to.equal('<abc> <def>') |
| 140 | + expect(encodeHeaderValue('references', ['abc', 'def'])).to.equal('<abc> <def>') |
| 141 | + }) |
| 142 | + |
| 143 | + it('should format message-id', function () { |
| 144 | + expect(encodeHeaderValue('message-id', 'abc')).to.equal('<abc>') |
| 145 | + }) |
| 146 | + |
| 147 | + it('should format addresses', function () { |
| 148 | + expect(encodeHeaderValue('from', { |
| 149 | + name: 'the safewithme testuser', |
| 150 | + address: 'safewithme.testuser@jõgeva.com' |
| 151 | + })).to.equal('the safewithme testuser <safewithme.testuser@xn--jgeva-dua.com>') |
| 152 | + }) |
| 153 | +}) |
| 154 | + |
| 155 | +describe('buildHeaderValue', function () { |
| 156 | + it('should build header value', function () { |
| 157 | + expect(buildHeaderValue({ |
| 158 | + value: 'test' |
| 159 | + })).to.equal('test') |
| 160 | + }) |
| 161 | + it('should build header value with params', function () { |
| 162 | + expect(buildHeaderValue({ |
| 163 | + value: 'test', |
| 164 | + params: { |
| 165 | + a: 'b' |
| 166 | + } |
| 167 | + })).to.equal('test; a=b') |
| 168 | + }) |
| 169 | + it('should build header value with empty params', function () { |
| 170 | + expect(buildHeaderValue({ |
| 171 | + value: 'test', |
| 172 | + params: { |
| 173 | + a: ';' |
| 174 | + } |
| 175 | + })).to.equal('test; a=";"') |
| 176 | + }) |
| 177 | + it('should build header value with quotes in params', function () { |
| 178 | + expect(buildHeaderValue({ |
| 179 | + value: 'test', |
| 180 | + params: { |
| 181 | + a: ';"' |
| 182 | + } |
| 183 | + })).to.equal('test; a=";\\""') |
| 184 | + }) |
| 185 | + it('should build header value with multiple params', function () { |
| 186 | + expect(buildHeaderValue({ |
| 187 | + value: 'test', |
| 188 | + params: { |
| 189 | + a: 'b', |
| 190 | + c: 'd' |
| 191 | + } |
| 192 | + })).to.equal('test; a=b; c=d') |
| 193 | + }) |
| 194 | +}) |
0 commit comments