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.

97 lines
3.1 KiB

7 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. spawn = require('child_process').spawn,
  12. request = require('request'),
  13. vows = require('vows'),
  14. macros = require('./helpers/macros');
  15. var examplesDir = path.join(__dirname, '..', 'examples', 'simple'),
  16. simpleScript = path.join(examplesDir, 'simple.js'),
  17. pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8')),
  18. fooURI = 'http://localhost:9090/foo',
  19. server;
  20. vows.describe('union/simple').addBatch({
  21. "When using union": {
  22. "a simple http server": {
  23. topic: function () {
  24. server = spawn(process.argv[0], [simpleScript]);
  25. server.stdout.on('data', this.callback.bind(this, null));
  26. },
  27. "a GET request to `/foo`": {
  28. topic: function () {
  29. request({ uri: fooURI }, this.callback);
  30. },
  31. "it should respond with `hello world`": function (err, res, body) {
  32. macros.assertValidResponse(err, res);
  33. assert.equal(body, 'hello world\n');
  34. },
  35. "it should respond with 'x-powered-by': 'union <version>'": function (err, res, body) {
  36. assert.isNull(err);
  37. assert.equal(res.headers['x-powered-by'], 'union ' + pkg.version);
  38. }
  39. },
  40. "a POST request to `/foo`": {
  41. topic: function () {
  42. request.post({ uri: fooURI }, this.callback);
  43. },
  44. "it should respond with `wrote to a stream!`": function (err, res, body) {
  45. macros.assertValidResponse(err, res);
  46. assert.equal(body, 'wrote to a stream!');
  47. }
  48. },
  49. "a GET request to `/redirect`": {
  50. topic: function () {
  51. request.get({
  52. url: 'http://localhost:9090/redirect',
  53. followRedirect: false
  54. }, this.callback);
  55. },
  56. "it should redirect to `http://www.google.com`": function (err, res, body) {
  57. assert.equal(res.statusCode, 302);
  58. assert.equal(res.headers.location, "http://www.google.com");
  59. }
  60. },
  61. "a GET request to `/custom_redirect`": {
  62. topic: function () {
  63. request.get({
  64. url: 'http://localhost:9090/custom_redirect',
  65. followRedirect: false
  66. }, this.callback);
  67. },
  68. "it should redirect to `/foo`": function (err, res, body) {
  69. assert.equal(res.statusCode, 301);
  70. assert.equal(res.headers.location, "http://localhost:9090/foo");
  71. }
  72. },
  73. "a GET request to `/async`": {
  74. topic: function () {
  75. request.get({
  76. url: 'http://localhost:9090/async',
  77. timeout: 500
  78. }, this.callback);
  79. },
  80. "it should not timeout": function (err, res, body) {
  81. assert.ifError(err);
  82. assert.equal(res.statusCode, 200);
  83. }
  84. }
  85. }
  86. }
  87. }).addBatch({
  88. "When the tests are over": {
  89. "the server should close": function () {
  90. server.kill();
  91. }
  92. }
  93. }).export(module);