arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/www/node_modules/union/test/double-write-test.js
www/node_modules/union/test/double-write-test.jsRaw
 1/*
 2 * simple-test.js: Simple tests for basic streaming and non-streaming HTTP requests with union.
 3 *
 4 * (C) 2011, Charlie Robbins & the Contributors
 5 * MIT LICENSE
 6 *
 7 */
 8
 9var assert = require('assert'),
10    fs = require('fs'),
11    path = require('path'),
12    request = require('request'),
13    vows = require('vows'),
14    union = require('../lib/index'),
15    macros = require('./helpers/macros');
16
17var doubleWrite = false,
18    server;
19
20server = union.createServer({
21  before: [
22    function (req, res) {
23      res.json(200, { 'hello': 'world' });
24      res.emit('next');
25    },
26    function (req, res) {
27      doubleWrite = true;
28      res.json(200, { 'hello': 'world' });
29      res.emit('next');
30    }
31  ]
32});
33
34
35vows.describe('union/double-write').addBatch({
36  "When using union": {
37    "an http server which attempts to write to the response twice": {
38      topic: function () {
39        server.listen(9091, this.callback);
40      },
41      "a GET request to `/foo`": {
42        topic: function () {
43          request({ uri: 'http://localhost:9091/foo' }, this.callback);
44        },
45        "it should respond with `{ 'hello': 'world' }`": function (err, res, body) {
46          macros.assertValidResponse(err, res);
47          assert.deepEqual(JSON.parse(body), { 'hello': 'world' });
48        },
49        "it should not write to the response twice": function () {
50          assert.isFalse(doubleWrite);
51        }
52      }
53    }
54  }
55}).addBatch({
56  "When the tests are over": {
57    "the server should close": function () {
58      server.close();
59    }
60  }
61}).export(module);
62