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.

48 lines
1.3 KiB

  1. var assert = require('assert');
  2. var common = require('../common');
  3. var formidable = require('../../lib/index');
  4. var http = require('http');
  5. var server = http.createServer(function(req, res) {
  6. var form = new formidable.IncomingForm();
  7. form.uploadDir = common.dir.tmp;
  8. form.on('end', function () {
  9. throw new Error('Unexpected "end" event');
  10. });
  11. form.on('error', function (e) {
  12. res.writeHead(500);
  13. res.end(e.message);
  14. });
  15. form.parse(req);
  16. });
  17. server.listen(0, function() {
  18. var body =
  19. '--foo\r\n' +
  20. 'Content-Disposition: form-data; name="file1"; filename="file1"\r\n' +
  21. 'Content-Type: application/octet-stream\r\n' +
  22. '\r\nThis is the first file\r\n' +
  23. '--foo\r\n' +
  24. 'Content-Type: application/octet-stream\r\n' +
  25. 'Content-Disposition: form-data; name="file2"; filename="file2"\r\n' +
  26. 'Content-Transfer-Encoding: unknown\r\n' +
  27. '\r\nThis is the second file\r\n' +
  28. '--foo--\r\n';
  29. var req = http.request({
  30. method: 'POST',
  31. port: server.address().port,
  32. headers: {
  33. 'Content-Length': body.length,
  34. 'Content-Type': 'multipart/form-data; boundary=foo'
  35. }
  36. });
  37. req.on('response', function (res) {
  38. assert.equal(res.statusCode, 500);
  39. res.on('data', function () {});
  40. res.on('end', function () {
  41. server.close();
  42. });
  43. });
  44. req.end(body);
  45. });