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