1 | var parseParams = require('../lib/utils').parseParams;
|
2 |
|
3 | var path = require('path'),
|
4 | assert = require('assert'),
|
5 | inspect = require('util').inspect;
|
6 |
|
7 | var group = path.basename(__filename, '.js') + '/';
|
8 |
|
9 | [
|
10 | { source: 'video/ogg',
|
11 | expected: ['video/ogg'],
|
12 | what: 'No parameters'
|
13 | },
|
14 | { source: 'video/ogg;',
|
15 | expected: ['video/ogg'],
|
16 | what: 'No parameters (with separator)'
|
17 | },
|
18 | { source: 'video/ogg; ',
|
19 | expected: ['video/ogg'],
|
20 | what: 'No parameters (with separator followed by whitespace)'
|
21 | },
|
22 | { source: ';video/ogg',
|
23 | expected: ['', 'video/ogg'],
|
24 | what: 'Empty parameter'
|
25 | },
|
26 | { source: 'video/*',
|
27 | expected: ['video/*'],
|
28 | what: 'Subtype with asterisk'
|
29 | },
|
30 | { source: 'text/plain; encoding=utf8',
|
31 | expected: ['text/plain', ['encoding', 'utf8']],
|
32 | what: 'Unquoted'
|
33 | },
|
34 | { source: 'text/plain; encoding=',
|
35 | expected: ['text/plain', ['encoding', '']],
|
36 | what: 'Unquoted empty string'
|
37 | },
|
38 | { source: 'text/plain; encoding="utf8"',
|
39 | expected: ['text/plain', ['encoding', 'utf8']],
|
40 | what: 'Quoted'
|
41 | },
|
42 | { source: 'text/plain; greeting="hello \\"world\\""',
|
43 | expected: ['text/plain', ['greeting', 'hello "world"']],
|
44 | what: 'Quotes within quoted'
|
45 | },
|
46 | { source: 'text/plain; encoding=""',
|
47 | expected: ['text/plain', ['encoding', '']],
|
48 | what: 'Quoted empty string'
|
49 | },
|
50 | { source: 'text/plain; encoding="utf8";\t foo=bar;test',
|
51 | expected: ['text/plain', ['encoding', 'utf8'], ['foo', 'bar'], 'test'],
|
52 | what: 'Multiple params with various spacing'
|
53 | },
|
54 | { source: "text/plain; filename*=iso-8859-1'en'%A3%20rates",
|
55 | expected: ['text/plain', ['filename', '£ rates']],
|
56 | what: 'Extended parameter (RFC 5987) with language'
|
57 | },
|
58 | { source: "text/plain; filename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
|
59 | expected: ['text/plain', ['filename', '£ and € rates']],
|
60 | what: 'Extended parameter (RFC 5987) without language'
|
61 | },
|
62 | { source: "text/plain; filename*=utf-8''%E6%B5%8B%E8%AF%95%E6%96%87%E6%A1%A3",
|
63 | expected: ['text/plain', ['filename', '测试文档']],
|
64 | what: 'Extended parameter (RFC 5987) without language #2'
|
65 | },
|
66 | { source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
|
67 | expected: ['text/plain', ['filename', '£ rates'], ['altfilename', '£ and € rates']],
|
68 | what: 'Multiple extended parameters (RFC 5987) with mixed charsets'
|
69 | },
|
70 | { source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename=\"foobarbaz\"",
|
71 | expected: ['text/plain', ['filename', '£ rates'], ['altfilename', 'foobarbaz']],
|
72 | what: 'Mixed regular and extended parameters (RFC 5987)'
|
73 | },
|
74 | { source: "text/plain; filename=\"foobarbaz\"; altfilename*=iso-8859-1'en'%A3%20rates",
|
75 | expected: ['text/plain', ['filename', 'foobarbaz'], ['altfilename', '£ rates']],
|
76 | what: 'Mixed regular and extended parameters (RFC 5987) #2'
|
77 | },
|
78 | { source: 'text/plain; filename="C:\\folder\\test.png"',
|
79 | expected: ['text/plain', ['filename', 'C:\\folder\\test.png']],
|
80 | what: 'Unescaped backslashes should be considered backslashes'
|
81 | },
|
82 | { source: 'text/plain; filename="John \\"Magic\\" Smith.png"',
|
83 | expected: ['text/plain', ['filename', 'John "Magic" Smith.png']],
|
84 | what: 'Escaped double-quotes should be considered double-quotes'
|
85 | },
|
86 | { source: 'multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY',
|
87 | expected: ['multipart/form-data', ['charset', 'utf-8'], ['boundary', '0xKhTmLbOuNdArY']],
|
88 | what: 'Multiple non-quoted parameters'
|
89 | },
|
90 | ].forEach(function(v) {
|
91 | var result = parseParams(v.source),
|
92 | msg = '[' + group + v.what + ']: parsed parameters mismatch.\n'
|
93 | + 'Saw: ' + inspect(result) + '\n'
|
94 | + 'Expected: ' + inspect(v.expected);
|
95 | assert.deepEqual(result, v.expected, msg);
|
96 | });
|