UNPKG

3.06 kBJavaScriptView Raw
1var parseHeader = require('../lib/Parser').parseHeader;
2
3var assert = require('assert'),
4 inspect = require('util').inspect;
5
6var CRLF = '\r\n';
7
8[
9 { source: ['To: Foo', CRLF,
10 ' Bar Baz', CRLF],
11 expected: { to: [ 'Foo Bar Baz' ] },
12 what: 'Folded header value (plain -- space)'
13 },
14 { source: ['To: Foo', CRLF,
15 '\tBar\tBaz', CRLF],
16 expected: { to: [ 'Foo\tBar\tBaz' ] },
17 what: 'Folded header value (plain -- tab)'
18 },
19 { source: ['Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?=', CRLF],
20 expected: { subject: [ '¡Hola, señor!' ] },
21 what: 'MIME encoded-word in value'
22 },
23 { source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
24 ' =?GB2312?Q?345678901234567890123456789012345678901234567890123456789012?=', CRLF,
25 ' =?GB2312?Q?345678901234567890?=', CRLF],
26 expected: { subject: [ '测试题目与中国信 long subjects are not OK 12345678901234567890123456789012345678901234567890123456789012345678901234567890' ] },
27 what: 'Folded header value (adjacent MIME encoded-words)'
28 },
29 { source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
30 ' 3=?GB2312?Q?45678901234567890123456789012345678901234567890123456789012?=', CRLF,
31 ' 3=?GB2312?Q?45678901234567890?=', CRLF],
32 expected: { subject: [ '测试题目与中国信 long subjects are not OK 12 345678901234567890123456789012345678901234567890123456789012 345678901234567890' ] },
33 what: 'Folded header value (non-adjacent MIME encoded-words)'
34 },
35 { source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
36 ' 3=?GB2312?Q?45678901234567890123456789012345678901234567890123456789012?=', CRLF,
37 ' =?GB2312?Q?345678901234567890?=', CRLF],
38 expected: { subject: [ '测试题目与中国信 long subjects are not OK 12 345678901234567890123456789012345678901234567890123456789012345678901234567890' ] },
39 what: 'Folded header value (one adjacent, one non-adjacent MIME encoded-words)'
40 },
41 // header with body
42 { source: ['Subject: test subject', CRLF,
43 'X-Another-Header: test', CRLF,
44 CRLF,
45 'This is body: Not a header', CRLF],
46 expected: { subject: [ 'test subject' ], 'x-another-header': [ 'test' ] },
47 what: 'Header with the body'
48 },
49
50].forEach(function(v) {
51 var result;
52
53 try {
54 result = parseHeader(v.source.join(''));
55 } catch (e) {
56 console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
57 return;
58 }
59
60 assert.deepEqual(result,
61 v.expected,
62 makeMsg(v.what,
63 'Result mismatch:'
64 + '\nParsed: ' + inspect(result, false, 10)
65 + '\nExpected: ' + inspect(v.expected, false, 10)
66 )
67 );
68});
69
70function makeMsg(what, msg) {
71 return '[' + what + ']: ' + msg;
72}
\No newline at end of file