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.

49 lines
1.3 KiB

  1. var http = require('http'),
  2. formidable = require('../../lib/index'),
  3. request = require('request'),
  4. assert = require('assert');
  5. var host = 'localhost';
  6. var index = [
  7. '<form action="/" method="post" enctype="multipart/form-data">',
  8. ' <input type="text" name="foo" />',
  9. ' <input type="submit" />',
  10. '</form>'
  11. ].join("\n");
  12. var server = http.createServer(function(req, res) {
  13. // Show a form for testing purposes.
  14. if (req.method == 'GET') {
  15. res.writeHead(200, {'content-type': 'text/html'});
  16. res.end(index);
  17. return;
  18. }
  19. // Parse form and write results to response.
  20. var form = new formidable.IncomingForm();
  21. form.parse(req, function(err, fields, files) {
  22. res.writeHead(200, {'content-type': 'text/plain'});
  23. res.write(JSON.stringify({err: err, fields: fields, files: files}));
  24. res.end();
  25. });
  26. }).listen(0, host, function() {
  27. console.log("Server up and running...");
  28. var server = this,
  29. url = 'http://' + host + ':' + server.address().port;
  30. var parts = [
  31. {'Content-Disposition': 'form-data; name="foo"', 'body': 'bar'}
  32. ]
  33. var req = request({method: 'POST', url: url, multipart: parts}, function(e, res, body) {
  34. var obj = JSON.parse(body);
  35. assert.equal("bar", obj.fields.foo);
  36. server.close();
  37. });
  38. });