summaryrefslogtreecommitdiffstats
path: root/emacs.d/lisp/jshint-mode/node_modules/formidable/test/system/test-multi-video-upload.js
blob: fcfdb94fc8983237fabb170b9ae267c89cc49661 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var common = require('../common');
var BOUNDARY = '---------------------------10102754414578508781458777923',
    FIXTURE = TEST_FIXTURES+'/multi_video.upload',
    fs = require('fs'),
    util = require(common.lib + '/util'),
    http = require('http'),
    formidable = require(common.lib + '/index'),
    server = http.createServer();

server.on('request', function(req, res) {
  var form = new formidable.IncomingForm(),
      uploads = {};

  form.uploadDir = TEST_TMP;
  form.parse(req);

  form
    .on('fileBegin', function(field, file) {
      assert.equal(field, 'upload');

      var tracker = {file: file, progress: [], ended: false};
      uploads[file.filename] = tracker;
      file
        .on('progress', function(bytesReceived) {
          tracker.progress.push(bytesReceived);
          assert.equal(bytesReceived, file.length);
        })
        .on('end', function() {
          tracker.ended = true;
        });
    })
    .on('field', function(field, value) {
      assert.equal(field, 'title');
      assert.equal(value, '');
    })
    .on('file', function(field, file) {
      assert.equal(field, 'upload');
      assert.strictEqual(uploads[file.filename].file, file);
    })
    .on('end', function() {
      assert.ok(uploads['shortest_video.flv']);
      assert.ok(uploads['shortest_video.flv'].ended);
      assert.ok(uploads['shortest_video.flv'].progress.length > 3);
      assert.equal(uploads['shortest_video.flv'].progress.slice(-1), uploads['shortest_video.flv'].file.length);
      assert.ok(uploads['shortest_video.mp4']);
      assert.ok(uploads['shortest_video.mp4'].ended);
      assert.ok(uploads['shortest_video.mp4'].progress.length > 3);

      server.close();
      res.writeHead(200);
      res.end('good');
    });
});

server.listen(TEST_PORT, function() {
  var client = http.createClient(TEST_PORT),
      stat = fs.statSync(FIXTURE),
      headers = {
        'content-type': 'multipart/form-data; boundary='+BOUNDARY,
        'content-length': stat.size,
      }
      request = client.request('POST', '/', headers),
      fixture = new fs.ReadStream(FIXTURE);

  fixture
    .on('data', function(b) {
      request.write(b);
    })
    .on('end', function() {
      request.end();
    });
});