PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/request/tests/test-form.js

https://bitbucket.org/gagginaspinnata/todo-app-with-angularjs
JavaScript | 79 lines | 52 code | 19 blank | 8 comment | 12 complexity | be87c76894622ee67746f763598c01d5 MD5 | raw file
Possible License(s): Apache-2.0, MIT
 1var assert = require('assert')
 2var http = require('http');
 3var path = require('path');
 4var mime = require('mime');
 5var request = require('../main.js');
 6var fs = require('fs');
 7
 8var remoteFile = 'http://nodejs.org/images/logo.png';
 9
10var FIELDS = [
11  {name: 'my_field', value: 'my_value'},
12  {name: 'my_buffer', value: new Buffer([1, 2, 3])},
13  {name: 'my_file', value: fs.createReadStream(__dirname + '/unicycle.jpg')},
14  {name: 'remote_file', value: request(remoteFile) }
15];
16
17var server = http.createServer(function(req, res) {
18
19  // temp workaround
20  var data = '';
21  req.setEncoding('utf8');
22
23  req.on('data', function(d) {
24    data += d;
25  });
26
27  req.on('end', function() {
28    // check for the fields' traces
29
30    // 1st field : my_field
31    var field = FIELDS.shift();
32    assert.ok( data.indexOf('form-data; name="'+field.name+'"') != -1 );
33    assert.ok( data.indexOf(field.value) != -1 );
34
35    // 2nd field : my_buffer
36    var field = FIELDS.shift();
37    assert.ok( data.indexOf('form-data; name="'+field.name+'"') != -1 );
38    assert.ok( data.indexOf(field.value) != -1 );
39
40    // 3rd field : my_file
41    var field = FIELDS.shift();
42    assert.ok( data.indexOf('form-data; name="'+field.name+'"') != -1 );
43    assert.ok( data.indexOf('; filename="'+path.basename(field.value.path)+'"') != -1 );
44    // check for unicycle.jpg traces
45    assert.ok( data.indexOf('2005:06:21 01:44:12') != -1 );
46    assert.ok( data.indexOf('Content-Type: '+mime.lookup(field.value.path) ) != -1 );
47
48    // 4th field : remote_file
49    var field = FIELDS.shift();
50    assert.ok( data.indexOf('form-data; name="'+field.name+'"') != -1 );
51    assert.ok( data.indexOf('; filename="'+path.basename(field.value.path)+'"') != -1 );
52    // check for http://nodejs.org/images/logo.png traces
53    assert.ok( data.indexOf('ImageReady') != -1 );
54    assert.ok( data.indexOf('Content-Type: '+mime.lookup(remoteFile) ) != -1 );
55
56    res.writeHead(200);
57    res.end('done');
58
59  });
60
61
62});
63
64server.listen(8080, function() {
65
66  var req = request.post('http://localhost:8080/upload', function () {
67    server.close();
68  })
69  var form = req.form()
70  
71  FIELDS.forEach(function(field) {
72    form.append(field.name, field.value);
73  });
74
75});
76
77process.on('exit', function() {
78  assert.strictEqual(FIELDS.length, 0);
79});