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