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