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.

96 lines
2.4 KiB

  1. var hashish = require('hashish');
  2. var fs = require('fs');
  3. var findit = require('findit');
  4. var path = require('path');
  5. var http = require('http');
  6. var net = require('net');
  7. var assert = require('assert');
  8. var common = require('../common');
  9. var formidable = common.formidable;
  10. var server = http.createServer();
  11. server.listen(common.port, findFixtures);
  12. function findFixtures() {
  13. var fixtures = [];
  14. findit
  15. .sync(common.dir.fixture + '/js')
  16. .forEach(function(jsPath) {
  17. if (!/\.js$/.test(jsPath)) return;
  18. var group = path.basename(jsPath, '.js');
  19. hashish.forEach(require(jsPath), function(fixture, name) {
  20. fixtures.push({
  21. name : group + '/' + name,
  22. fixture : fixture,
  23. });
  24. });
  25. });
  26. testNext(fixtures);
  27. }
  28. function testNext(fixtures) {
  29. var fixture = fixtures.shift();
  30. if (!fixture) return server.close();
  31. var name = fixture.name;
  32. var fixture = fixture.fixture;
  33. uploadFixture(name, function(err, parts) {
  34. if (err) throw err;
  35. fixture.forEach(function(expectedPart, i) {
  36. var parsedPart = parts[i];
  37. assert.equal(parsedPart.type, expectedPart.type);
  38. assert.equal(parsedPart.name, expectedPart.name);
  39. if (parsedPart.type === 'file') {
  40. var file = parsedPart.value;
  41. assert.equal(file.name, expectedPart.filename);
  42. if(expectedPart.sha1) assert.equal(file.hash, expectedPart.sha1);
  43. }
  44. });
  45. testNext(fixtures);
  46. });
  47. };
  48. function uploadFixture(name, cb) {
  49. server.once('request', function(req, res) {
  50. var form = new formidable.IncomingForm();
  51. form.uploadDir = common.dir.tmp;
  52. form.hash = "sha1";
  53. form.parse(req);
  54. function callback() {
  55. var realCallback = cb;
  56. cb = function() {};
  57. realCallback.apply(null, arguments);
  58. }
  59. var parts = [];
  60. form
  61. .on('error', callback)
  62. .on('fileBegin', function(name, value) {
  63. parts.push({type: 'file', name: name, value: value});
  64. })
  65. .on('field', function(name, value) {
  66. parts.push({type: 'field', name: name, value: value});
  67. })
  68. .on('end', function() {
  69. res.end('OK');
  70. callback(null, parts);
  71. });
  72. });
  73. var socket = net.createConnection(common.port);
  74. var file = fs.createReadStream(common.dir.fixture + '/http/' + name);
  75. file.pipe(socket, {end: false});
  76. socket.on('data', function () {
  77. socket.end();
  78. });
  79. }