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.

68 lines
1.8 KiB

7 years ago
  1. var assert = require('assert'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. request = require('request'),
  5. vows = require('vows'),
  6. union = require('../');
  7. vows.describe('union/streaming').addBatch({
  8. 'When using `union`': {
  9. 'a simple union server': {
  10. topic: function () {
  11. var self = this;
  12. union.createServer({
  13. buffer: false,
  14. before: [
  15. function (req, res, next) {
  16. var chunks = '';
  17. req.on('data', function (chunk) {
  18. chunks += chunk;
  19. });
  20. req.on('end', function () {
  21. self.callback(null, chunks);
  22. });
  23. }
  24. ]
  25. }).listen(9000, function () {
  26. request.post('http://localhost:9000').write('hello world');
  27. });
  28. },
  29. 'should receive complete POST data': function (chunks) {
  30. assert.equal(chunks, 'hello world');
  31. }
  32. },
  33. "a simple pipe to a file": {
  34. topic: function () {
  35. var self = this;
  36. union.createServer({
  37. before: [
  38. function (req, res, next) {
  39. var filename = path.join(__dirname, 'fixtures', 'pipe-write-test.txt'),
  40. writeStream = fs.createWriteStream(filename);
  41. req.pipe(writeStream);
  42. writeStream.on('close', function () {
  43. res.writeHead(200);
  44. fs.createReadStream(filename).pipe(res);
  45. });
  46. }
  47. ]
  48. }).listen(9044, function () {
  49. request({
  50. method: 'POST',
  51. uri: 'http://localhost:9044',
  52. body: 'hello world'
  53. }, self.callback);
  54. });
  55. },
  56. 'should receive complete POST data': function (err, res, body) {
  57. assert.equal(body, 'hello world');
  58. }
  59. }
  60. }
  61. }).export(module);