You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

  1. var common = require('../common');
  2. var test = require('utest');
  3. var assert = common.assert;
  4. var IncomingForm = common.require('incoming_form').IncomingForm;
  5. var path = require('path');
  6. var form;
  7. test('IncomingForm', {
  8. before: function() {
  9. form = new IncomingForm();
  10. },
  11. '#_fileName with regular characters': function() {
  12. var filename = 'foo.txt';
  13. assert.equal(form._fileName(makeHeader(filename)), 'foo.txt');
  14. },
  15. '#_fileName with unescaped quote': function() {
  16. var filename = 'my".txt';
  17. assert.equal(form._fileName(makeHeader(filename)), 'my".txt');
  18. },
  19. '#_fileName with escaped quote': function() {
  20. var filename = 'my%22.txt';
  21. assert.equal(form._fileName(makeHeader(filename)), 'my".txt');
  22. },
  23. '#_fileName with bad quote and additional sub-header': function() {
  24. var filename = 'my".txt';
  25. var header = makeHeader(filename) + '; foo="bar"';
  26. assert.equal(form._fileName(header), filename);
  27. },
  28. '#_fileName with semicolon': function() {
  29. var filename = 'my;.txt';
  30. assert.equal(form._fileName(makeHeader(filename)), 'my;.txt');
  31. },
  32. '#_fileName with utf8 character': function() {
  33. var filename = 'my☃.txt';
  34. assert.equal(form._fileName(makeHeader(filename)), 'my☃.txt');
  35. },
  36. '#_uploadPath strips harmful characters from extension when keepExtensions': function() {
  37. form.keepExtensions = true;
  38. var ext = path.extname(form._uploadPath('fine.jpg?foo=bar'));
  39. assert.equal(ext, '.jpg');
  40. var ext = path.extname(form._uploadPath('fine?foo=bar'));
  41. assert.equal(ext, '');
  42. var ext = path.extname(form._uploadPath('super.cr2+dsad'));
  43. assert.equal(ext, '.cr2');
  44. var ext = path.extname(form._uploadPath('super.bar'));
  45. assert.equal(ext, '.bar');
  46. },
  47. });
  48. function makeHeader(filename) {
  49. return 'Content-Disposition: form-data; name="upload"; filename="' + filename + '"';
  50. }