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.

62 lines
1.5 KiB

8 years ago
  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. var assert = require('assert'),
  9. fs = require('fs'),
  10. path = require('path'),
  11. request = require('request'),
  12. vows = require('vows'),
  13. union = require('../lib/index'),
  14. macros = require('./helpers/macros');
  15. var doubleWrite = false,
  16. server;
  17. server = union.createServer({
  18. before: [
  19. function (req, res) {
  20. res.json(200, { 'hello': 'world' });
  21. res.emit('next');
  22. },
  23. function (req, res) {
  24. doubleWrite = true;
  25. res.json(200, { 'hello': 'world' });
  26. res.emit('next');
  27. }
  28. ]
  29. });
  30. vows.describe('union/double-write').addBatch({
  31. "When using union": {
  32. "an http server which attempts to write to the response twice": {
  33. topic: function () {
  34. server.listen(9091, this.callback);
  35. },
  36. "a GET request to `/foo`": {
  37. topic: function () {
  38. request({ uri: 'http://localhost:9091/foo' }, this.callback);
  39. },
  40. "it should respond with `{ 'hello': 'world' }`": function (err, res, body) {
  41. macros.assertValidResponse(err, res);
  42. assert.deepEqual(JSON.parse(body), { 'hello': 'world' });
  43. },
  44. "it should not write to the response twice": function () {
  45. assert.isFalse(doubleWrite);
  46. }
  47. }
  48. }
  49. }
  50. }).addBatch({
  51. "When the tests are over": {
  52. "the server should close": function () {
  53. server.close();
  54. }
  55. }
  56. }).export(module);