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.

71 lines
2.3 KiB

  1. var common = require('../common');
  2. var BOUNDARY = '---------------------------10102754414578508781458777923',
  3. FIXTURE = TEST_FIXTURES+'/multi_video.upload',
  4. fs = require('fs'),
  5. http = require('http'),
  6. formidable = require(common.lib + '/index'),
  7. server = http.createServer();
  8. server.on('request', function(req, res) {
  9. var form = new formidable.IncomingForm(),
  10. uploads = {};
  11. form.uploadDir = TEST_TMP;
  12. form.hash = 'sha1';
  13. form.parse(req);
  14. form
  15. .on('fileBegin', function(field, file) {
  16. assert.equal(field, 'upload');
  17. var tracker = {file: file, progress: [], ended: false};
  18. uploads[file.name] = tracker;
  19. file
  20. .on('progress', function(bytesReceived) {
  21. tracker.progress.push(bytesReceived);
  22. assert.equal(bytesReceived, file.size);
  23. })
  24. .on('end', function() {
  25. tracker.ended = true;
  26. });
  27. })
  28. .on('field', function(field, value) {
  29. assert.equal(field, 'title');
  30. assert.equal(value, '');
  31. })
  32. .on('file', function(field, file) {
  33. assert.equal(field, 'upload');
  34. assert.strictEqual(uploads[file.name].file, file);
  35. })
  36. .on('end', function() {
  37. assert.ok(uploads['shortest_video.flv']);
  38. assert.ok(uploads['shortest_video.flv'].ended);
  39. assert.ok(uploads['shortest_video.flv'].progress.length > 3);
  40. assert.equal(uploads['shortest_video.flv'].file.hash, 'd6a17616c7143d1b1438ceeef6836d1a09186b3a');
  41. assert.equal(uploads['shortest_video.flv'].progress.slice(-1), uploads['shortest_video.flv'].file.size);
  42. assert.ok(uploads['shortest_video.mp4']);
  43. assert.ok(uploads['shortest_video.mp4'].ended);
  44. assert.ok(uploads['shortest_video.mp4'].progress.length > 3);
  45. assert.equal(uploads['shortest_video.mp4'].file.hash, '937dfd4db263f4887ceae19341dcc8d63bcd557f');
  46. server.close();
  47. res.writeHead(200);
  48. res.end('good');
  49. });
  50. });
  51. server.listen(TEST_PORT, function() {
  52. var stat, headers, request, fixture;
  53. stat = fs.statSync(FIXTURE);
  54. request = http.request({
  55. port: TEST_PORT,
  56. path: '/',
  57. method: 'POST',
  58. headers: {
  59. 'content-type': 'multipart/form-data; boundary='+BOUNDARY,
  60. 'content-length': stat.size,
  61. },
  62. });
  63. fs.createReadStream(FIXTURE).pipe(request);
  64. });