|
| 1 | +/* eslint-disable no-underscore-dangle */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const path = require('path'); |
| 6 | +// const assert = require('assert'); |
| 7 | +const Request = require('http').ClientRequest; |
| 8 | + |
| 9 | +// const test = require('utest'); |
| 10 | +const mod = require('../../src/index'); |
| 11 | + |
| 12 | +function getForm(name, opts) { |
| 13 | + return name === 'formidable' ? mod.formidable(opts) : new mod[name](opts); |
| 14 | +} |
| 15 | +function makeHeader(filename) { |
| 16 | + return `Content-Disposition: form-data; name="upload"; filename="${filename}"`; |
| 17 | +} |
| 18 | + |
| 19 | +['IncomingForm', 'Formidable', 'formidable'].forEach((name) => { |
| 20 | + test(`${name}#_fileName with regular characters`, () => { |
| 21 | + const filename = 'foo.txt'; |
| 22 | + const form = getForm(name); |
| 23 | + |
| 24 | + expect(form._fileName(makeHeader(filename))).toBe('foo.txt'); |
| 25 | + }); |
| 26 | + |
| 27 | + test(`${name}#_fileName with unescaped quote`, () => { |
| 28 | + const filename = 'my".txt'; |
| 29 | + const form = getForm(name); |
| 30 | + |
| 31 | + expect(form._fileName(makeHeader(filename))).toBe('my".txt'); |
| 32 | + }); |
| 33 | + |
| 34 | + test(`${name}#_fileName with escaped quote`, () => { |
| 35 | + const filename = 'my%22.txt'; |
| 36 | + const form = getForm(name); |
| 37 | + |
| 38 | + expect(form._fileName(makeHeader(filename))).toBe('my".txt'); |
| 39 | + }); |
| 40 | + |
| 41 | + test(`${name}#_fileName with bad quote and additional sub-header`, () => { |
| 42 | + const filename = 'my".txt'; |
| 43 | + const form = getForm(name); |
| 44 | + |
| 45 | + const header = `${makeHeader(filename)}; foo="bar"`; |
| 46 | + expect(form._fileName(header)).toBe(filename); |
| 47 | + }); |
| 48 | + |
| 49 | + test(`${name}#_fileName with semicolon`, () => { |
| 50 | + const filename = 'my;.txt'; |
| 51 | + const form = getForm(name); |
| 52 | + |
| 53 | + expect(form._fileName(makeHeader(filename))).toBe('my;.txt'); |
| 54 | + }); |
| 55 | + |
| 56 | + test(`${name}#_fileName with utf8 character`, () => { |
| 57 | + const filename = 'my☃.txt'; |
| 58 | + const form = getForm(name); |
| 59 | + |
| 60 | + expect(form._fileName(makeHeader(filename))).toBe('my☃.txt'); |
| 61 | + }); |
| 62 | + |
| 63 | + test(`${name}#_uploadPath strips harmful characters from extension when keepExtensions`, () => { |
| 64 | + const form = getForm(name, { keepExtensions: true }); |
| 65 | + |
| 66 | + let ext = path.extname(form._uploadPath('fine.jpg?foo=bar')); |
| 67 | + expect(ext).toBe('.jpg'); |
| 68 | + |
| 69 | + ext = path.extname(form._uploadPath('fine?foo=bar')); |
| 70 | + expect(ext).toBe(''); |
| 71 | + |
| 72 | + ext = path.extname(form._uploadPath('super.cr2+dsad')); |
| 73 | + expect(ext).toBe('.cr2'); |
| 74 | + |
| 75 | + ext = path.extname(form._uploadPath('super.bar')); |
| 76 | + expect(ext).toBe('.bar'); |
| 77 | + |
| 78 | + ext = path.extname(form._uploadPath('file.aAa')); |
| 79 | + expect(ext).toBe('.aAa'); |
| 80 | + }); |
| 81 | + |
| 82 | + test(`${name}#_Array parameters support`, () => { |
| 83 | + const form = getForm(name, { multiples: true }); |
| 84 | + |
| 85 | + const req = new Request(); |
| 86 | + req.headers = 'content-type: json; content-length:8'; |
| 87 | + form.parse(req, (error, fields) => { |
| 88 | + expect(Array.isArray(fields.a)).toBe(true); |
| 89 | + expect(fields.a[0]).toBe(1); |
| 90 | + expect(fields.a[1]).toBe(2); |
| 91 | + }); |
| 92 | + form.emit('field', 'a[]', 1); |
| 93 | + form.emit('field', 'a[]', 2); |
| 94 | + form.emit('end'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments