|
| 1 | +/* |
| 2 | +test return http request, added for issue #47: |
| 3 | +https://github.com/felixge/node-form-data/issues/47 |
| 4 | +
|
| 5 | +Checking correct length header and request object |
| 6 | +*/ |
| 7 | + |
| 8 | +var common = require('../common'); |
| 9 | +var assert = common.assert; |
| 10 | +var http = require('http'); |
| 11 | + |
| 12 | +var FormData = require(common.dir.lib + '/form_data'); |
| 13 | + |
| 14 | +var CRLF = '\r\n'; |
| 15 | + |
| 16 | +var expectedLength; |
| 17 | + |
| 18 | +var dataSize = 1000000; |
| 19 | + |
| 20 | +var server = http.createServer(function(req, res) { |
| 21 | + var data = '', uploaded = 0; |
| 22 | +console.log(['R', req.headers['content-length']]); |
| 23 | + assert.ok( typeof req.headers['content-length'] !== 'undefined' ); |
| 24 | + assert.equal(req.headers['content-length'], expectedLength); |
| 25 | + |
| 26 | + // check for uploaded body |
| 27 | + req.on('data', function(data) { |
| 28 | + uploaded += data.length; |
| 29 | + }); |
| 30 | + req.on('end', function() |
| 31 | + { |
| 32 | + // compare uploaded total to the expected length |
| 33 | + assert.equal(uploaded, expectedLength); |
| 34 | + |
| 35 | + res.writeHead(200); |
| 36 | + res.end('done'); |
| 37 | + }); |
| 38 | + |
| 39 | +}); |
| 40 | + |
| 41 | + |
| 42 | +server.listen(common.port, function() { |
| 43 | + var R, oWrite, progress = 0, form = new FormData(); |
| 44 | + |
| 45 | + var bufferData = []; |
| 46 | + for (var z = 0; z < dataSize; z++) { |
| 47 | + bufferData.push(1); |
| 48 | + } |
| 49 | + var buffer = new Buffer(bufferData); |
| 50 | + |
| 51 | + form.append('my_buffer', buffer); |
| 52 | + |
| 53 | + // (available to req handler) |
| 54 | + expectedLength = form._lastBoundary().length + form._overheadLength + dataSize; |
| 55 | + |
| 56 | + R = form.submit('http://localhost:' + common.port + '/', function(err, res) { |
| 57 | + if (err) { |
| 58 | + throw err; |
| 59 | + } |
| 60 | + |
| 61 | + assert.strictEqual(res.statusCode, 200); |
| 62 | + |
| 63 | + // unstuck new streams |
| 64 | + res.resume(); |
| 65 | + |
| 66 | + server.close(); |
| 67 | + |
| 68 | + // compare progress total to the expected length |
| 69 | + assert.equal(progress, expectedLength); |
| 70 | + }); |
| 71 | + |
| 72 | + // augment into request |
| 73 | + oWrite = R.write; |
| 74 | + R.write = function(chunk) { |
| 75 | + return oWrite.call(this, chunk, function() { |
| 76 | + form.emit('progress', chunk); |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + // track progres |
| 81 | + form.on('progress', function(chunk) { |
| 82 | + progress += chunk.length; |
| 83 | + assert.ok(progress <= expectedLength); |
| 84 | + }); |
| 85 | + |
| 86 | +}); |
0 commit comments