test-utils-parse-params.js 3.7 KB

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