socketioMEANseed/www/node_modules/http-server/test/http-server-test.js
www/node_modules/http-server/test/http-server-test.jsRaw
1var assert = require('assert'),
2 path = require('path'),
3 fs = require('fs'),
4 vows = require('vows'),
5 request = require('request'),
6 httpServer = require('../lib/http-server');
7
8var root = path.join(__dirname, 'fixtures', 'root');
9
10vows.describe('http-server').addBatch({
11 'When http-server is listening on 8080': {
12 topic: function () {
13 var server = httpServer.createServer({
14 root: root,
15 robots: true,
16 headers: {
17 'Access-Control-Allow-Origin': '*',
18 'Access-Control-Allow-Credentials': 'true'
19 }
20 });
21
22 server.listen(8080);
23 this.callback(null, server);
24 },
25 'it should serve files from root directory': {
26 topic: function () {
27 request('http://127.0.0.1:8080/file', this.callback);
28 },
29 'status code should be 200': function (res) {
30 assert.equal(res.statusCode, 200);
31 },
32 'and file content': {
33 topic: function (res, body) {
34 var self = this;
35 fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
36 self.callback(err, data, body);
37 });
38 },
39 'should match content of served file': function (err, file, body) {
40 assert.equal(body.trim(), file.trim());
41 }
42 }
43 },
44 'when requesting non-existent file': {
45 topic: function () {
46 request('http://127.0.0.1:8080/404', this.callback);
47 },
48 'status code should be 404': function (res) {
49 assert.equal(res.statusCode, 404);
50 }
51 },
52 'when requesting /': {
53 topic: function () {
54 request('http://127.0.0.1:8080/', this.callback);
55 },
56 'should respond with index': function (err, res, body) {
57 assert.equal(res.statusCode, 200);
58 assert.include(body, '/file');
59 assert.include(body, '/canYouSeeMe');
60 }
61 },
62 'when robots options is activated': {
63 topic: function () {
64 request('http://127.0.0.1:8080/', this.callback);
65 },
66 'should respond with status code 200 to /robots.txt': function (res) {
67 assert.equal(res.statusCode, 200);
68 }
69 },
70 'and options include custom set http-headers': {
71 topic: function () {
72 request('http://127.0.0.1:8080/', this.callback);
73 },
74 'should respond with headers set in options': function (err, res) {
75 assert.equal(res.headers['access-control-allow-origin'], '*');
76 assert.equal(res.headers['access-control-allow-credentials'], 'true');
77 }
78 },
79 'When http-server is proxying from 8081 to 8080': {
80 topic: function () {
81 var proxyServer = httpServer.createServer({
82 proxy: 'http://127.0.0.1:8080/',
83 root: path.join(__dirname, 'fixtures')
84 });
85 proxyServer.listen(8081);
86 this.callback(null, proxyServer);
87 },
88 'it should serve files from the proxy server root directory': {
89 topic: function () {
90 request('http://127.0.0.1:8081/root/file', this.callback);
91 },
92 'status code should be the enpoint code 200': function (res) {
93 assert.equal(res.statusCode, 200);
94 },
95 'and file content': {
96 topic: function (res, body) {
97 var self = this;
98 fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
99 self.callback(err, data, body);
100 });
101 },
102 'should match content of the served file': function (err, file, body) {
103 assert.equal(body.trim(), file.trim());
104 }
105 }
106 },
107 'it should fallback to the proxied server': {
108 topic: function () {
109 request('http://127.0.0.1:8081/file', this.callback);
110 },
111 'status code should be the enpoint code 200': function (res) {
112 assert.equal(res.statusCode, 200);
113 },
114 'and file content': {
115 topic: function (res, body) {
116 var self = this;
117 fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
118 self.callback(err, data, body);
119 });
120 },
121 'should match content of the proxied served file': function (err, file, body) {
122 assert.equal(body.trim(), file.trim());
123 }
124 }
125 }
126 }
127 },
128 'When cors is enabled': {
129 topic: function () {
130 var server = httpServer.createServer({
131 root: root,
132 cors: true,
133 corsHeaders: 'X-Test'
134 });
135 server.listen(8082);
136 this.callback(null, server);
137 },
138 'and given OPTIONS request': {
139 topic: function () {
140 request({
141 method: 'OPTIONS',
142 uri: 'http://127.0.0.1:8082/',
143 headers: {
144 'Access-Control-Request-Method': 'GET',
145 Origin: 'http://example.com',
146 'Access-Control-Request-Headers': 'Foobar'
147 }
148 }, this.callback);
149 },
150 'status code should be 204': function (err, res) {
151 assert.equal(res.statusCode, 204);
152 },
153 'response Access-Control-Allow-Headers should contain X-Test': function (err, res) {
154 assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
155 }
156 }
157 }
158}).export(module);