Websocket
[VSoRC/.git] / node_modules / node-static / test / integration / node-static-test.js
1 var vows    = require('vows')
2   , request = require('request')
3   , assert  = require('assert')
4   , static  = require('../../lib/node-static');
5
6 var fileServer  = new static.Server(__dirname + '/../fixtures');
7 var suite       = vows.describe('node-static');
8 var TEST_PORT   = 8080;
9 var TEST_SERVER = 'http://localhost:' + TEST_PORT;
10 var version     = static.version.join('.');
11 var server;
12 var callback;
13
14 headers = {
15   'requesting headers': {
16     topic : function(){
17       request.head(TEST_SERVER + '/index.html', this.callback);
18     }
19   }
20 }
21 headers['requesting headers']['should respond with node-static/' + version] = function(error, response, body){
22   assert.equal(response.headers['server'], 'node-static/' + version);
23 }
24
25 suite.addBatch({
26   'once an http server is listening with a callback': {
27     topic: function () {
28       server = require('http').createServer(function (request, response) {
29         fileServer.serve(request, response, function(err, result) {
30           if (callback)
31             callback(request, response, err, result);
32           else
33             request.end();
34         });
35       }).listen(TEST_PORT, this.callback)
36     },
37     'should be listening' : function(){
38       /* This test is necessary to ensure the topic execution.
39        * A topic without tests will be not executed */
40       assert.isTrue(true);
41     }
42   },
43 }).addBatch({
44     'streaming a 404 page': {
45       topic: function(){
46         callback = function(request, response, err, result) {
47           if (err) {
48             response.writeHead(err.status, err.headers);
49             setTimeout(function() {
50               response.end('Custom 404 Stream.')
51             }, 100);
52           }
53         }
54         request.get(TEST_SERVER + '/not-found', this.callback);
55       },
56       'should respond with 404' : function(error, response, body){
57         assert.equal(response.statusCode, 404);
58       },
59       'should respond with the streamed content': function(error, response, body){
60         callback = null;
61         assert.equal(body, 'Custom 404 Stream.');
62       }
63     }
64 }).addBatch({
65   'once an http server is listening without a callback': {
66     topic: function () {
67       server.close();
68       server = require('http').createServer(function (request, response) {
69         fileServer.serve(request, response);
70       }).listen(TEST_PORT, this.callback)
71     },
72     'should be listening' : function(){
73       /* This test is necessary to ensure the topic execution.
74        * A topic without tests will be not executed */
75       assert.isTrue(true);
76     }
77   }
78 }).addBatch({
79     'requesting a file not found': {
80       topic : function(){
81         request.get(TEST_SERVER + '/not-found', this.callback);
82       },
83       'should respond with 404' : function(error, response, body){
84         assert.equal(response.statusCode, 404);
85       }
86     }
87 })
88 .addBatch({
89     'requesting a malformed URI': {
90       topic: function(){
91         request.get(TEST_SERVER + '/a%AFc', this.callback);
92       },
93       'should respond with 400': function(error, response, body){
94         assert.equal(response.statusCode, 400);
95       }
96     }
97 })
98 .addBatch({
99   'serving empty.css': {
100     topic : function(){
101       request.get(TEST_SERVER + '/empty.css', this.callback);
102     },
103     'should respond with 200' : function(error, response, body){
104       assert.equal(response.statusCode, 200);
105     },
106     'should respond with text/css': function(error, response, body){
107       assert.equal(response.headers['content-type'], 'text/css');
108     },
109     'should respond with empty string': function(error, response, body){
110       assert.equal(body, '');
111     }
112   }
113 })
114 .addBatch({
115   'serving hello.txt': {
116     topic : function(){
117       request.get(TEST_SERVER + '/hello.txt', this.callback);
118     },
119     'should respond with 200' : function(error, response, body){
120       assert.equal(response.statusCode, 200);
121     },
122     'should respond with text/plain': function(error, response, body){
123       assert.equal(response.headers['content-type'], 'text/plain');
124     },
125     'should respond with hello world': function(error, response, body){
126       assert.equal(body, 'hello world');
127     }
128   }
129 }).addBatch({
130   'serving first 5 bytes of hello.txt': {
131     topic : function(){
132       var options = {
133         url: TEST_SERVER + '/hello.txt',
134         headers: {
135           'Range': 'bytes=0-4'
136         }
137       };
138       request.get(options, this.callback);
139     },
140     'should respond with 206' : function(error, response, body){
141       assert.equal(response.statusCode, 206);
142     },
143     'should respond with text/plain': function(error, response, body){
144       assert.equal(response.headers['content-type'], 'text/plain');
145     },
146     'should have content-length of 5 bytes': function(error, response, body){
147       assert.equal(response.headers['content-length'], 5);
148     },
149     'should have a valid Content-Range header in response': function(error, response, body){
150       assert.equal(response.headers['content-range'], 'bytes 0-4/11');
151     },
152     'should respond with hello': function(error, response, body){
153       assert.equal(body, 'hello');
154     }
155   }
156 }).addBatch({
157   'serving last 5 bytes of hello.txt': {
158     topic : function(){
159       var options = {
160         url: TEST_SERVER + '/hello.txt',
161         headers: {
162           'Range': 'bytes=6-10'
163         }
164       };
165       request.get(options, this.callback);
166     },
167     'should respond with 206' : function(error, response, body){
168       assert.equal(response.statusCode, 206);
169     },
170     'should respond with text/plain': function(error, response, body){
171       assert.equal(response.headers['content-type'], 'text/plain');
172     },
173     'should have content-length of 5 bytes': function(error, response, body){
174       assert.equal(response.headers['content-length'], 5);
175     },
176     'should have a valid Content-Range header in response': function(error, response, body){
177       assert.equal(response.headers['content-range'], 'bytes 6-10/11');
178     },
179     'should respond with world': function(error, response, body){
180       assert.equal(body, 'world');
181     }
182   }
183 }).addBatch({
184   'serving all from the start of hello.txt': {
185     topic : function(){
186       var options = {
187         url: TEST_SERVER + '/hello.txt',
188         headers: {
189           'Range': 'bytes=0-'
190         }
191       };
192       request.get(options, this.callback);
193     },
194     'should respond with 206' : function(error, response, body){
195       assert.equal(response.statusCode, 206);
196     },
197     'should respond with text/plain': function(error, response, body){
198       assert.equal(response.headers['content-type'], 'text/plain');
199     },
200     'should have content-length of 11 bytes': function(error, response, body){
201       assert.equal(response.headers['content-length'], 11);
202     },
203     'should have a valid Content-Range header in response': function(error, response, body){
204       assert.equal(response.headers['content-range'], 'bytes 0-10/11');
205     },
206     'should respond with "hello world"': function(error, response, body){
207       assert.equal(body, 'hello world');
208     }
209   }
210 }).addBatch({
211   'serving directory index': {
212     topic : function(){
213       request.get(TEST_SERVER, this.callback);
214     },
215     'should respond with 200' : function(error, response, body){
216       assert.equal(response.statusCode, 200);
217     },
218     'should respond with text/html': function(error, response, body){
219       assert.equal(response.headers['content-type'], 'text/html');
220     }
221   }
222 }).addBatch({
223   'serving index.html from the cache': {
224     topic : function(){
225       request.get(TEST_SERVER + '/index.html', this.callback);
226     },
227     'should respond with 200' : function(error, response, body){
228       assert.equal(response.statusCode, 200);
229     },
230     'should respond with text/html': function(error, response, body){
231       assert.equal(response.headers['content-type'], 'text/html');
232     }
233   }
234 }).addBatch({
235   'requesting with If-None-Match': {
236     topic : function(){
237       var _this = this;
238       request.get(TEST_SERVER + '/index.html', function(error, response, body){
239         request({
240           method: 'GET',
241           uri: TEST_SERVER + '/index.html',
242           headers: {'if-none-match': response.headers['etag']}
243         },
244         _this.callback);
245       });
246     },
247     'should respond with 304' : function(error, response, body){
248       assert.equal(response.statusCode, 304);
249     }
250   },
251   'requesting with If-None-Match and If-Modified-Since': {
252     topic : function(){
253       var _this = this;
254       request.get(TEST_SERVER + '/index.html', function(error, response, body){
255         var modified = Date.parse(response.headers['last-modified']);
256         var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString();
257         var nonMatchingEtag = '1111222233334444';
258         request({
259           method: 'GET',
260           uri: TEST_SERVER + '/index.html',
261           headers: {
262             'if-none-match': nonMatchingEtag,
263             'if-modified-since': oneDayLater
264           }
265         },
266         _this.callback);
267       });
268     },
269     'should respond with a 200': function(error, response, body){
270       assert.equal(response.statusCode, 200);
271     }
272   }
273 })
274 .addBatch({
275   'requesting POST': {
276     topic : function(){
277       request.post(TEST_SERVER + '/index.html', this.callback);
278     },
279     'should respond with 200' : function(error, response, body){
280       assert.equal(response.statusCode, 200);
281     },
282     'should not be empty' : function(error, response, body){
283       assert.isNotEmpty(body);
284     }
285   }
286 })
287 .addBatch({
288   'requesting HEAD': {
289     topic : function(){
290       request.head(TEST_SERVER + '/index.html', this.callback);
291     },
292     'should respond with 200' : function(error, response, body){
293       assert.equal(response.statusCode, 200);
294     },
295     'head must has no body' : function(error, response, body){
296       assert.isEmpty(body);
297     }
298   }
299 })
300 .addBatch(headers)
301 .addBatch({
302   'addings custom mime types': {
303     topic : function(){
304       static.mime.define({'application/font-woff': ['woff']});
305       this.callback();
306     },
307     'should add woff' : function(error, response, body){
308       assert.equal(static.mime.lookup('woff'), 'application/font-woff');
309     }
310   }
311 })
312 .addBatch({
313   'serving subdirectory index': {
314     topic : function(){
315       request.get(TEST_SERVER + '/there/', this.callback); // with trailing slash
316     },
317     'should respond with 200' : function(error, response, body){
318       assert.equal(response.statusCode, 200);
319     },
320     'should respond with text/html': function(error, response, body){
321       assert.equal(response.headers['content-type'], 'text/html');
322     }
323   }
324 })
325 .addBatch({
326   'redirecting to subdirectory index': {
327     topic : function(){
328       request.get({ url: TEST_SERVER + '/there', followRedirect: false }, this.callback); // without trailing slash
329     },
330     'should respond with 301' : function(error, response, body){
331       assert.equal(response.statusCode, 301);
332     },
333     'should respond with location header': function(error, response, body){
334       assert.equal(response.headers['location'], '/there/'); // now with trailing slash
335     },
336     'should respond with empty string body' : function(error, response, body){
337       assert.equal(body, '');
338     }
339   }
340 })
341 .addBatch({
342   'requesting a subdirectory (with trailing slash) not found': {
343     topic : function(){
344       request.get(TEST_SERVER + '/notthere/', this.callback); // with trailing slash
345     },
346     'should respond with 404' : function(error, response, body){
347       assert.equal(response.statusCode, 404);
348     }
349   }
350 })
351 .addBatch({
352   'requesting a subdirectory (without trailing slash) not found': {
353     topic : function(){
354       request.get({ url: TEST_SERVER + '/notthere', followRedirect: false }, this.callback); // without trailing slash
355     },
356     'should respond with 404' : function(error, response, body){
357       assert.equal(response.statusCode, 404);
358     }
359   }
360 }).addBatch({
361   'once an http server is listening with custom index configuration': {
362     topic: function () {
363       server.close();
364
365       fileServer  = new static.Server(__dirname + '/../fixtures', { indexFile: "hello.txt" });
366
367       server = require('http').createServer(function (request, response) {
368         fileServer.serve(request, response);
369       }).listen(TEST_PORT, this.callback)
370     },
371     'should be listening' : function(){
372       /* This test is necessary to ensure the topic execution.
373        * A topic without tests will be not executed */
374       assert.isTrue(true);
375     }
376   }
377 }).addBatch({
378   'serving custom index file': {
379     topic : function(){
380       request.get(TEST_SERVER + '/', this.callback);
381     },
382     'should respond with 200' : function(error, response, body){
383       assert.equal(response.statusCode, 200);
384     },
385     'should respond with empty string': function(error, response, body){
386       assert.equal(body, 'hello world');
387     }
388   }
389 }).export(module);
390